Class: Canistor::Storage::Object

Inherits:
Object
  • Object
show all
Defined in:
lib/canistor/storage/object.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#bucketObject

Returns the value of attribute bucket.



9
10
11
# File 'lib/canistor/storage/object.rb', line 9

def bucket
  @bucket
end

#dataObject

Returns the value of attribute data.



12
13
14
# File 'lib/canistor/storage/object.rb', line 12

def data
  @data
end

#keyObject

Returns the value of attribute key.



10
11
12
# File 'lib/canistor/storage/object.rb', line 10

def key
  @key
end

#last_modifiedObject (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

#regionObject

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

#digestObject



37
38
39
# File 'lib/canistor/storage/object.rb', line 37

def digest
  @digest ||= Digest::SHA1.hexdigest(data)
end

#endpointObject



45
46
47
# File 'lib/canistor/storage/object.rb', line 45

def endpoint
  Aws::Partitions::EndpointProvider.resolve(region, 's3')
end

#etagObject



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

#headersObject



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_headersObject



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

#labelObject



28
29
30
# File 'lib/canistor/storage/object.rb', line 28

def label
  [region, bucket, key].map(&:to_s).join(':') + ' ' + headers.inspect
end

#locationObject



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

#sizeObject



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