Class: CarrierWave::Storage::S3
- Defined in:
- lib/carrierwave/storage/s3.rb
Overview
Uploads things to Amazon S3 webservices. It requies the aws/s3 gem. In order for CarrierWave to connect to Amazon S3, you’ll need to specify an access key id, secret key and bucket:
CarrierWave.configure do |config|
config.s3_access_key_id = "xxxxxx"
config.s3_secret_access_key = "xxxxxx"
config.s3_bucket = "my_bucket_name"
end
You can also set the access policy for the uploaded files:
CarrierWave.configure do |config|
config.s3_access = :public
end
Possible values are the ‘canned access control policies’ provided in the aws/s3 gem, they are:
- :private
-
No one else has any access rights.
- :public_read
-
The anonymous principal is granted READ access. If this policy is used on an object, it can be read from a browser with no authentication.
- :public_read_write
-
The anonymous principal is granted READ and WRITE access.
- :authenticated_read
-
Any principal authenticated as a registered Amazon S3 user is granted READ access.
The default is :public_read, it should work in most cases.
You can assign HTTP headers to be used when S3 serves your files:
CarrierWave.configure do |config|
config.s3_headers = {"Content-Disposition" => "attachment; filename=foo.jpg;"}
end
You can also set the headers dynamically by overriding the s3_headers method:
class MyUploader < CarrierWave::Uploader::Base
def s3_headers
{ "Expires" => 1.year.from_how.httpdate }
end
end
You can change the generated url to a cnamed domain by setting the cnamed config:
CarrierWave.configure do |config|
config.s3_cnamed = true
config.s3_bucket = 'bucketname.domain.tld'
end
Now the resulting url will be
http://bucketname.domain.tld/path/to/file
instead of
http://s3.amazonaws.com/bucketname.domain.tld/path/to/file
Defined Under Namespace
Classes: File
Instance Attribute Summary
Attributes inherited from Abstract
Instance Method Summary collapse
-
#retrieve!(identifier) ⇒ Object
Do something to retrieve the file.
-
#store!(file) ⇒ Object
Store the file on S3.
Methods inherited from Abstract
Constructor Details
This class inherits a constructor from CarrierWave::Storage::Abstract
Instance Method Details
#retrieve!(identifier) ⇒ Object
Do something to retrieve the file
- identifier (String)
-
uniquely identifies the file
Returns
- CarrierWave::Storage::S3::File
-
the stored file
183 184 185 186 |
# File 'lib/carrierwave/storage/s3.rb', line 183 def retrieve!(identifier) connect!(uploader) CarrierWave::Storage::S3::File.new(uploader, uploader.store_path(identifier)) end |
#store!(file) ⇒ Object
Store the file on S3
Parameters
- file (CarrierWave::Storage::S3::File)
-
the file to store
Returns
- CarrierWave::Storage::S3
-
the stored file
164 165 166 167 168 169 170 |
# File 'lib/carrierwave/storage/s3.rb', line 164 def store!(file) connect!(uploader) = {:access => uploader.s3_access, :content_type => file.content_type} .merge!(uploader.s3_headers) AWS::S3::S3Object.store(uploader.store_path, file.read, uploader.s3_bucket, ) CarrierWave::Storage::S3::File.new(uploader, uploader.store_path) end |