Class: Canistor::Storage::Object
- Inherits:
-
Object
- Object
- Canistor::Storage::Object
- Defined in:
- lib/canistor/storage/object.rb
Instance Attribute Summary collapse
-
#bucket ⇒ Object
Returns the value of attribute bucket.
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#key ⇒ Object
Returns the value of attribute key.
-
#last_modified ⇒ Object
readonly
Returns the value of attribute last_modified.
-
#region ⇒ Object
Returns the value of attribute region.
Instance Method Summary collapse
- #delete(context, subject) ⇒ Object
- #digest ⇒ Object
- #endpoint ⇒ Object
- #etag ⇒ Object
- #get(context, subject) ⇒ Object
- #head(context, subject) ⇒ Object
- #headers ⇒ Object
- #identity_headers ⇒ Object
-
#initialize(**attributes) ⇒ Object
constructor
A new instance of Object.
- #label ⇒ Object
- #location ⇒ Object
- #put(context, subject) ⇒ Object
- #size ⇒ Object
- #write(headers, data) ⇒ Object
Constructor Details
#initialize(**attributes) ⇒ Object
Returns a new instance of Object.
15 16 17 18 19 20 21 22 |
# File 'lib/canistor/storage/object.rb', line 15 def initialize(**attributes) @headers = {} @data = '' attributes.each do |name, value| send("#{name}=", value) end @digest = nil end |
Instance Attribute Details
#bucket ⇒ Object
Returns the value of attribute bucket.
9 10 11 |
# File 'lib/canistor/storage/object.rb', line 9 def bucket @bucket end |
#data ⇒ Object
Returns the value of attribute data.
12 13 14 |
# File 'lib/canistor/storage/object.rb', line 12 def data @data end |
#key ⇒ Object
Returns the value of attribute key.
10 11 12 |
# File 'lib/canistor/storage/object.rb', line 10 def key @key end |
#last_modified ⇒ Object (readonly)
Returns the value of attribute last_modified.
13 14 15 |
# File 'lib/canistor/storage/object.rb', line 13 def last_modified @last_modified end |
#region ⇒ Object
Returns the value of attribute region.
8 9 10 |
# File 'lib/canistor/storage/object.rb', line 8 def region @region end |
Instance Method Details
#delete(context, subject) ⇒ Object
95 96 97 |
# File 'lib/canistor/storage/object.rb', line 95 def delete(context, subject) context.http_response.signal_headers(200, {}) end |
#digest ⇒ Object
37 38 39 |
# File 'lib/canistor/storage/object.rb', line 37 def digest @digest ||= Digest::SHA1.hexdigest(data) end |
#endpoint ⇒ Object
45 46 47 |
# File 'lib/canistor/storage/object.rb', line 45 def endpoint Aws::Partitions::EndpointProvider.resolve(region, 's3') end |
#etag ⇒ Object
41 42 43 |
# File 'lib/canistor/storage/object.rb', line 41 def etag '"' + digest + '"' end |
#get(context, subject) ⇒ Object
79 80 81 82 |
# File 'lib/canistor/storage/object.rb', line 79 def get(context, subject) context.http_response.signal_headers(200, headers) context.http_response.signal_data(data) end |
#head(context, subject) ⇒ Object
75 76 77 |
# File 'lib/canistor/storage/object.rb', line 75 def head(context, subject) context.http_response.signal_headers(200, headers) end |
#headers ⇒ Object
57 58 59 60 61 62 63 64 |
# File 'lib/canistor/storage/object.rb', line 57 def headers @headers.merge(identity_headers).merge( 'date' => Time.now.httpdate, 'content-length' => size.to_s, 'last-modified' => last_modified.httpdate, 'server' => 'Canistor' ) end |
#identity_headers ⇒ Object
66 67 68 69 70 71 72 73 |
# File 'lib/canistor/storage/object.rb', line 66 def identity_headers { 'x-amz-request-id' => Base64.strict_encode64(SecureRandom.hex(16)), 'x-amz-id' => digest[0, 16], 'x-amz-id-2' => Base64.strict_encode64(digest), 'etag' => etag } end |
#label ⇒ Object
28 29 30 |
# File 'lib/canistor/storage/object.rb', line 28 def label [region, bucket, key].map(&:to_s).join(':') + ' ' + headers.inspect end |
#location ⇒ Object
49 50 51 52 53 54 55 |
# File 'lib/canistor/storage/object.rb', line 49 def location [ endpoint, bucket, key, ].join('/') end |
#put(context, subject) ⇒ Object
84 85 86 87 88 89 90 91 92 93 |
# File 'lib/canistor/storage/object.rb', line 84 def put(context, subject) catch(:rendered_error) do source_object = source_object(context, subject) write( object_headers(context, source_object), object_data(context, source_object) ) context.http_response.signal_headers(200, identity_headers) end end |
#size ⇒ Object
24 25 26 |
# File 'lib/canistor/storage/object.rb', line 24 def size data&.size end |
#write(headers, data) ⇒ Object
32 33 34 35 |
# File 'lib/canistor/storage/object.rb', line 32 def write(headers, data) self.headers = headers self.data = data end |