oss-ruby-sdk 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/lib/oss/bucket.rb ADDED
@@ -0,0 +1,473 @@
1
+ require 'oss/client'
2
+ require 'nokogiri'
3
+ require 'oss/util'
4
+
5
+ module Oss
6
+
7
+ class Bucket
8
+
9
+ include Util
10
+
11
+ attr_accessor :client, :xml_obj
12
+
13
+ def initialize(client)
14
+ @client = client
15
+ end
16
+
17
+ # params:
18
+ # - bucket_name
19
+ # - acl
20
+ def put_bucket(bucket_name, acl)
21
+ # get location
22
+ location = client.endpoint.split('.')[0]
23
+
24
+ # build payload xml
25
+ payload = Nokogiri::XML::Builder.new do
26
+ CreateBucketConfiguration do
27
+ LocationConstraint location
28
+ end
29
+ end
30
+
31
+ # set acl header
32
+ headers = Hash.new
33
+ headers['x-oss-acl'] = acl
34
+
35
+ # sign configs
36
+ sign_configs = Hash.new
37
+ sign_configs[:resource] = "/#{bucket_name}"
38
+ sign_configs[:oss_headers] = "x-oss-acl:#{acl}"
39
+ sign_configs[:content_type] = 'application/x-www-form-urlencoded'
40
+
41
+ @xml_obj = client.put(
42
+ host: "#{bucket_name}.#{client.endpoint}",
43
+ path: '/',
44
+ headers: headers,
45
+ payload: payload.to_xml,
46
+ sign_configs: sign_configs
47
+ )
48
+
49
+ true
50
+ end
51
+
52
+ # params:
53
+ # - bucket_name
54
+ # - acl
55
+ def put_bucket_acl(bucket_name, acl)
56
+ # set acl header
57
+ headers = Hash.new
58
+ headers['x-oss-acl'] = acl
59
+
60
+ # sign configs
61
+ sign_configs = Hash.new
62
+ sign_configs[:resource] = "/#{bucket_name}"
63
+ sign_configs[:oss_headers] = "x-oss-acl:#{acl}"
64
+ sign_configs[:content_type] = 'application/x-www-form-urlencoded'
65
+
66
+ @xml_obj = client.put(
67
+ host: "#{bucket_name}.#{client.endpoint}",
68
+ path: '/?acl',
69
+ headers: headers,
70
+ sign_configs: sign_configs
71
+ )
72
+
73
+ true
74
+ end
75
+
76
+ # params:
77
+ # - bucket_name
78
+ # - target_bucket
79
+ # - enable
80
+ # - target_prefix
81
+ def put_bucket_logging(bucket_name, target_bucket, enable = true, target_prefix = nil)
82
+ # sign configs
83
+ sign_configs = Hash.new
84
+ sign_configs[:resource] = "/#{bucket_name}"
85
+ sign_configs[:content_type] = 'application/x-www-form-urlencoded'
86
+
87
+ # build payload xml
88
+ payload = Nokogiri::XML::Builder.new do
89
+ BucketLoggingStatus do
90
+ if enable
91
+ LoggingEnabled do
92
+ TargetBucket target_bucket
93
+ TargetPrefix target_prefix unless target_prefix.nil?
94
+ end
95
+ end
96
+ end
97
+ end
98
+
99
+ @xml_obj = client.put(
100
+ host: "#{bucket_name}.#{client.endpoint}",
101
+ path: '/?logging',
102
+ sign_configs: sign_configs,
103
+ payload: payload.to_xml
104
+ )
105
+
106
+ true
107
+ end
108
+
109
+ # params:
110
+ # - bucket_name
111
+ # - index_doc
112
+ # - error_doc
113
+ def put_bucket_website(bucket_name, index_doc, error_doc)
114
+ # sign configs
115
+ sign_configs = Hash.new
116
+ sign_configs[:resource] = "/#{bucket_name}"
117
+ sign_configs[:content_type] = 'application/x-www-form-urlencoded'
118
+
119
+ # build payload xml
120
+ payload = Nokogiri::XML::Builder.new do
121
+ WebsiteConfiguration do
122
+ IndexDocument do
123
+ Suffix index_doc
124
+ end
125
+ ErrorDocument do
126
+ Key error_doc
127
+ end
128
+ end
129
+ end
130
+
131
+ @xml_obj = client.put(
132
+ host: "#{bucket_name}.#{client.endpoint}",
133
+ path: '/?website',
134
+ sign_configs: sign_configs,
135
+ payload: payload.to_xml
136
+ )
137
+
138
+ true
139
+ end
140
+
141
+ # params:
142
+ # - bucket_name
143
+ # - allow_empty
144
+ # - referer_list
145
+ def put_bucket_referer(bucket_name, allow_empty, referer_list = [])
146
+ # sign configs
147
+ sign_configs = Hash.new
148
+ sign_configs[:resource] = "/#{bucket_name}"
149
+ sign_configs[:content_type] = 'application/x-www-form-urlencoded'
150
+
151
+ # build payload xml
152
+ payload = Nokogiri::XML::Builder.new do
153
+ RefererConfiguration do
154
+ AllowEmptyReferer allow_empty.to_s
155
+ RefererList do
156
+ referer_list.each do |referer|
157
+ Referer referer
158
+ end
159
+ end
160
+ end
161
+ end
162
+
163
+ @xml_obj = client.put(
164
+ host: "#{bucket_name}.#{client.endpoint}",
165
+ path: '/?referer',
166
+ sign_configs: sign_configs,
167
+ payload: payload.to_xml
168
+ )
169
+
170
+ true
171
+ end
172
+
173
+ # params:
174
+ # - bucket_name
175
+ # - rules
176
+ # - prefix
177
+ # - id
178
+ # - status
179
+ # - days
180
+ # - date
181
+ def put_bucket_lifecycle(bucket_name, rules = [])
182
+ # sign configs
183
+ sign_configs = Hash.new
184
+ sign_configs[:resource] = "/#{bucket_name}"
185
+ sign_configs[:content_type] = 'application/x-www-form-urlencoded'
186
+
187
+ # build payload xml
188
+ payload = Nokogiri::XML::Builder.new do
189
+ LifecycleConfiguration do
190
+ rules.each do |rule|
191
+ Rule do
192
+ ID rule[:id] unless rule[:id].nil?
193
+ Prefix rule[:prefix]
194
+ Status rule[:status] ? 'Enabled' : 'Disabled'
195
+ Expiration do
196
+ if rule[:days] != nil
197
+ Days rule[:days]
198
+ else
199
+ Date rule[:date]
200
+ end
201
+ end
202
+ end
203
+ end
204
+ end
205
+ end
206
+
207
+ @xml_obj = client.put(
208
+ host: "#{bucket_name}.#{client.endpoint}",
209
+ path: '/?lifecycle',
210
+ sign_configs: sign_configs,
211
+ payload: payload.to_xml
212
+ )
213
+
214
+ true
215
+ end
216
+
217
+ # params:
218
+ # - options:
219
+ # - delimiter
220
+ # - prefix
221
+ # - marker
222
+ # - max_keys
223
+ # - encoding_type
224
+ def get_bucket(bucket_name, options = {})
225
+ # sign configs
226
+ sign_configs = Hash.new
227
+ sign_configs[:resource] = "/#{bucket_name}"
228
+
229
+ @xml_obj = client.get(
230
+ host: "#{bucket_name}.#{client.endpoint}",
231
+ path: '/',
232
+ sign_configs: sign_configs,
233
+ query_string: options
234
+ ).xpath('ListBucketResult')
235
+
236
+ self
237
+ end
238
+
239
+ def contents
240
+ contents = Array.new
241
+ xml_contents = @xml_obj.xpath('Contents')
242
+ xml_contents.each do |content|
243
+ contents << {
244
+ key: content.xpath('Key').text,
245
+ last_modified: content.xpath('LastModified').text,
246
+ etag: content.xpath('ETag').text,
247
+ type: content.xpath('Type').text,
248
+ size: content.xpath('Size').text,
249
+ storage_class: content.xpath('StorageClass').text,
250
+ owner: {
251
+ id: content.xpath('Owner/ID').text,
252
+ display_name: content.xpath('Owner/DisplayName').text
253
+ }
254
+ }
255
+ end
256
+ contents
257
+ end
258
+
259
+ # params:
260
+ # - bucket_name
261
+ def get_bucket_acl(bucket_name)
262
+ # sign configs
263
+ sign_configs = Hash.new
264
+ sign_configs[:resource] = "/#{bucket_name}"
265
+
266
+ xml_obj = client.get(
267
+ host: "#{bucket_name}.#{client.endpoint}",
268
+ path: '/?acl',
269
+ sign_configs: sign_configs
270
+ ).xpath('AccessControlPolicy')
271
+
272
+ {
273
+ grant: xml_obj.xpath('AccessControlList/Grant').text,
274
+ owner: {
275
+ id: xml_obj.xpath('Owner/ID').text,
276
+ display_name: xml_obj.xpath('Owner/DisplayName').text
277
+ }
278
+ }
279
+ end
280
+
281
+ # params:
282
+ # - bucket_name
283
+ def get_bucket_location(bucket_name)
284
+ # sign configs
285
+ sign_configs = Hash.new
286
+ sign_configs[:resource] = "/#{bucket_name}"
287
+
288
+ xml_obj = client.get(
289
+ host: "#{bucket_name}.#{client.endpoint}",
290
+ path: '/?location',
291
+ sign_configs: sign_configs
292
+ )
293
+
294
+ xml_obj.xpath('LocationConstraint').text
295
+ end
296
+
297
+ # params:
298
+ # - bucket_name
299
+ def get_bucket_logging(bucket_name)
300
+ # sign configs
301
+ sign_configs = Hash.new
302
+ sign_configs[:resource] = "/#{bucket_name}"
303
+
304
+ xml_obj = client.get(
305
+ host: "#{bucket_name}.#{client.endpoint}",
306
+ path: '/?logging',
307
+ sign_configs: sign_configs
308
+ ).xpath('BucketLoggingStatus/LoggingEnabled')
309
+
310
+ if xml_obj.length > 0
311
+ {
312
+ logging_enabled: true,
313
+ target_bucket: xml_obj.xpath('TargetBucket').text,
314
+ target_prefix: xml_obj.xpath('TargetPrefix').text,
315
+ }
316
+ else
317
+ { logging_enabled: false }
318
+ end
319
+ end
320
+
321
+ # params:
322
+ # - bucket_name
323
+ def get_bucket_website(bucket_name)
324
+ # sign configs
325
+ sign_configs = Hash.new
326
+ sign_configs[:resource] = "/#{bucket_name}"
327
+
328
+ xml_obj = client.get(
329
+ host: "#{bucket_name}.#{client.endpoint}",
330
+ path: '/?website',
331
+ sign_configs: sign_configs
332
+ ).xpath('WebsiteConfiguration')
333
+
334
+ {
335
+ index_doc: xml_obj.xpath('IndexDocument/Suffix').text,
336
+ error_doc: xml_obj.xpath('ErrorDocument/Key').text,
337
+ }
338
+ end
339
+
340
+ # params:
341
+ # - bucket_name
342
+ def get_bucket_referer(bucket_name)
343
+ # sign configs
344
+ sign_configs = Hash.new
345
+ sign_configs[:resource] = "/#{bucket_name}"
346
+
347
+ xml_obj = client.get(
348
+ host: "#{bucket_name}.#{client.endpoint}",
349
+ path: '/?referer',
350
+ sign_configs: sign_configs
351
+ ).xpath('RefererConfiguration')
352
+
353
+ referers = Array.new
354
+ xml_obj.xpath('RefererList/Referer').each do |referer|
355
+ referers << referer.text
356
+ end
357
+
358
+ {
359
+ allow_empty_referer: xml_obj.xpath('AllowEmptyReferer').text == 'true',
360
+ referers: referers,
361
+ }
362
+ end
363
+
364
+ # params:
365
+ # - bucket_name
366
+ def get_bucket_lifecycle(bucket_name)
367
+ # sign configs
368
+ sign_configs = Hash.new
369
+ sign_configs[:resource] = "/#{bucket_name}"
370
+
371
+ xml_obj = client.get(
372
+ host: "#{bucket_name}.#{client.endpoint}",
373
+ path: '/?lifecycle',
374
+ sign_configs: sign_configs
375
+ ).xpath('LifecycleConfiguration')
376
+
377
+ rules = Array.new
378
+ xml_obj.xpath('Rule').each do |xml_rule|
379
+ rule = {
380
+ id: xml_rule.xpath('ID').text,
381
+ prefix: xml_rule.xpath('Prefix').text,
382
+ status: xml_rule.xpath('Status').text == 'Enabled',
383
+ }
384
+ if xml_rule.xpath('Expiration/Days').length > 0
385
+ rule[:days] = xml_rule.xpath('Expiration/Days').text.to_i
386
+ else
387
+ rule[:date] = xml_rule.xpath('Expiration/Date').text
388
+ end
389
+ rules << rule
390
+ end
391
+
392
+ rules
393
+ end
394
+
395
+ # params:
396
+ # - bucket_name
397
+ def delete_bucket_logging(bucket_name)
398
+ # sign configs
399
+ sign_configs = Hash.new
400
+ sign_configs[:resource] = "/#{bucket_name}"
401
+
402
+ client.delete(
403
+ host: "#{bucket_name}.#{client.endpoint}",
404
+ path: '/?logging',
405
+ sign_configs: sign_configs
406
+ )
407
+
408
+ true
409
+ end
410
+
411
+ # params:
412
+ # - bucket_name
413
+ def delete_bucket_website(bucket_name)
414
+ # sign configs
415
+ sign_configs = Hash.new
416
+ sign_configs[:resource] = "/#{bucket_name}"
417
+
418
+ client.delete(
419
+ host: "#{bucket_name}.#{client.endpoint}",
420
+ path: '/?website',
421
+ sign_configs: sign_configs
422
+ )
423
+
424
+ true
425
+ end
426
+
427
+ # params:
428
+ # - bucket_name
429
+ def delete_bucket_lifecycle(bucket_name)
430
+ # sign configs
431
+ sign_configs = Hash.new
432
+ sign_configs[:resource] = "/#{bucket_name}"
433
+
434
+ client.delete(
435
+ host: "#{bucket_name}.#{client.endpoint}",
436
+ path: '/?lifecycle',
437
+ sign_configs: sign_configs
438
+ )
439
+
440
+ true
441
+ end
442
+
443
+ # params:
444
+ # - bucket_name
445
+ def delete_bucket(bucket_name)
446
+ # sign configs
447
+ sign_configs = Hash.new
448
+ sign_configs[:resource] = "/#{bucket_name}"
449
+
450
+ client.delete(
451
+ host: "#{bucket_name}.#{client.endpoint}",
452
+ path: '/?logging',
453
+ sign_configs: sign_configs
454
+ )
455
+
456
+ true
457
+ end
458
+
459
+
460
+ def method_missing(method)
461
+ if @xml_obj.nil?
462
+ super
463
+ else
464
+ camel = Util.camelize(method)
465
+ value = @xml_obj.xpath(camel)
466
+ raise "missing xml attribute #{camel}" if value.length == 0
467
+ value.inner_text
468
+ end
469
+ end
470
+
471
+ end
472
+
473
+ end