Class: CarrierWave::Storage::RightS3

Inherits:
Abstract show all
Defined in:
lib/carrierwave/storage/right_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 RightAWS::S3Interface is used directly as opposed to the normal RightAWS::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

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



167
168
169
# File 'lib/carrierwave/storage/right_s3.rb', line 167

def connection
  @connection ||= RightAws::S3Interface.new(uploader.s3_access_key_id, uploader.s3_secret_access_key, {:logger => Logger.new('/home/ivan/s3.log')})
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



163
164
165
# File 'lib/carrierwave/storage/right_s3.rb', line 163

def retrieve!(identifier)
  CarrierWave::Storage::RightS3::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



147
148
149
150
151
# File 'lib/carrierwave/storage/right_s3.rb', line 147

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