2010-09-18 13:40:48 -04:00
|
|
|
module Fog
|
|
|
|
module Google
|
|
|
|
class Storage
|
|
|
|
class Real
|
|
|
|
|
2010-09-24 09:43:34 -04:00
|
|
|
# Create an object in an Google Storage bucket
|
2010-09-18 13:40:48 -04:00
|
|
|
#
|
|
|
|
# ==== Parameters
|
|
|
|
# * bucket_name<~String> - Name of bucket to create object in
|
|
|
|
# * object_name<~String> - Name of object to create
|
|
|
|
# * data<~File> - File or String to create object from
|
|
|
|
# * 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)
|
|
|
|
# * 'Content-MD5'<~String> - Base64 encoded 128-bit MD5 digest of message (defaults to Base64 encoded MD5 of object.read)
|
|
|
|
# * 'Content-Type'<~String> - Standard MIME type describing contents (defaults to MIME::Types.of.first)
|
2010-09-18 15:26:48 -04:00
|
|
|
# * 'x-goog-acl'<~String> - Permissions, must be in ['private', 'public-read', 'public-read-write', 'authenticated-read']
|
|
|
|
# * "x-goog-meta-#{name}" - Headers to be returned with object, note total size of request without body must be less than 8 KB.
|
2010-09-18 13:40:48 -04:00
|
|
|
#
|
|
|
|
# ==== Returns
|
|
|
|
# * response<~Excon::Response>:
|
|
|
|
# * headers<~Hash>:
|
|
|
|
# * 'ETag'<~String> - etag of new object
|
|
|
|
def put_object(bucket_name, object_name, data, options = {})
|
2011-03-10 16:04:42 -05:00
|
|
|
data = Fog::Storage.parse_data(data)
|
2010-09-18 13:40:48 -04:00
|
|
|
headers = data[:headers].merge!(options)
|
|
|
|
request({
|
|
|
|
:body => data[:body],
|
|
|
|
:expects => 200,
|
|
|
|
:headers => headers,
|
|
|
|
:host => "#{bucket_name}.#{@host}",
|
|
|
|
:idempotent => true,
|
|
|
|
:method => 'PUT',
|
|
|
|
:path => CGI.escape(object_name)
|
|
|
|
})
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
class Mock
|
|
|
|
|
|
|
|
def put_object(bucket_name, object_name, data, options = {})
|
2011-01-21 23:45:37 -05:00
|
|
|
acl = options['x-goog-acl'] || 'private'
|
|
|
|
if !['private', 'public-read', 'public-read-write', 'authenticated-read'].include?(acl)
|
|
|
|
raise Excon::Errors::BadRequest.new('invalid x-goog-acl')
|
|
|
|
else
|
|
|
|
@data[:acls][:object][bucket_name] ||= {}
|
|
|
|
@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-09-18 13:40:48 -04:00
|
|
|
unless data[:body].is_a?(String)
|
|
|
|
data[:body] = data[:body].read
|
|
|
|
end
|
|
|
|
response = Excon::Response.new
|
|
|
|
if (bucket = @data[:buckets][bucket_name])
|
|
|
|
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'],
|
|
|
|
'ETag' => Fog::Google::Mock.etag,
|
|
|
|
'Key' => object_name,
|
|
|
|
'Last-Modified' => Fog::Time.now.to_date_header,
|
|
|
|
'Content-Length' => options['Content-Length'] || data[:headers]['Content-Length'],
|
|
|
|
'StorageClass' => 'STANDARD'
|
2010-09-18 13:40:48 -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-goog-meta-/
|
|
|
|
object[key] = value
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-11-10 15:46:50 -05:00
|
|
|
bucket[:objects][object_name] = object
|
|
|
|
response.headers = {
|
2011-02-02 19:27:22 -05:00
|
|
|
'Content-Length' => object['Content-Length'],
|
2010-11-10 15:46:50 -05:00
|
|
|
'Content-Type' => object['Content-Type'],
|
|
|
|
'ETag' => object['ETag'],
|
2011-02-02 19:27:22 -05:00
|
|
|
'Last-Modified' => object['Last-Modified']
|
2010-11-10 15:46:50 -05:00
|
|
|
}
|
2010-09-18 13:40:48 -04:00
|
|
|
else
|
|
|
|
response.status = 404
|
|
|
|
raise(Excon::Errors.status_error({:expects => 200}, response))
|
|
|
|
end
|
|
|
|
response
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|