aliyun-sdk 0.7.3 → 0.8.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a27f8d07de2e2948e0af93983d9deee1fe09afcdc78be58f4387406061e19bc1
4
- data.tar.gz: 5f8ee19a0277de7a739221521621ef7be34f8a597f95b688819c737ec746dd1d
3
+ metadata.gz: 9ed62a6f5a59df2499c7fd4028b4aecfad8506018ef7a3425b1c7627e0f8ddc8
4
+ data.tar.gz: eb47336dccf3d526466e6cbf5fbf3a845c4f9b8be5d34a5b7f7a55cd6b87f693
5
5
  SHA512:
6
- metadata.gz: 5d63ecabcb9835ef73d8e1cb0bf4cebe94e8bdb16c3ea4f890ffcfe7be93edd9874a4b6c0efc175e7f545715b616e6e995cb193b8a16b8215bbfb601ccf4e65f
7
- data.tar.gz: 64a6931f66da611f9be61fa18ad172fe6c0ba69a4e8c0afd2f286fb2e9aa7b9d243da984cc1e1f98a8bfe035ddc198cf240114811b51a24c7b84c365def14753
6
+ metadata.gz: 4ee2ee99edbc2d8adbaa2d5dcf1cc2b661ee5430af90b8f5f95f6e4c7508fc878b4d83bc93c03d52efa8ea90796448e04992475ec8decc1756b886139d72c01a
7
+ data.tar.gz: 20d032b6e0c0537d2b6fbcc8d94aaba85e666c643a495bf2093418d731c5655c665a46523b17d11c9361edf683db418a9000e435e98afdaf53f4588a8c7bc794
@@ -1,8 +1,14 @@
1
1
  ## Change Log
2
2
 
3
+ ### v0.8.0 / 2020-08-17
4
+
5
+ - add bucket encryption
6
+ - add bucket versioning
7
+ - add env parameter to set default log level
8
+
3
9
  ### v0.7.3 / 2020-06-28
4
10
 
5
- - Add variable control log output path
11
+ - add variable control log output path
6
12
 
7
13
 
8
14
  ### v0.7.2 / 2020-06-05
@@ -37,11 +37,27 @@ module Aliyun
37
37
  @log_file ||= ENV["ALIYUN_OSS_SDK_LOG_PATH"]
38
38
  @logger = Logger.new(
39
39
  @log_file, MAX_NUM_LOG, ROTATE_SIZE)
40
- @logger.level = Logger::INFO
40
+ @logger.level = get_env_log_level || Logger::INFO
41
41
  end
42
42
  @logger
43
43
  end
44
44
 
45
+ def self.get_env_log_level
46
+ return unless ENV["ALIYUN_OSS_SDK_LOG_LEVEL"]
47
+ case ENV["ALIYUN_OSS_SDK_LOG_LEVEL"].upcase
48
+ when "DEBUG"
49
+ Logger::DEBUG
50
+ when "WARN"
51
+ Logger::WARN
52
+ when "ERROR"
53
+ Logger::ERROR
54
+ when "FATAL"
55
+ Logger::FATAL
56
+ when "UNKNOWN"
57
+ Logger::UNKNOWN
58
+ end
59
+ end
60
+
45
61
  end # logging
46
62
  end # Common
47
63
  end # Aliyun
@@ -47,6 +47,34 @@ module Aliyun
47
47
  end
48
48
  end
49
49
 
50
+ # 获取Bucket的versioning配置
51
+ # @return [BucketVersioning] Bucket的versioning配置
52
+ def versioning
53
+ @protocol.get_bucket_versioning(name)
54
+ end
55
+
56
+ # 设置Bucket的versioning配置
57
+ # @param versioning [BucketVersioning] versioning配置
58
+ def versioning=(versioning)
59
+ @protocol.put_bucket_versioning(name, versioning)
60
+ end
61
+
62
+ # 获取Bucket的encryption配置
63
+ # @return [BucketEncryption] Bucket的encryption配置
64
+ def encryption
65
+ @protocol.get_bucket_encryption(name)
66
+ end
67
+
68
+ # 设置Bucket的encryption配置
69
+ # @param encryption [BucketEncryption] encryption配置
70
+ def encryption=(encryption)
71
+ if encryption.enabled?
72
+ @protocol.put_bucket_encryption(name, encryption)
73
+ else
74
+ @protocol.delete_bucket_encryption(name)
75
+ end
76
+ end
77
+
50
78
  # 获取Bucket的website配置
51
79
  # @return [BucketWebsite] Bucket的website配置
52
80
  def website
@@ -212,6 +212,105 @@ module Aliyun
212
212
  logger.info("Done delete bucket logging")
213
213
  end
214
214
 
215
+ # Put bucket versioning settings
216
+ # @param name [String] the bucket name
217
+ # @param versioning [BucketVersioning] versioning options
218
+ def put_bucket_versioning(name, versioning)
219
+ logger.info("Begin put bucket versioning, "\
220
+ "name: #{name}, versioning: #{versioning}")
221
+
222
+ sub_res = {'versioning' => nil}
223
+ body = Nokogiri::XML::Builder.new do |xml|
224
+ xml.VersioningConfiguration {
225
+ xml.Status versioning.status
226
+ }
227
+ end.to_xml
228
+
229
+ @http.put(
230
+ {:bucket => name, :sub_res => sub_res},
231
+ {:body => body})
232
+
233
+ logger.info("Done put bucket versioning")
234
+ end
235
+
236
+ # Get bucket versioning settings
237
+ # @param name [String] the bucket name
238
+ # @return [BucketVersioning] versioning options of this bucket
239
+ def get_bucket_versioning(name)
240
+ logger.info("Begin get bucket versioning, name: #{name}")
241
+
242
+ sub_res = {'versioning' => nil}
243
+ r = @http.get({:bucket => name, :sub_res => sub_res})
244
+
245
+ doc = parse_xml(r.body)
246
+
247
+ versioning_node = doc.at_css("VersioningConfiguration")
248
+ opts = {
249
+ :status => get_node_text(versioning_node, 'Status')
250
+ }
251
+
252
+ logger.info("Done get bucket versioning")
253
+
254
+ BucketVersioning.new(opts)
255
+ end
256
+
257
+ # Put bucket encryption settings
258
+ # @param name [String] the bucket name
259
+ # @param encryption [BucketEncryption] encryption options
260
+ def put_bucket_encryption(name, encryption)
261
+ logger.info("Begin put bucket encryption, "\
262
+ "name: #{name}, encryption: #{encryption}")
263
+
264
+ sub_res = {'encryption' => nil}
265
+ body = Nokogiri::XML::Builder.new do |xml|
266
+ xml.ServerSideEncryptionRule {
267
+ xml.ApplyServerSideEncryptionByDefault {
268
+ xml.SSEAlgorithm encryption.sse_algorithm
269
+ xml.KMSMasterKeyID encryption.kms_master_key_id if encryption.kms_master_key_id
270
+ }
271
+ }
272
+ end.to_xml
273
+
274
+ @http.put(
275
+ {:bucket => name, :sub_res => sub_res},
276
+ {:body => body})
277
+
278
+ logger.info("Done put bucket encryption")
279
+ end
280
+
281
+ # Get bucket encryption settings
282
+ # @param name [String] the bucket name
283
+ # @return [BucketEncryption] encryption options of this bucket
284
+ def get_bucket_encryption(name)
285
+ logger.info("Begin get bucket encryption, name: #{name}")
286
+
287
+ sub_res = {'encryption' => nil}
288
+ r = @http.get({:bucket => name, :sub_res => sub_res})
289
+
290
+ doc = parse_xml(r.body)
291
+
292
+ encryption_node = doc.at_css("ApplyServerSideEncryptionByDefault")
293
+ opts = {
294
+ :sse_algorithm => get_node_text(encryption_node, 'SSEAlgorithm'),
295
+ :kms_master_key_id => get_node_text(encryption_node, 'KMSMasterKeyID')
296
+ }
297
+
298
+ logger.info("Done get bucket encryption")
299
+
300
+ BucketEncryption.new(opts)
301
+ end
302
+
303
+ # Delete bucket encryption settings, a.k.a. disable bucket encryption
304
+ # @param name [String] the bucket name
305
+ def delete_bucket_encryption(name)
306
+ logger.info("Begin delete bucket encryption, name: #{name}")
307
+
308
+ sub_res = {'encryption' => nil}
309
+ @http.delete({:bucket => name, :sub_res => sub_res})
310
+
311
+ logger.info("Done delete bucket encryption")
312
+ end
313
+
215
314
  # Put bucket website settings
216
315
  # @param name [String] the bucket name
217
316
  # @param website [BucketWebsite] the bucket website options
@@ -74,6 +74,31 @@ module Aliyun
74
74
  end
75
75
  end
76
76
 
77
+ ##
78
+ # Bucket Versioning setting. See: {http://help.aliyun.com/document_detail/oss/product-documentation/function/versioning.html OSS Bucket versioning}
79
+ # Attributes:
80
+ # * status [String] the bucket versioning status, can be 'Enabled' and 'Suspended'
81
+ # @example Enable bucket versioning
82
+ # bucket.versioning = BucketVersioning.new(:status => 'Enabled')
83
+ # @example Suspend bucket versioning
84
+ # bucket.versioning = BucketVersioning.new(:status => 'Suspended')
85
+ class BucketVersioning < Common::Struct::Base
86
+ attrs :status
87
+ end
88
+
89
+ ##
90
+ # Bucket Encryption setting. See: {http://help.aliyun.com/document_detail/oss/product-documentation/function/encryption.html OSS Bucket encryption}
91
+ # Attributes:
92
+ # * sse_algorithm [string] Indicates the default server-side encryption method
93
+ # * kms_master_key_id [string] Indicates the ID of CMK that is currently used.
94
+ class BucketEncryption < Common::Struct::Base
95
+ attrs :enable, :sse_algorithm, :kms_master_key_id
96
+
97
+ def enabled?
98
+ enable == true
99
+ end
100
+ end
101
+
77
102
  ##
78
103
  # Bucket website setting. See: {http://help.aliyun.com/document_detail/oss/product-documentation/function/host-static-website.html OSS Website hosting}
79
104
  # Attributes:
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Aliyun
4
4
 
5
- VERSION = "0.7.3"
5
+ VERSION = "0.8.0"
6
6
 
7
7
  end # Aliyun
@@ -96,6 +96,25 @@ module Aliyun
96
96
  end.to_xml
97
97
  end
98
98
 
99
+ def mock_versioning(opts)
100
+ Nokogiri::XML::Builder.new do |xml|
101
+ xml.VersioningConfiguration {
102
+ xml.Status opts.status
103
+ }
104
+ end.to_xml
105
+ end
106
+
107
+ def mock_encryption(opts)
108
+ Nokogiri::XML::Builder.new do |xml|
109
+ xml.ServerSideEncryptionRule {
110
+ xml.ApplyServerSideEncryptionByDefault {
111
+ xml.SSEAlgorithm opts.sse_algorithm
112
+ xml.KMSMasterKeyID opts.kms_master_key_id if opts.kms_master_key_id
113
+ }
114
+ }
115
+ end.to_xml
116
+ end
117
+
99
118
  def mock_website(opts)
100
119
  Nokogiri::XML::Builder.new do |xml|
101
120
  xml.WebsiteConfiguration {
@@ -354,7 +373,7 @@ module Aliyun
354
373
  end
355
374
  end # delete bucket
356
375
 
357
- context "acl, logging, website, referer, lifecycle" do
376
+ context "acl, logging, versioning, encryption, website, referer, lifecycle" do
358
377
  it "should update acl" do
359
378
  query = {'acl' => nil}
360
379
  stub_request(:put, request_path).with(:query => query)
@@ -376,6 +376,130 @@ module Aliyun
376
376
  .with(:query => query, :body => nil)
377
377
  end
378
378
 
379
+ it "should set versioning" do
380
+ query = {'versioning' => nil}
381
+
382
+ body = Nokogiri::XML::Builder.new do |xml|
383
+ xml.VersioningConfiguration {
384
+ xml.Status 'Enabled'
385
+ }
386
+ end.to_xml
387
+
388
+ stub_request(:put, bucket_url)
389
+ .with(:query => query)
390
+ .to_return(status: 200, :body => nil)
391
+
392
+ @bucket.versioning = BucketVersioning.new(:status => 'Enabled')
393
+
394
+ expect(WebMock).to have_requested(:put, bucket_url)
395
+ .with(:query => query, :body => body)
396
+ end
397
+
398
+ it "should get versioning" do
399
+ query = {'versioning' => nil}
400
+
401
+ body = Nokogiri::XML::Builder.new do |xml|
402
+ xml.VersioningConfiguration {
403
+ xml.Status 'Enabled'
404
+ }
405
+ end.to_xml
406
+
407
+ stub_request(:get, bucket_url)
408
+ .with(:query => query)
409
+ .to_return(status: 200, :body => body)
410
+
411
+ ret = @bucket.versioning
412
+
413
+ expect(WebMock).to have_requested(:get, bucket_url)
414
+ .with(:query => query, :body => nil)
415
+
416
+ expect(ret.status).to eq('Enabled')
417
+ end
418
+
419
+ it "should set encryption with aes256" do
420
+ query = {'encryption' => nil}
421
+
422
+ body = Nokogiri::XML::Builder.new do |xml|
423
+ xml.ServerSideEncryptionRule {
424
+ xml.ApplyServerSideEncryptionByDefault {
425
+ xml.SSEAlgorithm 'AES256'
426
+ }
427
+ }
428
+ end.to_xml
429
+
430
+ stub_request(:put, bucket_url)
431
+ .with(:query => query)
432
+ .to_return(status: 200, :body => nil)
433
+
434
+ @bucket.encryption = BucketEncryption.new(:enable => true, :sse_algorithm => 'AES256')
435
+
436
+ expect(WebMock).to have_requested(:put, bucket_url)
437
+ .with(:query => query, :body => body)
438
+ end
439
+
440
+ it "should set encryption with KMS" do
441
+ query = {'encryption' => nil}
442
+
443
+ body = Nokogiri::XML::Builder.new do |xml|
444
+ xml.ServerSideEncryptionRule {
445
+ xml.ApplyServerSideEncryptionByDefault {
446
+ xml.SSEAlgorithm 'KMS'
447
+ xml.KMSMasterKeyID 'kms-id'
448
+ }
449
+ }
450
+ end.to_xml
451
+
452
+ stub_request(:put, bucket_url)
453
+ .with(:query => query)
454
+ .to_return(status: 200, :body => nil)
455
+
456
+ @bucket.encryption = BucketEncryption.new(
457
+ :enable => true,
458
+ :sse_algorithm => 'KMS',
459
+ :kms_master_key_id => 'kms-id')
460
+
461
+ expect(WebMock).to have_requested(:put, bucket_url)
462
+ .with(:query => query, :body => body)
463
+ end
464
+
465
+ it "should get encryption" do
466
+ query = {'encryption' => nil}
467
+
468
+ body = Nokogiri::XML::Builder.new do |xml|
469
+ xml.ServerSideEncryptionRule {
470
+ xml.ApplyServerSideEncryptionByDefault {
471
+ xml.SSEAlgorithm 'KMS'
472
+ xml.KMSMasterKeyID 'kms-id'
473
+ }
474
+ }
475
+ end.to_xml
476
+
477
+ stub_request(:get, bucket_url)
478
+ .with(:query => query)
479
+ .to_return(status: 200, :body => body)
480
+
481
+ ret = @bucket.encryption
482
+
483
+ expect(WebMock).to have_requested(:get, bucket_url)
484
+ .with(:query => query, :body => nil)
485
+
486
+ expect(ret.sse_algorithm).to eq('KMS')
487
+ expect(ret.kms_master_key_id).to eq('kms-id')
488
+ end
489
+
490
+ it "should delete encryption" do
491
+ query = {'encryption' => nil}
492
+
493
+ stub_request(:delete, bucket_url)
494
+ .with(:query => query)
495
+ .to_return(status: 204, :body => nil)
496
+
497
+ @bucket.encryption = BucketEncryption.new(:enable => false)
498
+
499
+ expect(WebMock).to have_requested(:delete, bucket_url)
500
+ .with(:query => query, :body => nil)
501
+ end
502
+
379
503
  it "should get bucket url" do
380
504
  expect(@bucket.bucket_url)
381
505
  .to eq('http://rubysdk-bucket.oss-cn-hangzhou.aliyuncs.com/')
@@ -0,0 +1,73 @@
1
+ require 'minitest/autorun'
2
+ require 'yaml'
3
+ $LOAD_PATH.unshift(File.expand_path("../../lib", __FILE__))
4
+ require 'aliyun/oss'
5
+ require 'time'
6
+ require_relative 'config'
7
+
8
+ class TestBucket < Minitest::Test
9
+ def setup
10
+ Aliyun::Common::Logging.set_log_level(Logger::DEBUG)
11
+ @client = Aliyun::OSS::Client.new(TestConf.creds)
12
+ @bucket_name = TestConf.bucket + Time.now.to_i.to_s
13
+ @client.create_bucket(@bucket_name)
14
+ @bucket = @client.get_bucket(@bucket_name)
15
+ end
16
+
17
+ def teardown
18
+ @client.delete_bucket(@bucket_name)
19
+ end
20
+
21
+ def test_bucket_versioning
22
+ ret = @bucket.versioning
23
+ assert_nil ret.status
24
+
25
+ @bucket.versioning = Aliyun::OSS::BucketVersioning.new(:status => 'Enabled')
26
+ ret = @bucket.versioning
27
+ assert_equal 'Enabled', ret.status
28
+
29
+ @bucket.versioning = Aliyun::OSS::BucketVersioning.new(:status => 'Suspended')
30
+ ret = @bucket.versioning
31
+ assert_equal 'Suspended', ret.status
32
+
33
+ end
34
+
35
+ def test_bucket_encryption
36
+
37
+ begin
38
+ ret = @bucket.encryption
39
+ assert_raises "should not here"
40
+ rescue => exception
41
+ end
42
+
43
+ @bucket.encryption = Aliyun::OSS::BucketEncryption.new(
44
+ :enable => true,
45
+ :sse_algorithm => 'KMS')
46
+ ret = @bucket.encryption
47
+ assert_equal 'KMS', ret.sse_algorithm
48
+ assert_nil ret.kms_master_key_id
49
+
50
+ @bucket.encryption = Aliyun::OSS::BucketEncryption.new(
51
+ :enable => true,
52
+ :sse_algorithm => 'KMS',
53
+ :kms_master_key_id => 'kms-id')
54
+ ret = @bucket.encryption
55
+ assert_equal 'KMS', ret.sse_algorithm
56
+ assert_equal 'kms-id', ret.kms_master_key_id
57
+
58
+ @bucket.encryption = Aliyun::OSS::BucketEncryption.new(
59
+ :enable => true,
60
+ :sse_algorithm => 'AES256')
61
+ ret = @bucket.encryption
62
+ assert_equal 'AES256', ret.sse_algorithm
63
+ assert_nil ret.kms_master_key_id
64
+
65
+ @bucket.encryption = Aliyun::OSS::BucketEncryption.new(
66
+ :enable => false)
67
+ begin
68
+ ret = @bucket.encryption
69
+ assert_raises "should not here"
70
+ rescue => exception
71
+ end
72
+ end
73
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: aliyun-sdk
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.3
4
+ version: 0.8.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tianlong Wu
8
8
  autorequire:
9
9
  bindir: lib/aliyun
10
10
  cert_chain: []
11
- date: 2020-06-28 00:00:00.000000000 Z
11
+ date: 2020-08-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: nokogiri
@@ -200,6 +200,7 @@ files:
200
200
  - spec/aliyun/sts/util_spec.rb
201
201
  - tests/config.rb
202
202
  - tests/helper.rb
203
+ - tests/test_bucket.rb
203
204
  - tests/test_content_encoding.rb
204
205
  - tests/test_content_type.rb
205
206
  - tests/test_crc_check.rb
@@ -249,6 +250,7 @@ test_files:
249
250
  - spec/aliyun/sts/util_spec.rb
250
251
  - tests/config.rb
251
252
  - tests/helper.rb
253
+ - tests/test_bucket.rb
252
254
  - tests/test_content_encoding.rb
253
255
  - tests/test_content_type.rb
254
256
  - tests/test_crc_check.rb