2010-03-13 16:37:24 -05:00
|
|
|
module Fog
|
2011-06-15 17:26:43 -04:00
|
|
|
module Storage
|
|
|
|
class AWS
|
2010-03-13 16:37:24 -05:00
|
|
|
class Real
|
2009-08-08 15:31:32 -04:00
|
|
|
|
|
|
|
# Create an object in an S3 bucket
|
|
|
|
#
|
|
|
|
# ==== Parameters
|
|
|
|
# * bucket_name<~String> - Name of bucket to create object in
|
|
|
|
# * object_name<~String> - Name of object to create
|
2010-11-15 20:17:37 -05:00
|
|
|
# * data<~File||String> - File or String to create object from
|
2009-08-08 15:31:32 -04:00
|
|
|
# * options<~Hash>:
|
|
|
|
# * 'Cache-Control'<~String> - Caching behaviour
|
|
|
|
# * 'Content-Disposition'<~String> - Presentational information for the object
|
|
|
|
# * 'Content-Encoding'<~String> - Encoding of object data
|
|
|
|
# * 'Content-Length'<~String> - Size of object in bytes (defaults to object.read.length)
|
2010-11-15 20:17:37 -05:00
|
|
|
# * 'Content-MD5'<~String> - Base64 encoded 128-bit MD5 digest of message
|
2009-08-08 15:31:32 -04:00
|
|
|
# * 'Content-Type'<~String> - Standard MIME type describing contents (defaults to MIME::Types.of.first)
|
2010-11-17 17:29:33 -05:00
|
|
|
# * 'Expires'<~String> - Cache expiry
|
2009-08-08 15:31:32 -04:00
|
|
|
# * 'x-amz-acl'<~String> - Permissions, must be in ['private', 'public-read', 'public-read-write', 'authenticated-read']
|
2011-02-02 01:57:19 -05:00
|
|
|
# * 'x-amz-storage-class'<~String> - Default is 'STANDARD', set to 'REDUCED_REDUNDANCY' for non-critical, reproducable data
|
2009-08-08 15:31:32 -04:00
|
|
|
# * "x-amz-meta-#{name}" - Headers to be returned with object, note total size of request without body must be less than 8 KB.
|
|
|
|
#
|
|
|
|
# ==== Returns
|
2009-11-02 21:48:49 -05:00
|
|
|
# * response<~Excon::Response>:
|
2009-08-08 15:31:32 -04:00
|
|
|
# * headers<~Hash>:
|
|
|
|
# * 'ETag'<~String> - etag of new object
|
2010-10-29 21:05:59 -04:00
|
|
|
#
|
|
|
|
# ==== See Also
|
|
|
|
# http://docs.amazonwebservices.com/AmazonS3/latest/API/RESTObjectPUT.html
|
2010-11-15 20:17:37 -05:00
|
|
|
#
|
2009-09-08 23:33:37 -04:00
|
|
|
def put_object(bucket_name, object_name, data, options = {})
|
2011-03-10 16:04:42 -05:00
|
|
|
data = Fog::Storage.parse_data(data)
|
2009-09-08 23:33:37 -04:00
|
|
|
headers = data[:headers].merge!(options)
|
2012-03-26 20:14:02 -04:00
|
|
|
request({
|
|
|
|
:body => data[:body],
|
|
|
|
:expects => 200,
|
2009-12-04 00:39:04 -05:00
|
|
|
:headers => headers,
|
|
|
|
:host => "#{bucket_name}.#{@host}",
|
|
|
|
:idempotent => true,
|
|
|
|
:method => 'PUT',
|
|
|
|
:path => CGI.escape(object_name)
|
2009-08-08 15:31:32 -04:00
|
|
|
})
|
|
|
|
end
|
|
|
|
|
2009-07-13 22:14:59 -04:00
|
|
|
end
|
|
|
|
|
2010-10-29 21:16:36 -04:00
|
|
|
class Mock # :nodoc:all
|
2009-08-08 15:31:32 -04:00
|
|
|
|
2009-09-08 23:33:37 -04:00
|
|
|
def put_object(bucket_name, object_name, data, options = {})
|
2011-01-21 23:45:37 -05:00
|
|
|
acl = options['x-amz-acl'] || 'private'
|
|
|
|
if !['private', 'public-read', 'public-read-write', 'authenticated-read'].include?(acl)
|
|
|
|
raise Excon::Errors::BadRequest.new('invalid x-amz-acl')
|
|
|
|
else
|
2011-05-19 18:35:33 -04:00
|
|
|
self.data[:acls][:object][bucket_name] ||= {}
|
|
|
|
self.data[:acls][:object][bucket_name][object_name] = self.class.acls(acl)
|
2010-11-18 17:17:11 -05:00
|
|
|
end
|
2011-01-21 23:45:37 -05:00
|
|
|
|
2011-03-10 16:04:42 -05:00
|
|
|
data = Fog::Storage.parse_data(data)
|
2010-05-25 13:37:14 -04:00
|
|
|
unless data[:body].is_a?(String)
|
|
|
|
data[:body] = data[:body].read
|
|
|
|
end
|
2009-11-20 14:08:08 -05:00
|
|
|
response = Excon::Response.new
|
2011-05-19 18:35:33 -04:00
|
|
|
if (bucket = self.data[:buckets][bucket_name])
|
2009-08-08 15:31:32 -04:00
|
|
|
response.status = 200
|
2010-11-10 15:46:50 -05:00
|
|
|
object = {
|
2011-02-02 19:27:22 -05:00
|
|
|
:body => data[:body],
|
|
|
|
'Content-Type' => options['Content-Type'] || data[:headers]['Content-Type'],
|
2012-02-28 14:25:02 -05:00
|
|
|
'ETag' => Digest::MD5.hexdigest(data[:body]),
|
2011-02-02 19:27:22 -05:00
|
|
|
'Key' => object_name,
|
|
|
|
'Last-Modified' => Fog::Time.now.to_date_header,
|
|
|
|
'Content-Length' => options['Content-Length'] || data[:headers]['Content-Length'],
|
2011-12-07 10:32:08 -05:00
|
|
|
'StorageClass' => options['x-amz-storage-class'] || 'STANDARD',
|
|
|
|
'VersionId' => bucket[:versioning] == 'Enabled' ? Fog::Mock.random_base64(32) : 'null'
|
2009-08-08 15:31:32 -04:00
|
|
|
}
|
2011-02-02 18:50:47 -05:00
|
|
|
|
|
|
|
for key, value in options
|
|
|
|
case key
|
|
|
|
when 'Cache-Control', 'Content-Disposition', 'Content-Encoding', 'Content-MD5', 'Expires', /^x-amz-meta-/
|
|
|
|
object[key] = value
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-12-15 14:26:08 -05:00
|
|
|
if bucket[:versioning]
|
|
|
|
bucket[:objects][object_name] ||= []
|
2011-12-20 15:59:23 -05:00
|
|
|
|
|
|
|
# When versioning is suspended, putting an object will create a new 'null' version if the latest version
|
|
|
|
# is a value other than 'null', otherwise it will replace the latest version.
|
2012-02-06 09:53:27 -05:00
|
|
|
if bucket[:versioning] == 'Suspended' && bucket[:objects][object_name].first['VersionId'] == 'null'
|
|
|
|
bucket[:objects][object_name].shift
|
2011-12-20 15:59:23 -05:00
|
|
|
end
|
|
|
|
|
2012-02-06 09:53:27 -05:00
|
|
|
bucket[:objects][object_name].unshift(object)
|
2011-12-15 14:26:08 -05:00
|
|
|
else
|
|
|
|
bucket[:objects][object_name] = [object]
|
|
|
|
end
|
|
|
|
|
2010-11-10 15:46:50 -05:00
|
|
|
response.headers = {
|
2012-01-19 18:19:03 -05:00
|
|
|
'Content-Length' => object['Content-Length'],
|
|
|
|
'Content-Type' => object['Content-Type'],
|
|
|
|
'ETag' => object['ETag'],
|
|
|
|
'Last-Modified' => object['Last-Modified'],
|
2010-11-10 15:46:50 -05:00
|
|
|
}
|
2012-01-19 18:19:03 -05:00
|
|
|
|
|
|
|
response.headers['x-amz-version-id'] = object['VersionId'] if object['VersionId'] != 'null'
|
2009-08-08 15:31:32 -04:00
|
|
|
else
|
2009-08-16 14:45:52 -04:00
|
|
|
response.status = 404
|
2009-11-20 14:08:08 -05:00
|
|
|
raise(Excon::Errors.status_error({:expects => 200}, response))
|
2009-08-08 15:31:32 -04:00
|
|
|
end
|
|
|
|
response
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
2009-07-13 22:14:59 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|