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.config[:s3][:access_key_id] = "xxxxxx"
CarrierWave.config[:s3][:secret_access_key] = "xxxxxx"
CarrierWave.config[:s3][:bucket] = "my_bucket_name"

You can also set the access policy for the uploaded files:

CarrierWave.config[:s3][:access] = :public_read

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.config[:s3][:cnamed] = true

No the resulting url will be

http://bucket_name.domain.tld/path/to/file

instead of

http://s3.amazonaws.com/bucket_name.domain.tld/path/to/file

Defined Under Namespace

Classes: File

Instance Attribute Summary

Attributes inherited from Abstract

#uploader

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Abstract

#identifier, #initialize

Constructor Details

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

Class Method Details

.accessObject

Returns

Symbol

the access priviliges the uploaded files should have



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

def self.access
  CarrierWave.config[:s3][:access]
end

.bucketObject

Returns

String

the bucket set in the config options



158
159
160
# File 'lib/carrierwave/storage/s3.rb', line 158

def self.bucket
  CarrierWave.config[:s3][:bucket]
end

.setup!Object

Connect to Amazon S3



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

def self.setup!
  require 'aws/s3'
  AWS::S3::Base.establish_connection!(
    :access_key_id     => CarrierWave.config[:s3][:access_key_id],
    :secret_access_key => CarrierWave.config[:s3][:secret_access_key]
  )
end

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:



209
210
211
# File 'lib/carrierwave/storage/s3.rb', line 209

def retrieve!(identifier)
  CarrierWave::Storage::S3::File.new(uploader.store_path(identifier), 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



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

def store!(file)
  AWS::S3::S3Object.store(::File.join(uploader.store_path), file.read, self.class.bucket, :access => self.class.access)
  CarrierWave::Storage::S3::File.new(uploader.store_path, uploader.filename)
end