Class: Oss::Object

Inherits:
Object
  • Object
show all
Includes:
Util
Defined in:
lib/oss/object.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Util

#camelize, #hash_filter, #oss_headers_to_s, #set_query_string

Constructor Details

#initialize(client) ⇒ Object

Returns a new instance of Object.



14
15
16
# File 'lib/oss/object.rb', line 14

def initialize(client)
  @client = client
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method) ⇒ Object



317
318
319
320
321
322
323
324
325
326
# File 'lib/oss/object.rb', line 317

def method_missing(method)
  if @xml_obj.nil?
    super
  else
    camel = Util.camelize(method)
    value = @xml_obj.xpath(camel)
    raise "missing xml attribute #{camel}" if value.length == 0
    value.inner_text
  end
end

Instance Attribute Details

#clientObject

Returns the value of attribute client.



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

def client
  @client
end

#xml_objObject

Returns the value of attribute xml_obj.



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

def xml_obj
  @xml_obj
end

Instance Method Details

#append_object(bucket_name, object, file, position = 0, options = {}) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/oss/object.rb', line 141

def append_object(bucket_name, object, file, position = 0, options = {})
  # set header
  headers = Util.hash_filter(options, {
                                        expires:             'Expires',
                                        content_control:     'Cache-Control',
                                        content_encoding:    'Content-Encoding',
                                        content_type:        'Content-Type',
                                        content_md5:         'Content-MD5',
                                        content_disposition: 'Content-Disposition',
                                        encryption:          'x-oss-server-side-encryption',
                                        acl:                 'x-oss-object-acl',
                                    })

  # sign configs
  sign_configs = {
      resource:     "/#{bucket_name}",
      content_type: options[:content_type],
      content_md5:  options[:content_md5],
      sign_query_string: true,
      oss_headers:  Util.oss_headers_to_s(options, {
                                                     acl:        'x-oss-object-acl',
                                                     encryption: 'x-oss-server-side-encryption',
                                                 })
  }

  resp = client.post(
      host:         "#{bucket_name}.#{client.endpoint}",
      path:         "/#{object}?append&position=#{position}",
      headers:      headers,
      sign_configs: sign_configs,
      payload:      file,
      as:           :raw
  )

  {
      hash_crc64ecma:       resp.headers[:x_oss_hash_crc64ecma],
      next_append_position: resp.headers[:x_oss_next_append_position].to_i
  }
end

#copy_object(bucket_name, object, old_bucket, old_object, options = {}) ⇒ Object



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
# File 'lib/oss/object.rb', line 56

def copy_object(bucket_name, object, old_bucket, old_object, options = {})
  # copy source format
  options[:copy_source] = "/#{old_bucket}/#{old_object}"

  # set header
  headers = Util.hash_filter(options, {
                                        metadata_directive:  'x-oss-metadata-directive',
                                        if_modified_since:   'x-oss-copy-source-if-modified-since',
                                        if_unmodified_since: 'x-oss-copy-source-if-unmodified-since',
                                        if_match:            'x-oss-copy-source-if-match',
                                        if_none_match:       'x-oss-copy-source-if-none-match',
                                        encryption:          'x-oss-server-side-encryption',
                                        acl:                 'x-oss-object-acl',
                                        copy_source:         'x-oss-copy-source'
                                    })

  # sign configs
  sign_configs = {
      resource:     "/#{bucket_name}",
      content_type: 'application/x-www-form-urlencoded',
      oss_headers:  Util.oss_headers_to_s(options, {
                                                     metadata_directive:  'x-oss-metadata-directive',
                                                     if_modified_since:   'x-oss-copy-source-if-modified-since',
                                                     if_unmodified_since: 'x-oss-copy-source-if-unmodified-since',
                                                     if_match:            'x-oss-copy-source-if-match',
                                                     if_none_match:       'x-oss-copy-source-if-none-match',
                                                     encryption:          'x-oss-server-side-encryption',
                                                     acl:                 'x-oss-object-acl',
                                                     copy_source:         'x-oss-copy-source'
                                                 })
  }

  @xml_obj = client.put(
      host:         "#{bucket_name}.#{client.endpoint}",
      path:         "/#{object}",
      headers:      headers,
      sign_configs: sign_configs,
  )

  {
      last_modify: @xml_obj.xpath('CopyObjectResult/LastModified').text,
      etag:        @xml_obj.xpath('CopyObjectResult/ETag').text,
  }
end

#delete_multiple_objects(bucket_name, objects = []) ⇒ Object



195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/oss/object.rb', line 195

def delete_multiple_objects(bucket_name, objects = [])
  # build payload xml
  payload = Nokogiri::XML::Builder.new(:encoding => 'UTF-8') do
    Delete do
      Quiet 'false'
      objects.each do |obj|
        Object do
          Key obj
        end
      end
    end
  end

  xml_obj = client.post(
      host: "#{bucket_name}.#{client.endpoint}",
      path: "/?delete",
      sign_configs: {resource: "/#{bucket_name}", content_type: 'application/x-www-form-urlencoded'},
      content_md5_check: true,
      content_length_check: true,
      payload: payload.to_xml
  )

  # parse return deleted keys
  deleted = Array.new
  xml_obj.xpath('DeleteResult/Deleted').each do |obj|
    deleted << obj.xpath('Key').text
  end

  deleted
end

#delete_object(bucket_name, object_name) ⇒ Object

params:

  • bucket_name

  • object_name



184
185
186
187
188
189
190
191
192
193
# File 'lib/oss/object.rb', line 184

def delete_object(bucket_name, object_name)

  client.delete(
      host: "#{bucket_name}.#{client.endpoint}",
      path: "/#{object_name}",
      sign_configs: {resource: "/#{bucket_name}"}
  )

  true
end

#get_object(bucket_name, object, options = {}) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/oss/object.rb', line 111

def get_object(bucket_name, object, options = {})
  # set header
  headers = Util.hash_filter(options, {
                                        range:               'Range',
                                        if_modified_since:   'If-Modified-Since',
                                        if_unmodified_since: 'If-Unmodified-Since',
                                        if_match:            'If-Match',
                                        if_none_match:       'If-None-Match'
                                    })

  # request params query string
  query_string = Util.hash_filter(options, {
                                             response_content_type:        'response-content-type',
                                             response_content_language:    'response-content-language',
                                             response_expires:             'response-expires',
                                             response_cache_control:       'response-cache-control',
                                             response_content_disposition: 'response-content-disposition',
                                             response_content_encoding:    'response-content-encoding'
                                         })

  client.get(
      host:         "#{bucket_name}.#{client.endpoint}",
      path:         "/#{object}",
      headers:      headers,
      sign_configs: { resource: "/#{bucket_name}", sign_query_string: true },
      query_string: query_string,
      as:           :raw
  )
end

#get_object_acl(bucket_name, object_name) ⇒ Object

params:

  • bucket_name

  • object_name



271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
# File 'lib/oss/object.rb', line 271

def get_object_acl(bucket_name, object_name)
  xml_obj = client.get(
      host: "#{bucket_name}.#{client.endpoint}",
      path: "/#{object_name}?acl",
      sign_configs: {resource: "/#{bucket_name}"}
  ).xpath('AccessControlPolicy')

  {
      grant: xml_obj.xpath('AccessControlList/Grant').text,
      owner: {
          id:           xml_obj.xpath('Owner/ID').text,
          display_name: xml_obj.xpath('Owner/DisplayName').text
      }
  }
end

#get_object_url(bucket_name, object, options = {}) ⇒ Object



101
102
103
104
105
106
107
108
109
# File 'lib/oss/object.rb', line 101

def get_object_url(bucket_name, object, options = {})
  if options[:from].to_s == 'internal'
    "http://#{bucket_name}.#{client.endpoint.gsub('-internal', '').gsub('.aliyuncs.com', '-internal.aliyuncs.com')}/#{object}"
  elsif options[:from].to_s == 'cdn'
    "http://#{options[:cdn_domain]}/#{object}"
  else
    "http://#{bucket_name}.#{client.endpoint}/#{object}"
  end
end

#head_object(bucket_name, object_name, options = {}) ⇒ Object



226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'lib/oss/object.rb', line 226

def head_object(bucket_name, object_name, options = {})
  # set header
  headers = Util.hash_filter(options, {
                                        if_modified_since:   'If-Modified-Since',
                                        if_unmodified_since: 'If-Unmodified-Since',
                                        if_match:            'If-Match',
                                        if_none_match:       'If-None-Match'
                                    })

  resp = client.head(
      host: "#{bucket_name}.#{client.endpoint}",
      path: "/#{object_name}",
      headers: headers,
      sign_configs: {resource: "/#{bucket_name}"},
      as: :raw
  )

  # head request returns hole response headers
  resp.headers
end

#post_object(bucket_name, key, options = {}) ⇒ Object

params:

  • bucket_name

  • key

  • options



291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
# File 'lib/oss/object.rb', line 291

def post_object(bucket_name, key, options = {})

  # build form upload info hash
  form_hash = { 'key' => key, 'action' => "http://#{bucket_name}.#{client.endpoint}/"}
  options.each do |k, v|
    # need Signature
    if k == :policy
      # json policy string to base64 policy string
      form_hash['policy'] = Base64.encode64(v).gsub("\n", '')

      # create signature
      digest = OpenSSL::Digest.new('sha1')
      h = OpenSSL::HMAC.digest(digest, client.access_key_secret, form_hash['policy'])
      # base64 result
      form_hash['Signature'] = Base64.encode64(h).gsub("\n", '')
      # add access key id
      form_hash['OSSAccessKeyId'] = client.access_key_id
    else
      form_hash[k.to_s] = v
    end
  end

  # return a hash for render a html upload form
  form_hash
end

#put_object(bucket_name, object, file, options = {}) ⇒ Object



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
# File 'lib/oss/object.rb', line 18

def put_object(bucket_name, object, file, options = {})
  options[:content_type] = 'application/x-www-form-urlencoded' if options[:content_type].nil?

  # set header
  headers = Util.hash_filter(options, {
                                        expires:             'Expires',
                                        content_control:     'Cache-Control',
                                        content_encoding:    'Content-Encoding',
                                        content_type:        'Content-Type',
                                        content_md5:         'Content-MD5',
                                        content_disposition: 'Content-Disposition',
                                        encryption:          'x-oss-server-side-encryption',
                                        acl:                 'x-oss-object-acl',
                                    })

  # sign configs
  sign_configs = {
      resource:              "/#{bucket_name}",
      content_type:          options[:content_type],
      content_md5_check:     options[:content_md5_check],
      content_length_check:  options[:content_length_check],
      oss_headers:  Util.oss_headers_to_s(options, {
                                                    acl:        'x-oss-object-acl',
                                                    encryption: 'x-oss-server-side-encryption',
                                                })
  }

  client.put(
      host:         "#{bucket_name}.#{client.endpoint}",
      path:         "/#{object}",
      headers:      headers,
      sign_configs: sign_configs,
      payload:      file,
  )

  true
end

#put_object_acl(bucket_name, object_name, acl) ⇒ Object

params:

  • bucket_name

  • object_name

  • acl



251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
# File 'lib/oss/object.rb', line 251

def put_object_acl(bucket_name, object_name, acl)
  # sign configs
  sign_configs = Hash.new
  sign_configs[:resource]     = "/#{bucket_name}"
  sign_configs[:oss_headers]  = "x-oss-object-acl:#{acl}"
  sign_configs[:content_type] = 'application/x-www-form-urlencoded'

  @xml_obj = client.put(
      host: "#{bucket_name}.#{client.endpoint}",
      path: "/#{object_name}?acl",
      headers: {'x-oss-object-acl' => acl},
      sign_configs: sign_configs
  )

  true
end