Class: CarrierWave::Storage::S3

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

Overview

Uploads things to Amazon S3 using the “fog” gem. You’ll need to specify the access_key_id, secret_access_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 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

The following access policies are available:

: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

You can specify a region. US Standard “us-east-1” is the default.

CarrierWave.configure do |config|
  config.s3_region = 'eu-west-1'
end

Available options are defined in Fog Storage

'eu-west-1' => 's3-eu-west-1.amazonaws.com'
'us-east-1' => 's3.amazonaws.com'
'ap-southeast-1' => 's3-ap-southeast-1.amazonaws.com'
'us-west-1' => 's3-us-west-1.amazonaws.com'

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



219
220
221
222
223
224
225
# File 'lib/carrierwave/storage/s3.rb', line 219

def connection
  @connection ||= Fog::AWS::Storage.new(
    :aws_access_key_id => uploader.s3_access_key_id,
    :aws_secret_access_key => uploader.s3_secret_access_key,
    :region => uploader.s3_region
  )
end

#retrieve!(identifier) ⇒ Object

Do something to retrieve the file

identifier (String)

uniquely identifies the file

Returns

CarrierWave::Storage::S3::File

the stored file

Parameters:

  • identifier (String)

    uniquely identifies the file



215
216
217
# File 'lib/carrierwave/storage/s3.rb', line 215

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::S3::File

the stored file



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

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