/
get_object.rb
107 lines (101 loc) · 4.42 KB
/
get_object.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
module Fog
module AWS
class Storage
class Real
# 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
# * 'versionId'<~String> - specify a particular version to retrieve
#
# ==== Returns
# * response<~Excon::Response>:
# * 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
#
# ==== See Also
# http://docs.amazonwebservices.com/AmazonS3/latest/API/RESTObjectGET.html
def get_object(bucket_name, object_name, options = {}, &block)
unless bucket_name
raise ArgumentError.new('bucket_name is required')
end
unless object_name
raise ArgumentError.new('object_name is required')
end
if version_id = options.delete('versionId')
query = {'versionId' => version_id}
end
headers = {}
headers['If-Modified-Since'] = Fog::Time.at(options['If-Modified-Since'].to_i).to_date_header if options['If-Modified-Since']
headers['If-Unmodified-Since'] = Fog::Time.at(options['If-Unmodified-Since'].to_i).to_date_header if options['If-Modified-Since']
headers.merge!(options)
request({
:expects => 200,
:headers => headers,
:host => "#{bucket_name}.#{@host}",
:idempotent => true,
:method => 'GET',
:path => CGI.escape(object_name),
:query => query
}, &block)
end
end
class Mock # :nodoc:all
def get_object(bucket_name, object_name, options = {}, &block)
unless bucket_name
raise ArgumentError.new('bucket_name is required')
end
unless object_name
raise ArgumentError.new('object_name is required')
end
response = Excon::Response.new
if (bucket = @data[:buckets][bucket_name]) && (object = bucket[:objects][object_name])
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
else
response.status = 200
response.headers = {
'Content-Length' => object['Size'],
'Content-Type' => object['Content-Type'],
'ETag' => object['ETag'],
'Last-Modified' => object['LastModified']
}
unless block_given?
response.body = object[:body]
else
data = StringIO.new(object[:body])
remaining = data.length
while remaining > 0
chunk = data.read([remaining, Excon::CHUNK_SIZE].min)
block.call(chunk)
remaining -= Excon::CHUNK_SIZE
end
end
end
else
response.status = 404
raise(Excon::Errors.status_error({:expects => 200}, response))
end
response
end
end
end
end
end