Class: CarrierWave::Storage::S3::File

Inherits:
Object
  • Object
show all
Defined in:
lib/carrierwave/storage/s3.rb

Instance Method Summary collapse

Constructor Details

#initialize(uploader, base, path) ⇒ File

Returns a new instance of File.



65
66
67
68
69
# File 'lib/carrierwave/storage/s3.rb', line 65

def initialize(uploader, base, path)
  @uploader = uploader
  @path = path
  @base = base
end

Instance Method Details

#access_policyObject

The Amazon S3 Access policy ready to send in storage request headers.



128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/carrierwave/storage/s3.rb', line 128

def access_policy
  return @access_policy unless @access_policy.blank?
  if @uploader.s3_access_policy.blank? 
    if !@uploader.s3_access.blank?
      @access_policy = @uploader.s3_access.to_s.gsub(/_/, '-')
    else
      @access_policy = 'public-read'
    end
  else
    @access_policy = @uploader.s3_access_policy
  end
end

#content_typeObject



141
142
143
# File 'lib/carrierwave/storage/s3.rb', line 141

def content_type
  headers["content-type"]
end

#content_type=(type) ⇒ Object



145
146
147
# File 'lib/carrierwave/storage/s3.rb', line 145

def content_type=(type)
  headers["content-type"] = type
end

#deleteObject

Remove the file from Amazon S3



98
99
100
# File 'lib/carrierwave/storage/s3.rb', line 98

def delete
  connection.delete(bucket, @path)
end

#headersObject

Headers returned from file retrieval



150
151
152
# File 'lib/carrierwave/storage/s3.rb', line 150

def headers
  @headers ||= {}
end

#pathObject

Returns the current path of the file on S3

Returns

String

A path



78
79
80
# File 'lib/carrierwave/storage/s3.rb', line 78

def path
  @path
end

#readObject

Reads the contents of the file from S3

Returns

String

contents of the file



89
90
91
92
93
# File 'lib/carrierwave/storage/s3.rb', line 89

def read
  result = connection.get(bucket, @path)
  @headers = result[:headers]
  result[:object]
end

#store(file) ⇒ Object



117
118
119
120
121
122
123
124
125
# File 'lib/carrierwave/storage/s3.rb', line 117

def store(file)
  content_type ||= file.content_type # this might cause problems if content type changes between read and upload (unlikely)
  connection.put(bucket, @path, file.read,
    {
      'x-amz-acl' => access_policy,
      'content-type' => content_type
    }.merge(@uploader.s3_headers)
  )
end

#urlObject

Returns the url on Amazon’s S3 service

Returns

String

file’s url



109
110
111
112
113
114
115
# File 'lib/carrierwave/storage/s3.rb', line 109

def url
  if @uploader.s3_cnamed
    ["http://#{@uploader.s3_bucket}", @path].compact.join('/')
  else
    ["http://#{@uploader.s3_bucket}.s3.amazonaws.com", @path].compact.join('/')
  end
end