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
|
|
|
|
|
|
|
# Delete an object from S3
|
|
|
|
#
|
|
|
|
# ==== Parameters
|
|
|
|
# * bucket_name<~String> - Name of bucket containing object to delete
|
|
|
|
# * object_name<~String> - Name of object to delete
|
|
|
|
#
|
|
|
|
# ==== Returns
|
2009-11-02 21:48:49 -05:00
|
|
|
# * response<~Excon::Response>:
|
2009-08-08 15:31:32 -04:00
|
|
|
# * status<~Integer> - 204
|
2010-10-29 21:05:59 -04:00
|
|
|
#
|
|
|
|
# ==== See Also
|
|
|
|
# http://docs.amazonwebservices.com/AmazonS3/latest/API/RESTObjectDELETE.html
|
|
|
|
|
2011-02-23 14:16:22 -05:00
|
|
|
def delete_object(bucket_name, object_name, options = {})
|
2011-12-20 11:32:52 -05:00
|
|
|
if version_id = options.delete('versionId')
|
|
|
|
path = "#{CGI.escape(object_name)}?versionId=#{CGI.escape(version_id)}"
|
|
|
|
else
|
|
|
|
path = CGI.escape(object_name)
|
|
|
|
end
|
|
|
|
|
2011-02-23 14:16:22 -05:00
|
|
|
headers = options
|
2011-06-15 17:26:43 -04:00
|
|
|
request({
|
2009-12-04 00:39:04 -05:00
|
|
|
:expects => 204,
|
2011-02-23 14:16:22 -05:00
|
|
|
:headers => headers,
|
2009-12-04 00:39:04 -05:00
|
|
|
:host => "#{bucket_name}.#{@host}",
|
|
|
|
:idempotent => true,
|
|
|
|
:method => 'DELETE',
|
2011-12-20 11:32:52 -05:00
|
|
|
:path => path
|
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
|
|
|
|
2011-02-23 14:16:22 -05:00
|
|
|
def delete_object(bucket_name, object_name, options = {})
|
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 = 204
|
2011-12-15 14:26:08 -05:00
|
|
|
|
2011-12-20 11:32:52 -05:00
|
|
|
version_id = options.delete('versionId')
|
2011-12-15 14:26:08 -05:00
|
|
|
|
2011-12-20 11:32:52 -05:00
|
|
|
if bucket[:versioning]
|
2011-12-15 14:26:08 -05:00
|
|
|
bucket[:objects][object_name] ||= []
|
2011-12-20 11:32:52 -05:00
|
|
|
|
|
|
|
if version_id
|
|
|
|
version = bucket[:objects][object_name].find { |object| object['VersionId'] == version_id}
|
|
|
|
|
|
|
|
# S3 special cases the 'null' value to not error out if no such version exists.
|
|
|
|
if version || version_id == 'null'
|
|
|
|
bucket[:objects][object_name].delete(version)
|
|
|
|
|
|
|
|
response.headers['x-amz-delete-marker'] = 'true' if version[:delete_marker]
|
|
|
|
response.headers['x-amz-version-id'] = version_id
|
|
|
|
else
|
|
|
|
response.status = 400
|
|
|
|
response.body = invalid_version_id_payload(version_id)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
delete_marker = {
|
|
|
|
:delete_marker => true,
|
|
|
|
'Key' => object_name,
|
|
|
|
'VersionId' => Fog::Mock.random_base64(32),
|
|
|
|
'Last-Modified' => Fog::Time.now.to_date_header
|
|
|
|
}
|
|
|
|
|
|
|
|
bucket[:objects][object_name] << delete_marker
|
|
|
|
response.headers['x-amz-delete-marker'] = 'true'
|
|
|
|
response.headers['x-amz-version-id'] = delete_marker['VersionId']
|
|
|
|
end
|
2011-12-15 14:26:08 -05:00
|
|
|
else
|
2011-12-20 11:32:52 -05:00
|
|
|
if version_id && version_id != 'null'
|
|
|
|
response.status = 400
|
|
|
|
response.body = invalid_version_id_payload(version_id)
|
|
|
|
else
|
|
|
|
bucket[:objects].delete(object_name)
|
|
|
|
|
|
|
|
response.headers['x-amz-version-id'] = 'null'
|
|
|
|
end
|
2011-12-15 14:26:08 -05:00
|
|
|
end
|
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 => 204}, response))
|
2009-08-08 15:31:32 -04:00
|
|
|
end
|
|
|
|
response
|
|
|
|
end
|
|
|
|
|
2011-12-20 11:32:52 -05:00
|
|
|
private
|
|
|
|
|
|
|
|
def invalid_version_id_payload(version_id)
|
|
|
|
{
|
|
|
|
'Error' => {
|
|
|
|
'Code' => 'InvalidArgument',
|
|
|
|
'Message' => 'Invalid version id specified',
|
|
|
|
'ArgumentValue' => version_id,
|
|
|
|
'ArgumentName' => 'versionId',
|
|
|
|
'RequestId' => Fog::Mock.random_hex(16),
|
|
|
|
'HostId' => Fog::Mock.random_base64(65)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2009-08-08 15:31:32 -04:00
|
|
|
end
|
2009-07-13 22:14:59 -04:00
|
|
|
end
|
|
|
|
end
|
2010-09-03 04:11:45 -04:00
|
|
|
end
|