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
|
2012-12-26 23:32:23 -05:00
|
|
|
# @see AWS Object docs http://docs.aws.amazon.com/AmazonS3/latest/API/RESTObjectOps.html
|
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-12-26 23:28:53 -05:00
|
|
|
# @note Chunk size to use for multipart uploads.
|
|
|
|
# Use small chunk sizes to minimize memory. E.g. 5242880 = 5mb
|
2012-03-04 12:56:12 -05:00
|
|
|
attr_accessor :multipart_chunk_size
|
|
|
|
|
2012-12-26 23:31:35 -05:00
|
|
|
|
|
|
|
# Set file's access control list (ACL).
|
|
|
|
#
|
|
|
|
# valid acls: private, public-read, public-read-write, authenticated-read
|
|
|
|
#
|
|
|
|
# @param [String] new_acl one of valid options
|
|
|
|
# @return [String] @acl
|
|
|
|
#
|
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
|
|
|
|
|
2012-12-26 23:31:35 -05:00
|
|
|
|
|
|
|
# Get file's body if exists, else ' '.
|
|
|
|
#
|
|
|
|
# @return [File]
|
|
|
|
#
|
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
|
|
|
|
|
2012-12-26 23:31:35 -05:00
|
|
|
|
|
|
|
# Set body attribute.
|
|
|
|
#
|
|
|
|
# @param [File] new_body
|
|
|
|
# @return [File] attributes[:body]
|
|
|
|
#
|
2010-11-19 16:45:45 -05:00
|
|
|
def body=(new_body)
|
|
|
|
attributes[:body] = new_body
|
|
|
|
end
|
|
|
|
|
2012-12-26 23:31:35 -05:00
|
|
|
|
|
|
|
# Get the file instance's directory.
|
|
|
|
#
|
|
|
|
# @return [Fog::AWS::Storage::Directory]
|
|
|
|
#
|
2010-01-14 23:44:39 -05:00
|
|
|
def directory
|
|
|
|
@directory
|
2009-08-27 23:47:01 -04:00
|
|
|
end
|
|
|
|
|
2012-12-26 23:31:35 -05:00
|
|
|
|
|
|
|
# Copy object from one bucket to other bucket.
|
|
|
|
#
|
|
|
|
# required attributes: directory, key
|
|
|
|
#
|
|
|
|
# @param target_directory_key [String]
|
|
|
|
# @param target_file_key [String]
|
|
|
|
# @param options [Hash] options for copy_object method
|
|
|
|
# @return [String] Fog::AWS::Files#head status of directory contents
|
|
|
|
#
|
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
|
2012-12-22 18:30:12 -05:00
|
|
|
service.copy_object(directory.key, key, target_directory_key, target_file_key, options)
|
|
|
|
target_directory = service.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
|
|
|
|
|
2012-12-26 23:31:35 -05:00
|
|
|
|
|
|
|
# Destroy file via http DELETE.
|
|
|
|
#
|
|
|
|
# required attributes: directory, key
|
|
|
|
#
|
|
|
|
# @param options [Hash]
|
2012-12-30 21:43:16 -05:00
|
|
|
# @option options versionId []
|
2012-12-26 23:31:35 -05:00
|
|
|
# @return [Boolean] true if successful
|
|
|
|
#
|
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
|
2012-12-22 18:30:12 -05:00
|
|
|
service.delete_object(directory.key, key, options)
|
2009-08-27 23:47:01 -04:00
|
|
|
true
|
2009-09-02 23:17:53 -04:00
|
|
|
end
|
|
|
|
|
2012-12-26 23:32:57 -05:00
|
|
|
|
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
|
|
|
|
|
2012-12-26 23:32:57 -05:00
|
|
|
|
2011-01-04 14:01:04 -05:00
|
|
|
remove_method :metadata=
|
|
|
|
def metadata=(new_metadata)
|
|
|
|
merge_attributes(new_metadata)
|
|
|
|
end
|
|
|
|
|
2012-12-26 23:32:57 -05:00
|
|
|
|
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
|
|
|
|
|
2012-12-26 23:31:35 -05:00
|
|
|
|
|
|
|
# Set Access-Control-List permissions.
|
|
|
|
#
|
|
|
|
# valid new_publics: public_read, private
|
|
|
|
#
|
|
|
|
# @param [String] new_public
|
|
|
|
# @return [String] new_puplic
|
|
|
|
#
|
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
|
|
|
|
|
2012-12-26 23:31:35 -05:00
|
|
|
|
|
|
|
# Get pubically acessible url via http GET.
|
|
|
|
# Checks persmissions before creating.
|
|
|
|
# Defaults to s3 subdomain or compliant bucket name
|
|
|
|
#
|
|
|
|
# required attributes: directory, key
|
|
|
|
#
|
|
|
|
# @return [String] public url
|
|
|
|
#
|
2010-11-05 14:37:12 -04:00
|
|
|
def public_url
|
|
|
|
requires :directory, :key
|
2012-12-22 18:30:12 -05:00
|
|
|
if service.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 15:12:25 -05:00
|
|
|
if directory.key.to_s =~ Fog::AWS::COMPLIANT_BUCKET_NAMES
|
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-26 23:29:44 -05:00
|
|
|
# Save file with body as contents to directory.key with name key via http PUT
|
|
|
|
#
|
|
|
|
# required attributes: body, directory, key
|
2012-12-19 22:43:34 -05:00
|
|
|
#
|
|
|
|
# @param [Hash] options
|
2012-12-19 22:53:02 -05: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'
|
2012-12-26 23:28:25 -05:00
|
|
|
# @option options [String] expires sets number of seconds before AWS Object expires.
|
2012-12-19 22:53:02 -05:00
|
|
|
# @option options [String] storage_class sets x-amz-storage-class HTTP header. Defaults to 'STANDARD'. Or, 'REDUCED_REDUNDANCY'
|
2012-12-19 22:47:33 -05:00
|
|
|
# @option options [String] encryption sets HTTP encryption header. Set to 'AES256' to encrypt files at rest on S3
|
2012-12-26 23:29:44 -05:00
|
|
|
# @return [Boolean] true if no errors
|
2012-12-19 22:43:34 -05:00
|
|
|
#
|
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
|
2012-12-22 18:30:12 -05:00
|
|
|
data = service.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
|
|
|
|
|
2012-12-26 23:31:35 -05:00
|
|
|
|
|
|
|
# Get a url for file.
|
|
|
|
#
|
|
|
|
# required attributes: key
|
|
|
|
#
|
2013-03-07 10:22:37 -05:00
|
|
|
# @param expires [String] number of seconds (since 1970-01-01 00:00) before url expires
|
2012-12-26 23:31:35 -05:00
|
|
|
# @param options [Hash]
|
|
|
|
# @return [String] url
|
|
|
|
#
|
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
|
|
|
|
|
2012-12-26 23:31:35 -05:00
|
|
|
|
|
|
|
# File version if exists or creates new version.
|
|
|
|
# @return [Fog::Storage::AWS::Version]
|
|
|
|
#
|
2011-12-29 16:19:39 -05:00
|
|
|
def versions
|
|
|
|
@versions ||= begin
|
|
|
|
Fog::Storage::AWS::Versions.new(
|
|
|
|
:file => self,
|
2012-12-22 18:30:12 -05:00
|
|
|
:service => service
|
2011-12-29 16:19:39 -05:00
|
|
|
)
|
|
|
|
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
|
2012-12-22 18:30:12 -05:00
|
|
|
res = service.initiate_multipart_upload(directory.key, key, options)
|
2012-03-04 12:56:12 -05:00
|
|
|
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
|
2012-12-22 18:30:12 -05:00
|
|
|
part_upload = service.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
|
2012-12-22 18:30:12 -05:00
|
|
|
service.abort_multipart_upload(directory.key, key, upload_id) if upload_id
|
2012-03-04 19:38:19 -05:00
|
|
|
raise
|
|
|
|
else
|
2012-03-04 12:56:12 -05:00
|
|
|
# Complete the upload
|
2012-12-22 18:30:12 -05:00
|
|
|
service.complete_multipart_upload(directory.key, key, upload_id, part_tags)
|
2012-03-04 12:56:12 -05:00
|
|
|
end
|
|
|
|
|
2009-08-02 19:37:54 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|