class CarrierWave::Storage::S3

Uploads things to Amazon S3 webservices using the “aws” library (aws 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

The AWS::S3Interface is used directly as opposed to the normal AWS::S3::Bucket et.al. classes. This gives much improved performance and avoids unnecessary requests.

You can set the access policy for the uploaded files:

CarrierWave.configure do |config|
  config.s3_access_policy = 'public-read'
end

The default is ‘public-read’. For more options see:

docs.amazonwebservices.com/AmazonS3/latest/RESTAccessPolicy.html#RESTCannedAccessPolicies

For backwards compatability with the original aws-s3 library, if the old config.s3_access is set it will be converted to the appropriate access policy:

: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.

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://bucketname.domain.tld.s3.amazonaws.com/path/to/file

Public Instance Methods

connection() click to toggle source
# File lib/carrierwave/storage/s3.rb, line 196
def connection
  @connection ||= Aws::S3Interface.new(
    uploader.s3_access_key_id, uploader.s3_secret_access_key,
    :multi_thread => uploader.s3_multi_thread
  )
end
retrieve!(identifier) click to toggle source

Do something to retrieve the file

@param [String] identifier uniquely identifies the file

identifier (String)

uniquely identifies the file

Returns

CarrierWave::Storage::RightS3::File

the stored file

# File lib/carrierwave/storage/s3.rb, line 192
def retrieve!(identifier)
  CarrierWave::Storage::S3::File.new(uploader, self, uploader.store_path(identifier))
end
store!(file) click to toggle source

Store the file on S3

Parameters

file (CarrierWave::SanitizedFile)

the file to store

Returns

CarrierWave::Storage::RightS3::File

the stored file

# File lib/carrierwave/storage/s3.rb, line 176
def store!(file)
  f = CarrierWave::Storage::S3::File.new(uploader, self, uploader.store_path)
  f.store(file)
  f
end