Class: CarrierWave::Storage::S3

Inherits:
Abstract
  • Object
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.

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Abstract

#path

Constructor Details

#initialize(store_path, identifier) ⇒ S3

Returns a new instance of S3.



32
33
34
35
# File 'lib/carrierwave/storage/s3.rb', line 32

def initialize(store_path, identifier)
  @store_path = store_path
  @identifier = identifier
end

Class Method Details

.accessObject

Returns

Symbol

the access priviliges the uploaded files should have



62
63
64
# File 'lib/carrierwave/storage/s3.rb', line 62

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

.bucketObject

Returns

String

the bucket set in the config options



53
54
55
# File 'lib/carrierwave/storage/s3.rb', line 53

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

.retrieve!(uploader, identifier) ⇒ Object

Do something to retrieve the file

uploader (CarrierWave::Uploader)

an uploader object

identifier (String)

uniquely identifies the file

Returns

CarrierWave::Storage::S3

the stored file

Parameters:

  • uploader (CarrierWave::Uploader)

    an uploader object

  • identifier (String)

    uniquely identifies the file



95
96
97
# File 'lib/carrierwave/storage/s3.rb', line 95

def self.retrieve!(uploader, identifier)
  self.new(uploader.store_path(identifier), identifier)
end

.setup!Object

Connect to Amazon S3



40
41
42
43
44
45
46
# File 'lib/carrierwave/storage/s3.rb', line 40

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

.store!(uploader, file) ⇒ Object

Store the file on S3

Parameters

uploader (CarrierWave::Uploader)

an uploader object

file (CarrierWave::SanitizedFile)

the file to store

Returns

CarrierWave::Storage::S3

the stored file



78
79
80
81
# File 'lib/carrierwave/storage/s3.rb', line 78

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

Instance Method Details

#identifierObject

Returns the filename on S3

Returns

String

path to the file



106
107
108
# File 'lib/carrierwave/storage/s3.rb', line 106

def identifier
  @identifier
end

#readObject

Reads the contents of the file from S3

Returns

String

contents of the file



117
118
119
# File 'lib/carrierwave/storage/s3.rb', line 117

def read
  S3Object.value @store_path, self.class.bucket
end

#urlObject

Returns the url on Amazon’s S3 service

Returns

String

file’s url



128
129
130
# File 'lib/carrierwave/storage/s3.rb', line 128

def url
  ["http://s3.amazonaws.com", self.class.bucket, @store_path].compact.join('/')
end