class CarrierWave::Storage::S3::File

Public Instance Methods

access_policy() click to toggle source

The Amazon S3 Access policy ready to send in storage request headers.

# File lib/carrierwave/storage/s3.rb, line 127
def access_policy
  return @access_policy unless @access_policy.blank?
  if @uploader.s3_access_policy.blank? 
    if !@uploader.s3_access.blank?
      @access_policy = @uploader.s3_access.to_s.gsub(/_/, '-')
    else
      @access_policy = 'public-read'
    end
  else
    @access_policy = @uploader.s3_access_policy
  end
end
content_type() click to toggle source
# File lib/carrierwave/storage/s3.rb, line 140
def content_type
  headers["content-type"]
end
content_type=(type) click to toggle source
# File lib/carrierwave/storage/s3.rb, line 144
def content_type=(type)
  headers["content-type"] = type
end
delete() click to toggle source

Remove the file from Amazon S3

# File lib/carrierwave/storage/s3.rb, line 97
def delete
  connection.delete(bucket, @path)
end
headers() click to toggle source

Headers returned from file retrieval

# File lib/carrierwave/storage/s3.rb, line 149
def headers
  @headers ||= {}
end
path() click to toggle source

Returns the current path of the file on S3

Returns

String

A path

# File lib/carrierwave/storage/s3.rb, line 77
def path
  @path
end
read() click to toggle source

Reads the contents of the file from S3

Returns

String

contents of the file

# File lib/carrierwave/storage/s3.rb, line 88
def read
  result = connection.get(bucket, @path)
  @headers = result[:headers]
  result[:object]
end
store(file) click to toggle source
# File lib/carrierwave/storage/s3.rb, line 116
def store(file)
  content_type ||= file.content_type # this might cause problems if content type changes between read and upload (unlikely)
  connection.put(bucket, @path, file.read,
    {
      'x-amz-acl' => access_policy,
      'content-type' => content_type
    }.merge(@uploader.s3_headers)
  )
end
url() click to toggle source

Returns the url on Amazon’s S3 service

Returns

String

file’s url

# File lib/carrierwave/storage/s3.rb, line 108
def url
  if @uploader.s3_cnamed
    ["http://#{@uploader.s3_bucket}", @path].compact.join('/')
  else
    ["http://#{@uploader.s3_bucket}.s3.amazonaws.com", @path].compact.join('/')
  end
end

Public Class Methods

new(uploader, base, path) click to toggle source
# File lib/carrierwave/storage/s3.rb, line 64
def initialize(uploader, base, path)
  @uploader = uploader
  @path = path
  @base = base
end