Class: CarrierWave::Storage::S3::File

Inherits:
Object
  • Object
show all
Defined in:
lib/carrierwave/storage/s3.rb

Instance Method Summary collapse

Constructor Details

#initialize(uploader, base, path) ⇒ File

Returns a new instance of File.



73
74
75
76
77
# File 'lib/carrierwave/storage/s3.rb', line 73

def initialize(uploader, base, path)
  @uploader = uploader
  @path = path
  @base = base
end

Instance Method Details

#authenticated_urlObject



133
134
135
# File 'lib/carrierwave/storage/s3.rb', line 133

def authenticated_url
  connection.get_object_url(bucket, path, Time.now + 60 * 10)
end

#content_typeObject



147
148
149
# File 'lib/carrierwave/storage/s3.rb', line 147

def content_type
  headers["Content-Type"]
end

#content_type=(type) ⇒ Object



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

def content_type=(type)
  headers["Content-Type"] = type
end

#deleteObject

Remove the file from Amazon S3



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

def delete
  connection.delete_object(bucket, @path)
end

#headersObject

Headers returned from file retrieval



160
161
162
163
164
165
166
# File 'lib/carrierwave/storage/s3.rb', line 160

def headers
  @headers ||= begin
    connection.head_object(bucket, @path).headers
  rescue Excon::Errors::NotFound # Don't die, just return no headers
    {}
  end
end

#pathObject

Returns the current path of the file on S3

Returns

String

A path



86
87
88
# File 'lib/carrierwave/storage/s3.rb', line 86

def path
  @path
end

#public_urlObject



125
126
127
128
129
130
131
# File 'lib/carrierwave/storage/s3.rb', line 125

def public_url
  if cnamed?
    ["http://#{bucket}", path].compact.join('/')
  else
    ["http://#{bucket}.s3.amazonaws.com", path].compact.join('/')
  end
end

#readObject

Reads the contents of the file from S3

Returns

String

contents of the file



97
98
99
100
101
# File 'lib/carrierwave/storage/s3.rb', line 97

def read
  result = connection.get_object(bucket, @path)
  @headers = result.headers
  result.body
end

#sizeObject



155
156
157
# File 'lib/carrierwave/storage/s3.rb', line 155

def size
   headers['Content-Length'].to_i
end

#store(file) ⇒ Object



137
138
139
140
141
142
143
144
145
# File 'lib/carrierwave/storage/s3.rb', line 137

def store(file)
  content_type ||= file.content_type # this might cause problems if content type changes between read and upload (unlikely)
  connection.put_object(bucket, path, file.read,
    {
      'x-amz-acl' => access_policy.to_s.gsub('_', '-'),
      'Content-Type' => content_type
    }.merge(@uploader.s3_headers)
  )
end

#urlObject

Returns the url on Amazon’s S3 service

Returns

String

file’s url



117
118
119
120
121
122
123
# File 'lib/carrierwave/storage/s3.rb', line 117

def url
  if access_policy == :authenticated_read
    authenticated_url
  else
    public_url
  end
end