Class: CarrierWave::Storage::S3::File
- Inherits:
-
Object
- Object
- CarrierWave::Storage::S3::File
- Defined in:
- lib/carrierwave/storage/s3.rb
Instance Method Summary collapse
- #authenticated_url ⇒ Object
- #content_type ⇒ Object
- #content_type=(type) ⇒ Object
-
#delete ⇒ Object
Remove the file from Amazon S3.
-
#headers ⇒ Object
Headers returned from file retrieval.
-
#initialize(uploader, base, path) ⇒ File
constructor
A new instance of File.
-
#path ⇒ Object
Returns the current path of the file on S3.
- #public_url ⇒ Object
-
#read ⇒ Object
Reads the contents of the file from S3.
- #size ⇒ Object
- #store(file) ⇒ Object
-
#url ⇒ Object
Returns the url on Amazon’s S3 service.
Constructor Details
#initialize(uploader, base, path) ⇒ File
Returns a new instance of File.
73 74 75 76 77 |
# File 'lib/carrierwave/storage/s3.rb', line 73 def initialize(uploader, base, path) @uploader = uploader @path = path @base = base end |
Instance Method Details
#authenticated_url ⇒ Object
134 135 136 |
# File 'lib/carrierwave/storage/s3.rb', line 134 def authenticated_url connection.get_object_https_url(bucket, path, Time.now + authentication_timeout) end |
#content_type ⇒ Object
148 149 150 |
# File 'lib/carrierwave/storage/s3.rb', line 148 def content_type headers["Content-Type"] end |
#content_type=(type) ⇒ Object
152 153 154 |
# File 'lib/carrierwave/storage/s3.rb', line 152 def content_type=(type) headers["Content-Type"] = type end |
#delete ⇒ Object
Remove the file from Amazon S3
106 107 108 |
# File 'lib/carrierwave/storage/s3.rb', line 106 def delete connection.delete_object(bucket, @path) end |
#headers ⇒ Object
Headers returned from file retrieval
161 162 163 164 165 166 167 |
# File 'lib/carrierwave/storage/s3.rb', line 161 def headers @headers ||= begin connection.head_object(bucket, @path).headers rescue Excon::Errors::NotFound # Don't die, just return no headers {} end end |
#path ⇒ Object
Returns the current path of the file on S3
Returns
- String
-
A path
86 87 88 |
# File 'lib/carrierwave/storage/s3.rb', line 86 def path @path end |
#public_url ⇒ Object
125 126 127 128 129 130 131 132 |
# File 'lib/carrierwave/storage/s3.rb', line 125 def public_url scheme = use_ssl? ? 'https' : 'http' if cnamed? ["#{scheme}://#{bucket}", path].compact.join('/') else ["#{scheme}://#{bucket}.s3.amazonaws.com", path].compact.join('/') end end |
#read ⇒ Object
Reads the contents of the file from S3
Returns
- String
-
contents of the file
97 98 99 100 101 |
# File 'lib/carrierwave/storage/s3.rb', line 97 def read result = connection.get_object(bucket, @path) @headers = result.headers result.body end |
#size ⇒ Object
156 157 158 |
# File 'lib/carrierwave/storage/s3.rb', line 156 def size headers['Content-Length'].to_i end |
#store(file) ⇒ Object
138 139 140 141 142 143 144 145 146 |
# File 'lib/carrierwave/storage/s3.rb', line 138 def store(file) content_type ||= file.content_type # this might cause problems if content type changes between read and upload (unlikely) connection.put_object(bucket, path, file.read, { 'x-amz-acl' => access_policy.to_s.gsub('_', '-'), 'Content-Type' => content_type }.merge(@uploader.s3_headers) ) end |
#url ⇒ Object
Returns the url on Amazon’s S3 service
Returns
- String
-
file’s url
117 118 119 120 121 122 123 |
# File 'lib/carrierwave/storage/s3.rb', line 117 def url if access_policy == :authenticated_read authenticated_url else public_url end end |