2010-10-04 17:02:08 -04:00
|
|
|
require 'fog/core/model'
|
2011-12-29 16:19:39 -05:00
|
|
|
require 'fog/aws/models/storage/versions'
|
2010-03-13 16:37:24 -05:00
|
|
|
|
2009-08-02 19:37:54 -04:00
|
|
|
module Fog
|
2011-06-15 17:26:43 -04:00
|
|
|
module Storage
|
|
|
|
class AWS
|
2009-08-02 19:37:54 -04:00
|
|
|
|
2010-01-14 23:44:39 -05:00
|
|
|
class File < Fog::Model
|
2009-08-02 19:37:54 -04:00
|
|
|
|
2010-09-07 14:30:02 -04:00
|
|
|
identity :key, :aliases => 'Key'
|
2009-10-23 13:27:23 -04:00
|
|
|
|
2010-10-12 21:07:36 -04:00
|
|
|
attr_writer :body
|
2010-11-08 20:14:21 -05:00
|
|
|
attribute :cache_control, :aliases => 'Cache-Control'
|
|
|
|
attribute :content_disposition, :aliases => 'Content-Disposition'
|
|
|
|
attribute :content_encoding, :aliases => 'Content-Encoding'
|
2011-01-26 18:56:50 -05:00
|
|
|
attribute :content_length, :aliases => ['Content-Length', 'Size'], :type => :integer
|
2010-11-08 20:14:21 -05: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 15:54:22 -05:00
|
|
|
attribute :last_modified, :aliases => ['Last-Modified', 'LastModified']
|
2011-01-04 14:01:04 -05:00
|
|
|
attribute :metadata
|
2010-11-08 20:14:21 -05:00
|
|
|
attribute :owner, :aliases => 'Owner'
|
|
|
|
attribute :storage_class, :aliases => ['x-amz-storage-class', 'StorageClass']
|
2012-01-14 15:27:58 -05: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 19:37:54 -04: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 12:38:55 -04: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 14:30:23 -05:00
|
|
|
def body
|
2010-11-19 16:45:45 -05:00
|
|
|
attributes[:body] ||= if last_modified && (file = collection.get(identity))
|
2010-03-31 00:09:35 -04:00
|
|
|
file.body
|
2010-02-19 14:30:23 -05:00
|
|
|
else
|
|
|
|
''
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-11-19 16:45:45 -05:00
|
|
|
def body=(new_body)
|
|
|
|
attributes[:body] = new_body
|
|
|
|
end
|
|
|
|
|
2010-01-14 23:44:39 -05:00
|
|
|
def directory
|
|
|
|
@directory
|
2009-08-27 23:47:01 -04:00
|
|
|
end
|
|
|
|
|
2011-09-04 09:41:42 -04:00
|
|
|
def copy(target_directory_key, target_file_key, options = {})
|
2010-01-14 23:44:39 -05: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 18:02:08 -04:00
|
|
|
target_directory = connection.directories.new(:key => target_directory_key)
|
2011-10-27 13:49:10 -04:00
|
|
|
target_directory.files.head(target_file_key)
|
2009-08-04 23:09:08 -04:00
|
|
|
end
|
|
|
|
|
2011-12-29 14:08:58 -05:00
|
|
|
def destroy(options = {})
|
2010-01-14 23:44:39 -05: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 23:47:01 -04:00
|
|
|
true
|
2009-09-02 23:17:53 -04:00
|
|
|
end
|
|
|
|
|
2011-01-04 14:01:04 -05:00
|
|
|
remove_method :metadata
|
|
|
|
def metadata
|
2012-10-30 20:05:23 -04:00
|
|
|
attributes.reject {|key, value| !(key.to_s =~ /^x-amz-/)}
|
2011-01-04 14:01:04 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
remove_method :metadata=
|
|
|
|
def metadata=(new_metadata)
|
|
|
|
merge_attributes(new_metadata)
|
|
|
|
end
|
|
|
|
|
2010-10-12 19:22:12 -04:00
|
|
|
remove_method :owner=
|
2009-12-08 14:54:01 -05:00
|
|
|
def owner=(new_owner)
|
|
|
|
if new_owner
|
2010-11-19 16:45:45 -05:00
|
|
|
attributes[:owner] = {
|
2009-12-08 14:54:01 -05:00
|
|
|
:display_name => new_owner['DisplayName'],
|
|
|
|
:id => new_owner['ID']
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-11-18 14:18:46 -05:00
|
|
|
def public=(new_public)
|
|
|
|
if new_public
|
|
|
|
@acl = 'public-read'
|
|
|
|
else
|
|
|
|
@acl = 'private'
|
|
|
|
end
|
|
|
|
new_public
|
|
|
|
end
|
|
|
|
|
2010-11-05 14:37:12 -04:00
|
|
|
def public_url
|
|
|
|
requires :directory, :key
|
2010-11-18 14:18:46 -05: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-02-17 18:07:18 -05:00
|
|
|
if directory.key.to_s =~ /^(?:[a-z]|\d(?!\d{0,2}(?:\.\d{1,3}){3}$))(?:[a-z0-9]|\-(?![\.])){1,61}[a-z0-9]$/
|
2012-08-13 17:13:29 -04:00
|
|
|
"https://#{directory.key}.s3.amazonaws.com/#{Fog::AWS.escape(key)}".gsub('%2F','/')
|
2010-11-05 14:37:12 -04:00
|
|
|
else
|
2012-08-13 17:13:29 -04:00
|
|
|
"https://s3.amazonaws.com/#{directory.key}/#{Fog::AWS.escape(key)}".gsub('%2F','/')
|
2010-11-05 14:37:12 -04:00
|
|
|
end
|
|
|
|
else
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-12-19 22:43:34 -05:00
|
|
|
#
|
|
|
|
# @param [Hash] options
|
|
|
|
#
|
2009-08-04 23:09:08 -04:00
|
|
|
def save(options = {})
|
2010-01-14 23:44:39 -05:00
|
|
|
requires :body, :directory, :key
|
2010-10-29 19:38:17 -04:00
|
|
|
if options != {}
|
2011-10-19 15:49:34 -04:00
|
|
|
Fog::Logger.deprecation("options param is deprecated, use acl= instead [light_black](#{caller.first})[/]")
|
2010-10-29 19:38:17 -04:00
|
|
|
end
|
2010-11-08 20:14:21 -05: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 02:37:15 -04:00
|
|
|
options.merge!(metadata)
|
2010-11-08 20:14:21 -05:00
|
|
|
options['x-amz-storage-class'] = storage_class if storage_class
|
2012-01-14 15:27:58 -05:00
|
|
|
options['x-amz-server-side-encryption'] = encryption if encryption
|
2010-11-08 20:14:21 -05: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 20:14:02 -04: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 16:12:55 -04:00
|
|
|
self.content_length = Fog::Storage.get_body_size(body)
|
2012-03-26 20:14:02 -04:00
|
|
|
self.content_type ||= Fog::Storage.get_content_type(body)
|
2009-08-27 23:47:01 -04:00
|
|
|
true
|
2009-08-04 23:09:08 -04:00
|
|
|
end
|
|
|
|
|
2011-12-09 06:42:53 -05:00
|
|
|
def url(expires, options = {})
|
2010-06-11 20:27:27 -04:00
|
|
|
requires :key
|
2012-04-22 22:16:19 -04:00
|
|
|
collection.get_url(key, expires, options)
|
2010-06-11 20:27:27 -04: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 23:09:08 -04:00
|
|
|
private
|
|
|
|
|
2010-01-14 23:44:39 -05:00
|
|
|
def directory=(new_directory)
|
|
|
|
@directory = new_directory
|
2009-08-04 23:09:08 -04: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 09:39:11 -04: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 19:37:54 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|