2010-10-04 14:02:08 -07:00
|
|
|
require 'fog/core/model'
|
2011-12-29 16:19:39 -05:00
|
|
|
require 'fog/aws/models/storage/versions'
|
2010-03-13 13:37:24 -08:00
|
|
|
|
2009-08-02 16:37:54 -07:00
|
|
|
module Fog
|
2011-06-15 14:26:43 -07:00
|
|
|
module Storage
|
|
|
|
class AWS
|
2009-08-02 16:37:54 -07:00
|
|
|
|
2010-01-14 20:44:39 -08:00
|
|
|
class File < Fog::Model
|
2009-08-02 16:37:54 -07:00
|
|
|
|
2010-09-07 11:30:02 -07:00
|
|
|
identity :key, :aliases => 'Key'
|
2009-10-23 10:27:23 -07:00
|
|
|
|
2010-10-12 18:07:36 -07:00
|
|
|
attr_writer :body
|
2010-11-08 17:14:21 -08:00
|
|
|
attribute :cache_control, :aliases => 'Cache-Control'
|
|
|
|
attribute :content_disposition, :aliases => 'Content-Disposition'
|
|
|
|
attribute :content_encoding, :aliases => 'Content-Encoding'
|
2011-01-26 15:56:50 -08:00
|
|
|
attribute :content_length, :aliases => ['Content-Length', 'Size'], :type => :integer
|
2010-11-08 17:14:21 -08:00
|
|
|
attribute :content_md5, :aliases => 'Content-MD5'
|
|
|
|
attribute :content_type, :aliases => 'Content-Type'
|
|
|
|
attribute :etag, :aliases => ['Etag', 'ETag']
|
|
|
|
attribute :expires, :aliases => 'Expires'
|
2010-12-30 12:54:22 -08:00
|
|
|
attribute :last_modified, :aliases => ['Last-Modified', 'LastModified']
|
2011-01-04 11:01:04 -08:00
|
|
|
attribute :metadata
|
2010-11-08 17:14:21 -08:00
|
|
|
attribute :owner, :aliases => 'Owner'
|
|
|
|
attribute :storage_class, :aliases => ['x-amz-storage-class', 'StorageClass']
|
2012-01-14 12:27:58 -08:00
|
|
|
attribute :encryption, :aliases => 'x-amz-server-side-encryption'
|
2011-12-29 14:08:22 -05:00
|
|
|
attribute :version, :aliases => 'x-amz-version-id'
|
2009-08-02 16:37:54 -07:00
|
|
|
|
2012-03-04 12:56:12 -05:00
|
|
|
# Chunk size to use for multipart uploads
|
|
|
|
# Use small chunk sizes to minimize memory
|
|
|
|
# E.g. 5242880 = 5mb
|
|
|
|
attr_accessor :multipart_chunk_size
|
|
|
|
|
2010-09-23 09:38:55 -07:00
|
|
|
def acl=(new_acl)
|
|
|
|
valid_acls = ['private', 'public-read', 'public-read-write', 'authenticated-read']
|
|
|
|
unless valid_acls.include?(new_acl)
|
|
|
|
raise ArgumentError.new("acl must be one of [#{valid_acls.join(', ')}]")
|
|
|
|
end
|
|
|
|
@acl = new_acl
|
|
|
|
end
|
|
|
|
|
2010-02-19 11:30:23 -08:00
|
|
|
def body
|
2010-11-19 13:45:45 -08:00
|
|
|
attributes[:body] ||= if last_modified && (file = collection.get(identity))
|
2010-03-30 21:09:35 -07:00
|
|
|
file.body
|
2010-02-19 11:30:23 -08:00
|
|
|
else
|
|
|
|
''
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-11-19 13:45:45 -08:00
|
|
|
def body=(new_body)
|
|
|
|
attributes[:body] = new_body
|
|
|
|
end
|
|
|
|
|
2010-01-14 20:44:39 -08:00
|
|
|
def directory
|
|
|
|
@directory
|
2009-08-27 20:47:01 -07:00
|
|
|
end
|
|
|
|
|
2011-09-04 09:41:42 -04:00
|
|
|
def copy(target_directory_key, target_file_key, options = {})
|
2010-01-14 20:44:39 -08:00
|
|
|
requires :directory, :key
|
2011-09-04 09:41:42 -04:00
|
|
|
connection.copy_object(directory.key, key, target_directory_key, target_file_key, options)
|
2010-05-03 15:02:08 -07:00
|
|
|
target_directory = connection.directories.new(:key => target_directory_key)
|
2011-10-27 19:49:10 +02:00
|
|
|
target_directory.files.head(target_file_key)
|
2009-08-04 20:09:08 -07:00
|
|
|
end
|
|
|
|
|
2011-12-29 14:08:58 -05:00
|
|
|
def destroy(options = {})
|
2010-01-14 20:44:39 -08:00
|
|
|
requires :directory, :key
|
2011-12-29 14:08:58 -05:00
|
|
|
attributes[:body] = nil if options['versionId'] == version
|
|
|
|
connection.delete_object(directory.key, key, options)
|
2009-08-27 20:47:01 -07:00
|
|
|
true
|
2009-09-02 20:17:53 -07:00
|
|
|
end
|
|
|
|
|
2011-01-04 11:01:04 -08:00
|
|
|
remove_method :metadata
|
|
|
|
def metadata
|
2012-10-31 11:05:23 +11:00
|
|
|
attributes.reject {|key, value| !(key.to_s =~ /^x-amz-/)}
|
2011-01-04 11:01:04 -08:00
|
|
|
end
|
|
|
|
|
|
|
|
remove_method :metadata=
|
|
|
|
def metadata=(new_metadata)
|
|
|
|
merge_attributes(new_metadata)
|
|
|
|
end
|
|
|
|
|
2010-10-12 16:22:12 -07:00
|
|
|
remove_method :owner=
|
2009-12-08 11:54:01 -08:00
|
|
|
def owner=(new_owner)
|
|
|
|
if new_owner
|
2010-11-19 13:45:45 -08:00
|
|
|
attributes[:owner] = {
|
2009-12-08 11:54:01 -08:00
|
|
|
:display_name => new_owner['DisplayName'],
|
|
|
|
:id => new_owner['ID']
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-11-18 11:18:46 -08:00
|
|
|
def public=(new_public)
|
|
|
|
if new_public
|
|
|
|
@acl = 'public-read'
|
|
|
|
else
|
|
|
|
@acl = 'private'
|
|
|
|
end
|
|
|
|
new_public
|
|
|
|
end
|
|
|
|
|
2010-11-05 11:37:12 -07:00
|
|
|
def public_url
|
|
|
|
requires :directory, :key
|
2010-11-18 11:18:46 -08:00
|
|
|
if connection.get_object_acl(directory.key, key).body['AccessControlList'].detect {|grant| grant['Grantee']['URI'] == 'http://acs.amazonaws.com/groups/global/AllUsers' && grant['Permission'] == 'READ'}
|
2012-12-05 21:12:25 +01:00
|
|
|
if directory.key.to_s =~ Fog::AWS::COMPLIANT_BUCKET_NAMES
|
2012-08-13 15:13:29 -06:00
|
|
|
"https://#{directory.key}.s3.amazonaws.com/#{Fog::AWS.escape(key)}".gsub('%2F','/')
|
2010-11-05 11:37:12 -07:00
|
|
|
else
|
2012-08-13 15:13:29 -06:00
|
|
|
"https://s3.amazonaws.com/#{directory.key}/#{Fog::AWS.escape(key)}".gsub('%2F','/')
|
2010-11-05 11:37:12 -07:00
|
|
|
end
|
|
|
|
else
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-12-19 21:43:34 -06:00
|
|
|
#
|
|
|
|
# @param [Hash] options
|
2012-12-19 21:53:02 -06:00
|
|
|
# @option options [String] acl sets x-amz-acl HTTP header. Valid values include, private | public-read | public-read-write | authenticated-read | bucket-owner-read | bucket-owner-full-control
|
|
|
|
# @option options [String] cache_controle sets Cache-Control header. For example, 'No-cache'
|
|
|
|
# @option options [String] content_disposition sets Content-Disposition HTTP header. For exampple, 'attachment; filename=testing.txt'
|
|
|
|
# @option options [String] content_encoding sets Content-Encoding HTTP header. For example, 'x-gzip'
|
|
|
|
# @option options [String] content_md5 sets Content-MD5. For example, '79054025255fb1a26e4bc422aef54eb4'
|
|
|
|
# @option options [String] content_type Content-Type. For example, 'text/plain'
|
|
|
|
# @option options [String] expires sets Expires HTTP header. For example, 'Thu, 01 Dec 1994 16:00:00 GMT'
|
|
|
|
# @option options [String] storage_class sets x-amz-storage-class HTTP header. Defaults to 'STANDARD'. Or, 'REDUCED_REDUNDANCY'
|
2012-12-19 21:47:33 -06:00
|
|
|
# @option options [String] encryption sets HTTP encryption header. Set to 'AES256' to encrypt files at rest on S3
|
2012-12-19 21:43:34 -06:00
|
|
|
#
|
2009-08-04 20:09:08 -07:00
|
|
|
def save(options = {})
|
2010-01-14 20:44:39 -08:00
|
|
|
requires :body, :directory, :key
|
2010-10-29 16:38:17 -07:00
|
|
|
if options != {}
|
2011-10-19 14:49:34 -05:00
|
|
|
Fog::Logger.deprecation("options param is deprecated, use acl= instead [light_black](#{caller.first})[/]")
|
2010-10-29 16:38:17 -07:00
|
|
|
end
|
2010-11-08 17:14:21 -08:00
|
|
|
options['x-amz-acl'] ||= @acl if @acl
|
|
|
|
options['Cache-Control'] = cache_control if cache_control
|
|
|
|
options['Content-Disposition'] = content_disposition if content_disposition
|
|
|
|
options['Content-Encoding'] = content_encoding if content_encoding
|
|
|
|
options['Content-MD5'] = content_md5 if content_md5
|
|
|
|
options['Content-Type'] = content_type if content_type
|
|
|
|
options['Expires'] = expires if expires
|
2011-07-10 15:37:15 +09:00
|
|
|
options.merge!(metadata)
|
2010-11-08 17:14:21 -08:00
|
|
|
options['x-amz-storage-class'] = storage_class if storage_class
|
2012-01-14 12:27:58 -08:00
|
|
|
options['x-amz-server-side-encryption'] = encryption if encryption
|
2010-11-08 17:14:21 -08:00
|
|
|
|
2012-03-04 12:56:12 -05:00
|
|
|
if multipart_chunk_size && body.respond_to?(:read)
|
|
|
|
data = multipart_save(options)
|
|
|
|
merge_attributes(data.body)
|
|
|
|
else
|
|
|
|
data = connection.put_object(directory.key, key, body, options)
|
2012-03-26 19:14:02 -05:00
|
|
|
merge_attributes(data.headers.reject {|key, value| ['Content-Length', 'Content-Type'].include?(key)})
|
2012-03-04 12:56:12 -05:00
|
|
|
end
|
|
|
|
self.etag.gsub!('"','')
|
2011-03-18 20:12:55 +00:00
|
|
|
self.content_length = Fog::Storage.get_body_size(body)
|
2012-03-26 19:14:02 -05:00
|
|
|
self.content_type ||= Fog::Storage.get_content_type(body)
|
2009-08-27 20:47:01 -07:00
|
|
|
true
|
2009-08-04 20:09:08 -07:00
|
|
|
end
|
|
|
|
|
2011-12-09 12:42:53 +01:00
|
|
|
def url(expires, options = {})
|
2010-06-12 08:27:27 +08:00
|
|
|
requires :key
|
2012-04-22 19:16:19 -07:00
|
|
|
collection.get_url(key, expires, options)
|
2010-06-12 08:27:27 +08:00
|
|
|
end
|
|
|
|
|
2011-12-29 16:19:39 -05:00
|
|
|
def versions
|
|
|
|
@versions ||= begin
|
|
|
|
Fog::Storage::AWS::Versions.new(
|
|
|
|
:file => self,
|
|
|
|
:connection => connection
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-08-04 20:09:08 -07:00
|
|
|
private
|
|
|
|
|
2010-01-14 20:44:39 -08:00
|
|
|
def directory=(new_directory)
|
|
|
|
@directory = new_directory
|
2009-08-04 20:09:08 -07:00
|
|
|
end
|
|
|
|
|
2012-03-04 12:56:12 -05:00
|
|
|
def multipart_save(options)
|
|
|
|
# Initiate the upload
|
|
|
|
res = connection.initiate_multipart_upload(directory.key, key, options)
|
|
|
|
upload_id = res.body["UploadId"]
|
|
|
|
|
|
|
|
# Store ETags of upload parts
|
|
|
|
part_tags = []
|
|
|
|
|
|
|
|
# Upload each part
|
|
|
|
# TODO: optionally upload chunks in parallel using threads
|
|
|
|
# (may cause network performance problems with many small chunks)
|
|
|
|
# TODO: Support large chunk sizes without reading the chunk into memory
|
|
|
|
body.rewind if body.respond_to?(:rewind)
|
|
|
|
while (chunk = body.read(multipart_chunk_size)) do
|
2012-09-09 14:39:11 +01:00
|
|
|
md5 = Base64.encode64(Digest::MD5.digest(chunk)).strip
|
|
|
|
part_upload = connection.upload_part(directory.key, key, upload_id, part_tags.size + 1, chunk, 'Content-MD5' => md5 )
|
2012-03-04 12:56:12 -05:00
|
|
|
part_tags << part_upload.headers["ETag"]
|
|
|
|
end
|
|
|
|
|
2012-03-04 19:38:19 -05:00
|
|
|
rescue
|
|
|
|
# Abort the upload & reraise
|
|
|
|
connection.abort_multipart_upload(directory.key, key, upload_id) if upload_id
|
|
|
|
raise
|
|
|
|
else
|
2012-03-04 12:56:12 -05:00
|
|
|
# Complete the upload
|
|
|
|
connection.complete_multipart_upload(directory.key, key, upload_id, part_tags)
|
|
|
|
end
|
|
|
|
|
2009-08-02 16:37:54 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|