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.



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

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

Instance Method Details

#authenticated_urlObject



131
132
133
# File 'lib/carrierwave/storage/s3.rb', line 131

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

#content_typeObject



145
146
147
# File 'lib/carrierwave/storage/s3.rb', line 145

def content_type
  headers["Content-Type"]
end

#content_type=(type) ⇒ Object



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

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

#deleteObject

Remove the file from Amazon S3



93
94
95
# File 'lib/carrierwave/storage/s3.rb', line 93

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

#headersObject

Headers returned from file retrieval



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

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



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

def path
  @path
end

#public_urlObject



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

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



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

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

#rename(new_path) ⇒ Object



97
98
99
100
101
102
103
104
105
106
# File 'lib/carrierwave/storage/s3.rb', line 97

def rename(new_path)
  file = connection.put_object(bucket, new_path, read,
    {
      'x-amz-acl' => access_policy.to_s.gsub('_', '-'),
      'Content-Type' => content_type
    }.merge(@uploader.s3_headers)
  )
  delete
  file
end

#sizeObject



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

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

#store(file) ⇒ Object



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

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



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

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