Class: Canistor::Storage::Object

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

Defined Under Namespace

Classes: CopyObjectResult

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**attributes) ⇒ Object

Returns a new instance of Object.



43
44
45
46
47
48
49
50
51
# File 'lib/canistor/storage/object.rb', line 43

def initialize(**attributes)
  @versioned = false
  @headers = {}
  @data = ''
  attributes.each do |name, value|
    send("#{name}=", value)
  end
  @digest = nil
end

Instance Attribute Details

#bucketObject

Returns the value of attribute bucket.



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

def bucket
  @bucket
end

#dataObject

Returns the value of attribute data.



40
41
42
# File 'lib/canistor/storage/object.rb', line 40

def data
  @data
end

#keyObject

Returns the value of attribute key.



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

def key
  @key
end

#last_modifiedObject (readonly)

Returns the value of attribute last_modified.



41
42
43
# File 'lib/canistor/storage/object.rb', line 41

def last_modified
  @last_modified
end

#regionObject

Returns the value of attribute region.



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

def region
  @region
end

Instance Method Details

#copyObject



136
137
138
139
140
# File 'lib/canistor/storage/object.rb', line 136

def copy
  object = self.class.new(region: region, bucket: bucket, key: key)
  object.write(headers, data)
  object
end

#copy_headers(context) ⇒ Object



146
147
148
149
150
151
152
153
154
155
156
# File 'lib/canistor/storage/object.rb', line 146

def copy_headers(context)
  directive = context.http_request.headers['x-amz-metadata-directive']
  case directive
  when 'COPY'
    identity_headers
  when 'REPLACE', nil
    context.http_request.headers
  else
    raise ArgumentError, "Unsupported metadata directive: `#{directive}'"
  end
end

#delete(context, subject) ⇒ Object



142
143
144
# File 'lib/canistor/storage/object.rb', line 142

def delete(context, subject)
  context.http_response.signal_headers(200, {})
end

#digestObject



80
81
82
# File 'lib/canistor/storage/object.rb', line 80

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

#endpointObject



88
89
90
# File 'lib/canistor/storage/object.rb', line 88

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

#etagObject



84
85
86
# File 'lib/canistor/storage/object.rb', line 84

def etag
  '"' + digest + '"'
end

#get(context, subject) ⇒ Object



123
124
125
126
# File 'lib/canistor/storage/object.rb', line 123

def get(context, subject)
  context.http_response.signal_headers(200, headers)
  context.http_response.signal_data(data)
end

#head(context, subject) ⇒ Object



119
120
121
# File 'lib/canistor/storage/object.rb', line 119

def head(context, subject)
  context.http_response.signal_headers(200, headers)
end

#headersObject



100
101
102
103
104
105
106
107
# File 'lib/canistor/storage/object.rb', line 100

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



109
110
111
112
113
114
115
116
117
# File 'lib/canistor/storage/object.rb', line 109

def identity_headers
  {
    'etag' => etag,
    'x-amz-id' => digest[0, 16],
    'x-amz-id-2' => Base64.strict_encode64(digest),
    'x-amz-request-id' => Base64.strict_encode64(SecureRandom.hex(16)),
    'x-amz-version-id' => version_id
  }.compact
end

#labelObject



71
72
73
# File 'lib/canistor/storage/object.rb', line 71

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

#locationObject



92
93
94
95
96
97
98
# File 'lib/canistor/storage/object.rb', line 92

def location
  [
    endpoint,
    bucket,
    key,
  ].join('/')
end

#put(context, subject) ⇒ Object



128
129
130
131
132
133
134
# File 'lib/canistor/storage/object.rb', line 128

def put(context, subject)
  if source_object = find_source(context, subject)
    apply_source_object(context, subject, source_object)
  else
    apply_request(context, subject)
  end
end

#sizeObject



67
68
69
# File 'lib/canistor/storage/object.rb', line 67

def size
  data&.size
end

#version_idObject



61
62
63
64
65
# File 'lib/canistor/storage/object.rb', line 61

def version_id
  if versioned?
    @version_id ||= generate_version_id
  end
end

#versioned=(versioned) ⇒ Object



53
54
55
# File 'lib/canistor/storage/object.rb', line 53

def versioned=(versioned)
  @versioned = versioned
end

#versioned?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/canistor/storage/object.rb', line 57

def versioned?
  !!@versioned
end

#write(headers, data) ⇒ Object



75
76
77
78
# File 'lib/canistor/storage/object.rb', line 75

def write(headers, data)
  self.headers = headers
  self.data = data
end