Class: Oss::Bucket

Inherits:
Object
  • Object
show all
Includes:
Util
Defined in:
lib/oss/bucket.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) ⇒ Bucket

Returns a new instance of Bucket.



13
14
15
# File 'lib/oss/bucket.rb', line 13

def initialize(client)
  @client = client
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method) ⇒ Object



460
461
462
463
464
465
466
467
468
469
# File 'lib/oss/bucket.rb', line 460

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.



11
12
13
# File 'lib/oss/bucket.rb', line 11

def client
  @client
end

#xml_objObject

Returns the value of attribute xml_obj.



11
12
13
# File 'lib/oss/bucket.rb', line 11

def xml_obj
  @xml_obj
end

Instance Method Details

#contentsObject



239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
# File 'lib/oss/bucket.rb', line 239

def contents
  contents = Array.new
  xml_contents = @xml_obj.xpath('Contents')
  xml_contents.each do |content|
    contents << {
        key:           content.xpath('Key').text,
        last_modified: content.xpath('LastModified').text,
        etag:          content.xpath('ETag').text,
        type:          content.xpath('Type').text,
        size:          content.xpath('Size').text,
        storage_class: content.xpath('StorageClass').text,
        owner: {
            id:           content.xpath('Owner/ID').text,
            display_name: content.xpath('Owner/DisplayName').text
        }
    }
  end
  contents
end

#delete_bucket(bucket_name) ⇒ Object

params:

  • bucket_name



445
446
447
448
449
450
451
452
453
454
455
456
457
# File 'lib/oss/bucket.rb', line 445

def delete_bucket(bucket_name)
  # sign configs
  sign_configs = Hash.new
  sign_configs[:resource] = "/#{bucket_name}"

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

  true
end

#delete_bucket_lifecycle(bucket_name) ⇒ Object

params:

  • bucket_name



429
430
431
432
433
434
435
436
437
438
439
440
441
# File 'lib/oss/bucket.rb', line 429

def delete_bucket_lifecycle(bucket_name)
  # sign configs
  sign_configs = Hash.new
  sign_configs[:resource] = "/#{bucket_name}"

  client.delete(
      host: "#{bucket_name}.#{client.endpoint}",
      path: '/?lifecycle',
      sign_configs: sign_configs
  )

  true
end

#delete_bucket_logging(bucket_name) ⇒ Object

params:

  • bucket_name



397
398
399
400
401
402
403
404
405
406
407
408
409
# File 'lib/oss/bucket.rb', line 397

def delete_bucket_logging(bucket_name)
  # sign configs
  sign_configs = Hash.new
  sign_configs[:resource] = "/#{bucket_name}"

  client.delete(
      host: "#{bucket_name}.#{client.endpoint}",
      path: '/?logging',
      sign_configs: sign_configs
  )

  true
end

#delete_bucket_website(bucket_name) ⇒ Object

params:

  • bucket_name



413
414
415
416
417
418
419
420
421
422
423
424
425
# File 'lib/oss/bucket.rb', line 413

def delete_bucket_website(bucket_name)
  # sign configs
  sign_configs = Hash.new
  sign_configs[:resource] = "/#{bucket_name}"

  client.delete(
      host: "#{bucket_name}.#{client.endpoint}",
      path: '/?website',
      sign_configs: sign_configs
  )

  true
end

#get_bucket(bucket_name, options = {}) ⇒ Object

params:

  • options:

    • delimiter

    • prefix

    • marker

    • max_keys

    • encoding_type



224
225
226
227
228
229
230
231
232
233
234
235
236
237
# File 'lib/oss/bucket.rb', line 224

def get_bucket(bucket_name, options = {})
  # sign configs
  sign_configs = Hash.new
  sign_configs[:resource] = "/#{bucket_name}"

  @xml_obj = client.get(
      host: "#{bucket_name}.#{client.endpoint}",
      path: '/',
      sign_configs: sign_configs,
      query_string: options
  ).xpath('ListBucketResult')

  self
end

#get_bucket_acl(bucket_name) ⇒ Object

params:

  • bucket_name



261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
# File 'lib/oss/bucket.rb', line 261

def get_bucket_acl(bucket_name)
  # sign configs
  sign_configs = Hash.new
  sign_configs[:resource] = "/#{bucket_name}"

  xml_obj = client.get(
      host: "#{bucket_name}.#{client.endpoint}",
      path: '/?acl',
      sign_configs: sign_configs
  ).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_bucket_lifecycle(bucket_name) ⇒ Object

params:

  • bucket_name



366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
# File 'lib/oss/bucket.rb', line 366

def get_bucket_lifecycle(bucket_name)
  # sign configs
  sign_configs = Hash.new
  sign_configs[:resource] = "/#{bucket_name}"

  xml_obj = client.get(
      host: "#{bucket_name}.#{client.endpoint}",
      path: '/?lifecycle',
      sign_configs: sign_configs
  ).xpath('LifecycleConfiguration')

  rules = Array.new
  xml_obj.xpath('Rule').each do |xml_rule|
    rule = {
        id: xml_rule.xpath('ID').text,
        prefix: xml_rule.xpath('Prefix').text,
        status: xml_rule.xpath('Status').text == 'Enabled',
    }
    if xml_rule.xpath('Expiration/Days').length > 0
      rule[:days] = xml_rule.xpath('Expiration/Days').text.to_i
    else
      rule[:date] = xml_rule.xpath('Expiration/Date').text
    end
    rules << rule
  end

  rules
end

#get_bucket_location(bucket_name) ⇒ Object

params:

  • bucket_name



283
284
285
286
287
288
289
290
291
292
293
294
295
# File 'lib/oss/bucket.rb', line 283

def get_bucket_location(bucket_name)
  # sign configs
  sign_configs = Hash.new
  sign_configs[:resource] = "/#{bucket_name}"

  xml_obj = client.get(
      host: "#{bucket_name}.#{client.endpoint}",
      path: '/?location',
      sign_configs: sign_configs
  )

  xml_obj.xpath('LocationConstraint').text
end

#get_bucket_logging(bucket_name) ⇒ Object

params:

  • bucket_name



299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
# File 'lib/oss/bucket.rb', line 299

def get_bucket_logging(bucket_name)
  # sign configs
  sign_configs = Hash.new
  sign_configs[:resource] = "/#{bucket_name}"

  xml_obj = client.get(
      host: "#{bucket_name}.#{client.endpoint}",
      path: '/?logging',
      sign_configs: sign_configs
  ).xpath('BucketLoggingStatus/LoggingEnabled')

  if xml_obj.length > 0
    {
        logging_enabled: true,
        target_bucket:   xml_obj.xpath('TargetBucket').text,
        target_prefix:   xml_obj.xpath('TargetPrefix').text,
    }
  else
    { logging_enabled: false }
  end
end

#get_bucket_referer(bucket_name) ⇒ Object

params:

  • bucket_name



342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
# File 'lib/oss/bucket.rb', line 342

def get_bucket_referer(bucket_name)
  # sign configs
  sign_configs = Hash.new
  sign_configs[:resource] = "/#{bucket_name}"

  xml_obj = client.get(
      host: "#{bucket_name}.#{client.endpoint}",
      path: '/?referer',
      sign_configs: sign_configs
  ).xpath('RefererConfiguration')

  referers = Array.new
  xml_obj.xpath('RefererList/Referer').each do |referer|
    referers << referer.text
  end

  {
      allow_empty_referer: xml_obj.xpath('AllowEmptyReferer').text == 'true',
      referers: referers,
  }
end

#get_bucket_website(bucket_name) ⇒ Object

params:

  • bucket_name



323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
# File 'lib/oss/bucket.rb', line 323

def get_bucket_website(bucket_name)
  # sign configs
  sign_configs = Hash.new
  sign_configs[:resource] = "/#{bucket_name}"

  xml_obj = client.get(
      host: "#{bucket_name}.#{client.endpoint}",
      path: '/?website',
      sign_configs: sign_configs
  ).xpath('WebsiteConfiguration')

  {
      index_doc: xml_obj.xpath('IndexDocument/Suffix').text,
      error_doc: xml_obj.xpath('ErrorDocument/Key').text,
  }
end

#put_bucket(bucket_name, acl) ⇒ Object

params:

  • bucket_name

  • acl



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

def put_bucket(bucket_name, acl)
  # get location
  location = client.endpoint.split('.')[0]

  # build payload xml
  payload = Nokogiri::XML::Builder.new do
    CreateBucketConfiguration do
      LocationConstraint location
    end
  end

  # set acl header
  headers = Hash.new
  headers['x-oss-acl'] = acl

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

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

  true
end

#put_bucket_acl(bucket_name, acl) ⇒ Object

params:

  • bucket_name

  • acl



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/oss/bucket.rb', line 55

def put_bucket_acl(bucket_name, acl)
  # set acl header
  headers = Hash.new
  headers['x-oss-acl'] = acl

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

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

  true
end

#put_bucket_lifecycle(bucket_name, rules = []) ⇒ Object

params:

  • bucket_name

  • rules

    • prefix

    • id

    • status

    • days

    • date



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/oss/bucket.rb', line 181

def put_bucket_lifecycle(bucket_name, rules = [])
  # sign configs
  sign_configs = Hash.new
  sign_configs[:resource]     = "/#{bucket_name}"
  sign_configs[:content_type] = 'application/x-www-form-urlencoded'

  # build payload xml
  payload = Nokogiri::XML::Builder.new do
    LifecycleConfiguration do
      rules.each do |rule|
        Rule do
          ID rule[:id] unless rule[:id].nil?
          Prefix rule[:prefix]
          Status rule[:status] ? 'Enabled' : 'Disabled'
          Expiration do
            if rule[:days] != nil
              Days rule[:days]
            else
              Date rule[:date]
            end
          end
        end
      end
    end
  end

  @xml_obj = client.put(
      host: "#{bucket_name}.#{client.endpoint}",
      path: '/?lifecycle',
      sign_configs: sign_configs,
      payload: payload.to_xml
  )

  true
end

#put_bucket_logging(bucket_name, target_bucket, enable = true, target_prefix = nil) ⇒ Object

params:

  • bucket_name

  • target_bucket

  • enable

  • target_prefix



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

def put_bucket_logging(bucket_name, target_bucket, enable = true, target_prefix = nil)
  # sign configs
  sign_configs = Hash.new
  sign_configs[:resource]     = "/#{bucket_name}"
  sign_configs[:content_type] = 'application/x-www-form-urlencoded'

  # build payload xml
  payload = Nokogiri::XML::Builder.new do
    BucketLoggingStatus do
      if enable
        LoggingEnabled do
          TargetBucket target_bucket
          TargetPrefix target_prefix unless target_prefix.nil?
        end
      end
    end
  end

  @xml_obj = client.put(
      host: "#{bucket_name}.#{client.endpoint}",
      path: '/?logging',
      sign_configs: sign_configs,
      payload: payload.to_xml
  )

  true
end

#put_bucket_referer(bucket_name, allow_empty, referer_list = []) ⇒ Object

params:

  • bucket_name

  • allow_empty

  • referer_list



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

def put_bucket_referer(bucket_name, allow_empty, referer_list = [])
  # sign configs
  sign_configs = Hash.new
  sign_configs[:resource]     = "/#{bucket_name}"
  sign_configs[:content_type] = 'application/x-www-form-urlencoded'

  # build payload xml
  payload = Nokogiri::XML::Builder.new do
    RefererConfiguration do
      AllowEmptyReferer allow_empty.to_s
      RefererList do
        referer_list.each do |referer|
          Referer referer
        end
      end
    end
  end

  @xml_obj = client.put(
      host: "#{bucket_name}.#{client.endpoint}",
      path: '/?referer',
      sign_configs: sign_configs,
      payload: payload.to_xml
  )

  true
end

#put_bucket_website(bucket_name, index_doc, error_doc) ⇒ Object

params:

  • bucket_name

  • index_doc

  • error_doc



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/bucket.rb', line 113

def put_bucket_website(bucket_name, index_doc, error_doc)
  # sign configs
  sign_configs = Hash.new
  sign_configs[:resource]     = "/#{bucket_name}"
  sign_configs[:content_type] = 'application/x-www-form-urlencoded'

  # build payload xml
  payload = Nokogiri::XML::Builder.new do
    WebsiteConfiguration do
      IndexDocument do
        Suffix index_doc
      end
      ErrorDocument do
        Key error_doc
      end
    end
  end

  @xml_obj = client.put(
      host: "#{bucket_name}.#{client.endpoint}",
      path: '/?website',
      sign_configs: sign_configs,
      payload: payload.to_xml
  )

  true
end