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 assign HTTP headers to be used when S3 serves your files:

CarrierWave.configure do |config|
  config.s3_headers = {"Content-Disposition" => "attachment; filename=foo.jpg;"}
end

You can also set the headers dynamically by overriding the s3_headers method:

class MyUploader < CarrierWave::Uploader::Base
  def s3_headers
    { "Expires" => 1.year.from_how.httpdate }
  end
end

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

FIXME maybe need to mangle here, too

Parameters:



190
191
192
193
# File 'lib/carrierwave/storage/s3.rb', line 190

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



170
171
172
173
174
175
176
# File 'lib/carrierwave/storage/s3.rb', line 170

def store!(file)
  connect!(uploader)
  s3_options = {:access => uploader.s3_access, :content_type => file.content_type}
  s3_options.merge!(uploader.s3_headers)
  AWS::S3::S3Object.store(File.mangle_path_for_s3(uploader.store_path), file.read, uploader.s3_bucket, s3_options)
  CarrierWave::Storage::S3::File.new(uploader, uploader.store_path)
end