2010-03-13 16:37:24 -05:00
|
|
|
module Fog
|
|
|
|
module AWS
|
|
|
|
module S3
|
|
|
|
class Real
|
2009-08-08 15:31:32 -04:00
|
|
|
|
|
|
|
# Get an object from S3
|
|
|
|
#
|
|
|
|
# ==== Parameters
|
|
|
|
# * bucket_name<~String> - Name of bucket to read from
|
|
|
|
# * object_name<~String> - Name of object to read
|
|
|
|
# * options<~Hash>:
|
|
|
|
# * 'If-Match'<~String> - Returns object only if its etag matches this value, otherwise returns 412 (Precondition Failed).
|
|
|
|
# * 'If-Modified-Since'<~Time> - Returns object only if it has been modified since this time, otherwise returns 304 (Not Modified).
|
|
|
|
# * 'If-None-Match'<~String> - Returns object only if its etag differs from this value, otherwise returns 304 (Not Modified)
|
|
|
|
# * 'If-Unmodified-Since'<~Time> - Returns object only if it has not been modified since this time, otherwise returns 412 (Precodition Failed).
|
|
|
|
# * 'Range'<~String> - Range of object to download
|
|
|
|
# ==== Returns
|
2009-11-02 21:48:49 -05:00
|
|
|
# * response<~Excon::Response>:
|
2009-08-08 15:31:32 -04:00
|
|
|
# * body<~String> - Contents of object
|
|
|
|
# * headers<~Hash>:
|
|
|
|
# * 'Content-Length'<~String> - Size of object contents
|
|
|
|
# * 'Content-Type'<~String> - MIME type of object
|
|
|
|
# * 'ETag'<~String> - Etag of object
|
|
|
|
# * 'Last-Modified'<~String> - Last modified timestamp for object
|
2009-09-09 23:44:28 -04:00
|
|
|
def get_object(bucket_name, object_name, options = {}, &block)
|
2009-09-08 23:24:21 -04:00
|
|
|
unless bucket_name
|
|
|
|
raise ArgumentError.new('bucket_name is required')
|
|
|
|
end
|
|
|
|
unless object_name
|
|
|
|
raise ArgumentError.new('object_name is required')
|
|
|
|
end
|
2009-08-08 15:31:32 -04:00
|
|
|
headers = {}
|
|
|
|
headers['If-Modified-Since'] = options['If-Modified-Since'].utc.strftime("%a, %d %b %Y %H:%M:%S +0000") if options['If-Modified-Since']
|
|
|
|
headers['If-Unmodified-Since'] = options['If-Unmodified-Since'].utc.strftime("%a, %d %b %Y %H:%M:%S +0000") if options['If-Modified-Since']
|
|
|
|
headers.merge!(options)
|
|
|
|
request({
|
2010-02-27 16:39:33 -05:00
|
|
|
:block => block,
|
2009-08-16 14:44:50 -04:00
|
|
|
:expects => 200,
|
|
|
|
:headers => headers,
|
|
|
|
:host => "#{bucket_name}.#{@host}",
|
2009-12-08 14:22:46 -05:00
|
|
|
:idempotent => true,
|
2009-08-16 14:44:50 -04:00
|
|
|
:method => 'GET',
|
2010-02-27 16:39:33 -05:00
|
|
|
:path => CGI.escape(object_name)
|
2009-08-08 15:31:32 -04:00
|
|
|
})
|
|
|
|
end
|
|
|
|
|
2009-10-03 18:43:19 -04:00
|
|
|
def get_object_url(bucket_name, object_name, expires)
|
|
|
|
unless bucket_name
|
|
|
|
raise ArgumentError.new('bucket_name is required')
|
|
|
|
end
|
|
|
|
unless object_name
|
|
|
|
raise ArgumentError.new('object_name is required')
|
|
|
|
end
|
|
|
|
url({
|
|
|
|
:headers => {},
|
|
|
|
:host => "#{bucket_name}.#{@host}",
|
|
|
|
:method => 'GET',
|
2010-03-13 16:37:24 -05:00
|
|
|
:path => CGI.escape(object_name)
|
2009-10-03 18:43:19 -04:00
|
|
|
}, expires)
|
|
|
|
end
|
|
|
|
|
2009-07-13 22:14:59 -04:00
|
|
|
end
|
2009-08-08 15:31:32 -04:00
|
|
|
|
2010-03-13 16:37:24 -05:00
|
|
|
class Mock
|
2009-08-08 15:31:32 -04:00
|
|
|
|
2009-09-10 13:08:29 -04:00
|
|
|
def get_object(bucket_name, object_name, options = {}, &block)
|
2009-09-08 23:24:21 -04:00
|
|
|
unless bucket_name
|
|
|
|
raise ArgumentError.new('bucket_name is required')
|
|
|
|
end
|
|
|
|
unless object_name
|
|
|
|
raise ArgumentError.new('object_name is required')
|
|
|
|
end
|
2009-11-20 14:08:08 -05:00
|
|
|
response = Excon::Response.new
|
2010-03-13 16:37:24 -05:00
|
|
|
if (bucket = @data[:buckets][bucket_name]) && (object = bucket[:objects][object_name])
|
2009-08-16 14:45:52 -04:00
|
|
|
if options['If-Match'] && options['If-Match'] != object['ETag']
|
|
|
|
response.status = 412
|
|
|
|
elsif options['If-Modified-Since'] && options['If-Modified-Since'] > Time.parse(object['LastModified'])
|
|
|
|
response.status = 304
|
|
|
|
elsif options['If-None-Match'] && options['If-None-Match'] == object['ETag']
|
|
|
|
response.status = 304
|
|
|
|
elsif options['If-Unmodified-Since'] && options['If-Unmodified-Since'] < Time.parse(object['LastModified'])
|
|
|
|
response.status = 412
|
2009-08-08 15:31:32 -04:00
|
|
|
else
|
2009-08-16 14:45:52 -04:00
|
|
|
response.status = 200
|
|
|
|
response.headers = {
|
|
|
|
'Content-Length' => object['Size'],
|
2009-09-02 23:09:06 -04:00
|
|
|
'Content-Type' => object['Content-Type'],
|
2009-08-16 14:45:52 -04:00
|
|
|
'ETag' => object['ETag'],
|
|
|
|
'Last-Modified' => object['LastModified']
|
|
|
|
}
|
2009-09-10 13:08:29 -04:00
|
|
|
unless block_given?
|
|
|
|
response.body = object[:body]
|
|
|
|
else
|
|
|
|
data = StringIO.new(object[:body])
|
|
|
|
remaining = data.length
|
|
|
|
while remaining > 0
|
2009-12-18 21:13:46 -05:00
|
|
|
chunk = data.read([remaining, Excon::CHUNK_SIZE].min)
|
2009-09-10 13:08:29 -04:00
|
|
|
block.call(chunk)
|
2009-12-18 21:13:46 -05:00
|
|
|
remaining -= Excon::CHUNK_SIZE
|
2009-09-10 13:08:29 -04:00
|
|
|
end
|
|
|
|
end
|
2009-08-08 15:31:32 -04:00
|
|
|
end
|
|
|
|
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
|
|
|
|
|
2010-02-02 01:53:18 -05:00
|
|
|
def get_object_url(bucket_name, object_name, expires)
|
|
|
|
raise MockNotImplemented.new("Contributions welcome!")
|
|
|
|
end
|
|
|
|
|
2009-08-08 15:31:32 -04:00
|
|
|
end
|
2009-07-13 22:14:59 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|