Class: CarrierWave::Storage::S3

Inherits:
Abstract show all
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 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

#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

#retrieve!(identifier) ⇒ Object

Do something to retrieve the file

identifier (String)

uniquely identifies the file

Returns

CarrierWave::Storage::S3::File

the stored file

Parameters:



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

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



150
151
152
153
154
# File 'lib/carrierwave/storage/s3.rb', line 150

def store!(file)
  connect!(uploader)
  AWS::S3::S3Object.store(uploader.store_path, file.read, uploader.s3_bucket, :access => uploader.s3_access)
  CarrierWave::Storage::S3::File.new(uploader, uploader.store_path)
end