Class: CarrierWave::Storage::S3

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

Overview

Uploads things to Amazon S3 webservices using the RightAWS libraries (right_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

Defined Under Namespace

Classes: File

Instance Attribute Summary

Attributes inherited from Abstract

#uploader

Instance Method Summary collapse

Methods inherited from Abstract

#identifier, #initialize

Constructor Details

This class inherits a constructor from CarrierWave::Storage::Abstract

Instance Method Details

#connectionObject



197
198
199
200
201
202
# File 'lib/carrierwave/storage/s3.rb', line 197

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) ⇒ Object

Do something to retrieve the file

identifier (String)

uniquely identifies the file

Returns

CarrierWave::Storage::RightS3::File

the stored file

Parameters:

  • identifier (String)

    uniquely identifies the file



193
194
195
# File 'lib/carrierwave/storage/s3.rb', line 193

def retrieve!(identifier)
  CarrierWave::Storage::S3::File.new(uploader, self, uploader.store_path(identifier))
end

#store!(file) ⇒ Object

Store the file on S3

Parameters

file (CarrierWave::SanitizedFile)

the file to store

Returns

CarrierWave::Storage::RightS3::File

the stored file



177
178
179
180
181
# File 'lib/carrierwave/storage/s3.rb', line 177

def store!(file)
  f = CarrierWave::Storage::S3::File.new(uploader, self, uploader.store_path)
  f.store(file)
  f
end