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



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

def authenticated_url
  connection.get_object_url(bucket, path, Time.now + authentication_timeout)
end

#content_typeObject



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

def content_type
  headers["Content-Type"]
end

#content_type=(type) ⇒ Object



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

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



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

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
132
# File 'lib/carrierwave/storage/s3.rb', line 125

def public_url
  scheme = use_ssl? ? 'https' : 'http'
  if cnamed?
    ["#{scheme}://#{bucket}", path].compact.join('/')
  else
    ["#{scheme}://#{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



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

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

#store(file) ⇒ Object



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

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