aliyun-sdk 0.7.1 → 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: aea256f156d30bf7e84ed0f6a2cd0670b63c02fe1fc6a6f9641e479dc2dfcbf5
4
- data.tar.gz: 1344db0e4aa351fc9065779ef80297857a57e48fdf5d41527dd5ce3cb62c17cb
3
+ metadata.gz: 9ed62a6f5a59df2499c7fd4028b4aecfad8506018ef7a3425b1c7627e0f8ddc8
4
+ data.tar.gz: eb47336dccf3d526466e6cbf5fbf3a845c4f9b8be5d34a5b7f7a55cd6b87f693
5
5
  SHA512:
6
- metadata.gz: 53387168b68b6b209f8a2c83e7704c0b0fa5bc02cb7dcd384c19bfe357eece672a5e9b2e566e2da261d1133d5a99280455e74eb71ef3aa09158659948b3fb2bf
7
- data.tar.gz: 93615f26a714a0daf08cfe18addd2fc25bc5e1d028ec53ab8302f6aeabd4e4b05dc59f254cc823faeba2d846f0b0b8e5a74736fc9348ee1ee6d88366b100cc84
6
+ metadata.gz: 4ee2ee99edbc2d8adbaa2d5dcf1cc2b661ee5430af90b8f5f95f6e4c7508fc878b4d83bc93c03d52efa8ea90796448e04992475ec8decc1756b886139d72c01a
7
+ data.tar.gz: 20d032b6e0c0537d2b6fbcc8d94aaba85e666c643a495bf2093418d731c5655c665a46523b17d11c9361edf683db418a9000e435e98afdaf53f4588a8c7bc794
data/CHANGELOG.md CHANGED
@@ -1,5 +1,20 @@
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
+
9
+ ### v0.7.3 / 2020-06-28
10
+
11
+ - add variable control log output path
12
+
13
+
14
+ ### v0.7.2 / 2020-06-05
15
+
16
+ - add env parameter to descide whether output log file
17
+
3
18
  ### v0.7.1 / 2019-11-16
4
19
 
5
20
  - add the validity check of bucket name
@@ -11,7 +11,6 @@ module Aliyun
11
11
  # logger.info(xxx)
12
12
  module Logging
13
13
 
14
- DEFAULT_LOG_FILE = "./aliyun_sdk.log"
15
14
  MAX_NUM_LOG = 100
16
15
  ROTATE_SIZE = 10 * 1024 * 1024
17
16
 
@@ -34,13 +33,31 @@ module Aliyun
34
33
 
35
34
  def self.logger
36
35
  unless @logger
36
+ # Environment parameter ALIYUN_OSS_SDK_LOG_PATH used to set output log to a file,do not output log if not set
37
+ @log_file ||= ENV["ALIYUN_OSS_SDK_LOG_PATH"]
37
38
  @logger = Logger.new(
38
- @log_file ||= DEFAULT_LOG_FILE, MAX_NUM_LOG, ROTATE_SIZE)
39
- @logger.level = Logger::INFO
39
+ @log_file, MAX_NUM_LOG, ROTATE_SIZE)
40
+ @logger.level = get_env_log_level || Logger::INFO
40
41
  end
41
42
  @logger
42
43
  end
43
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
+
44
61
  end # logging
45
62
  end # Common
46
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.1"
5
+ VERSION = "0.8.0"
6
6
 
7
7
  end # Aliyun
@@ -89,13 +89,32 @@ module Aliyun
89
89
  if opts.enabled?
90
90
  xml.LoggingEnabled {
91
91
  xml.TargetBucket opts.target_bucket
92
- xml.TargetPrefix opts.target_prefix
92
+ xml.TargetPrefix opts.target_prefix if opts.target_prefix
93
93
  }
94
94
  end
95
95
  }
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)
@@ -392,6 +411,19 @@ module Aliyun
392
411
  .with(:query => query, :body => mock_logging(logging_opts))
393
412
  end
394
413
 
414
+ it "should enable logging without target prefix" do
415
+ query = {'logging' => nil}
416
+ stub_request(:put, request_path).with(:query => query)
417
+
418
+ logging_opts = BucketLogging.new(
419
+ :enable => true,
420
+ :target_bucket => 'target-bucket')
421
+ @protocol.put_bucket_logging(@bucket, logging_opts)
422
+
423
+ expect(WebMock).to have_requested(:put, request_path)
424
+ .with(:query => query, :body => mock_logging(logging_opts))
425
+ end
426
+
395
427
  it "should disable logging" do
396
428
  query = {'logging' => nil}
397
429
  stub_request(:put, request_path).with(:query => query)
@@ -420,6 +452,23 @@ module Aliyun
420
452
  expect(logging.to_s).to eq(logging_opts.to_s)
421
453
  end
422
454
 
455
+ it "should get logging without target-bucket" do
456
+ query = {'logging' => nil}
457
+ logging_opts = BucketLogging.new(
458
+ :enable => false,
459
+ :target_bucket => nil, :target_prefix => nil)
460
+
461
+ stub_request(:get, request_path)
462
+ .with(:query => query)
463
+ .to_return(:body => mock_logging(logging_opts))
464
+
465
+ logging = @protocol.get_bucket_logging(@bucket)
466
+
467
+ expect(WebMock).to have_requested(:get, request_path)
468
+ .with(:query => query, :body => nil)
469
+ expect(logging.to_s).to eq(logging_opts.to_s)
470
+ end
471
+
423
472
  it "should delete logging" do
424
473
  query = {'logging' => nil}
425
474
  stub_request(:delete, request_path).with(:query => query)
@@ -430,6 +479,18 @@ module Aliyun
430
479
  .with(:query => query, :body => nil)
431
480
  end
432
481
 
482
+ it "should raise Exception when enable logging" do
483
+ query = {'logging' => nil}
484
+ stub_request(:put, request_path).with(:query => query)
485
+
486
+ logging_opts = BucketLogging.new(
487
+ :enable => true,
488
+ :target_bucket => nil, :target_prefix => nil)
489
+ expect {
490
+ @protocol.put_bucket_logging(@bucket, logging_opts)
491
+ }.to raise_error(ClientError)
492
+ end
493
+
433
494
  it "should update website" do
434
495
  query = {'website' => nil}
435
496
  stub_request(:put, request_path).with(:query => query)
@@ -442,6 +503,18 @@ module Aliyun
442
503
  .with(:query => query, :body => mock_website(website_opts))
443
504
  end
444
505
 
506
+ it "should update website with index only" do
507
+ query = {'website' => nil}
508
+ stub_request(:put, request_path).with(:query => query)
509
+
510
+ website_opts = BucketWebsite.new(
511
+ :enable => true, :index => 'index.html', :error => nil)
512
+ @protocol.put_bucket_website(@bucket, website_opts)
513
+
514
+ expect(WebMock).to have_requested(:put, request_path)
515
+ .with(:query => query, :body => mock_website(website_opts))
516
+ end
517
+
445
518
  it "should get website" do
446
519
  query = {'website' => nil}
447
520
  website_opts = BucketWebsite.new(
@@ -468,6 +541,18 @@ module Aliyun
468
541
  .with(:query => query, :body => nil)
469
542
  end
470
543
 
544
+ it "should raise Exception when update website" do
545
+ query = {'website' => nil}
546
+ stub_request(:put, request_path).with(:query => query)
547
+
548
+ website_opts = BucketWebsite.new(
549
+ :enable => true, :index => nil)
550
+
551
+ expect {
552
+ @protocol.put_bucket_website(@bucket, website_opts)
553
+ }.to raise_error(ClientError)
554
+ end
555
+
471
556
  it "should update referer" do
472
557
  query = {'referer' => nil}
473
558
  stub_request(:put, request_path).with(:query => query)
@@ -541,6 +626,44 @@ module Aliyun
541
626
  .with(:query => query, :body => nil)
542
627
  end
543
628
 
629
+ it "should raise Exception when update lifecycle " do
630
+ query = {'lifecycle' => nil}
631
+ stub_request(:put, request_path).with(:query => query)
632
+
633
+ rules = (1..5).map do |i|
634
+ LifeCycleRule.new(
635
+ :id => i, :enable => i % 2 == 0, :prefix => "foo#{i}",
636
+ :expiry => 'invalid')
637
+ end
638
+
639
+ expect {
640
+ @protocol.put_bucket_lifecycle(@bucket, rules)
641
+ }.to raise_error(ClientError)
642
+ end
643
+
644
+ it "should raise Exception when get lifecycle " do
645
+ query = {'lifecycle' => nil}
646
+ body = '<LifecycleConfiguration>
647
+ <Rule>
648
+ <ID>delete after one day</ID>
649
+ <Prefix>logs/</Prefix>
650
+ <Status>Enabled</Status>
651
+ <Expiration>
652
+ <Days>1</Days>
653
+ <Date>2017-01-01T00:00:00.000Z</Date>
654
+ </Expiration>
655
+ </Rule>
656
+ </LifecycleConfiguration>'
657
+
658
+ stub_request(:get, request_path)
659
+ .with(:query => query)
660
+ .to_return(:body => body)
661
+
662
+ expect {
663
+ rules = @protocol.get_bucket_lifecycle(@bucket)
664
+ }.to raise_error(ClientError)
665
+ end
666
+
544
667
  it "should set cors" do
545
668
  query = {'cors' => nil}
546
669
  stub_request(:put, request_path).with(:query => query)
@@ -558,6 +681,24 @@ module Aliyun
558
681
  .with(:query => query, :body => mock_cors(rules))
559
682
  end
560
683
 
684
+ it "should set cors with MaxAgeSeconds " do
685
+ query = {'cors' => nil}
686
+ stub_request(:put, request_path).with(:query => query)
687
+
688
+ rules = (1..5).map do |i|
689
+ CORSRule.new(
690
+ :allowed_origins => (1..3).map {|x| "origin-#{x}"},
691
+ :allowed_methods => ['PUT', 'GET'],
692
+ :allowed_headers => (1..3).map {|x| "header-#{x}"},
693
+ :expose_headers => (1..3).map {|x| "header-#{x}"},
694
+ :max_age_seconds => 5)
695
+ end
696
+ @protocol.set_bucket_cors(@bucket, rules)
697
+
698
+ expect(WebMock).to have_requested(:put, request_path)
699
+ .with(:query => query, :body => mock_cors(rules))
700
+ end
701
+
561
702
  it "should get cors" do
562
703
  query = {'cors' => nil}
563
704
  return_rules = (1..5).map do |i|
@@ -156,6 +156,350 @@ module Aliyun
156
156
  .with(:query => query, :body => nil)
157
157
  end
158
158
 
159
+ it "should set logging setting" do
160
+ query = {'logging' => nil}
161
+
162
+ body = Nokogiri::XML::Builder.new do |xml|
163
+ xml.BucketLoggingStatus {
164
+ xml.LoggingEnabled {
165
+ xml.TargetBucket 'target-bucket'
166
+ }
167
+ }
168
+ end.to_xml
169
+
170
+ stub_request(:put, bucket_url).with(:query => query)
171
+
172
+ @bucket.logging = BucketLogging.new(:enable => true, :target_bucket => 'target-bucket')
173
+
174
+ expect(WebMock).to have_requested(:put, bucket_url)
175
+ .with(:query => query, :body => body)
176
+ end
177
+
178
+ it "should get logging setting" do
179
+ query = {'logging' => nil}
180
+
181
+ retbody = Nokogiri::XML::Builder.new do |xml|
182
+ xml.BucketLoggingStatus {
183
+ xml.LoggingEnabled {
184
+ xml.TargetBucket 'target-bucket'
185
+ xml.TargetPrefix 'target-prefix'
186
+ }
187
+ }
188
+ end.to_xml
189
+
190
+ stub_request(:get, bucket_url)
191
+ .with(:query => query)
192
+ .to_return(status: 200, :body => retbody)
193
+
194
+ logging = @bucket.logging
195
+
196
+ expect(WebMock).to have_requested(:get, bucket_url)
197
+ .with(:query => query, :body => nil)
198
+ expect(logging.enable).to eq(true)
199
+ expect(logging.target_bucket).to eq('target-bucket')
200
+
201
+ end
202
+
203
+ it "should set webseit" do
204
+ query = {'website' => nil}
205
+
206
+ body = Nokogiri::XML::Builder.new do |xml|
207
+ xml.WebsiteConfiguration {
208
+ xml.IndexDocument {
209
+ xml.Suffix 'index.html'
210
+ }
211
+ }
212
+ end.to_xml
213
+
214
+ stub_request(:put, bucket_url)
215
+ .with(:query => query)
216
+ .to_return(status: 200, :body => nil)
217
+
218
+ @bucket.website = BucketWebsite.new(:enable => true, :index => 'index.html')
219
+
220
+ expect(WebMock).to have_requested(:put, bucket_url)
221
+ .with(:query => query, :body => body)
222
+ end
223
+
224
+ it "should delete webseit" do
225
+ query = {'website' => nil}
226
+
227
+ stub_request(:delete, bucket_url)
228
+ .with(:query => query)
229
+ .to_return(status: 204, :body => nil)
230
+
231
+ @bucket.website = BucketWebsite.new(:enable => false)
232
+
233
+ expect(WebMock).to have_requested(:delete, bucket_url)
234
+ .with(:query => query, :body => nil)
235
+ end
236
+
237
+ it "should get webseit" do
238
+ query = {'website' => nil}
239
+
240
+ body = Nokogiri::XML::Builder.new do |xml|
241
+ xml.WebsiteConfiguration {
242
+ xml.IndexDocument {
243
+ xml.Suffix 'index.html'
244
+ }
245
+ xml.ErrorDocument {
246
+ xml.Key 'error.html'
247
+ }
248
+ }
249
+ end.to_xml
250
+
251
+ stub_request(:get, bucket_url)
252
+ .with(:query => query)
253
+ .to_return(status: 200, :body => body)
254
+
255
+ website = @bucket.website
256
+
257
+ expect(WebMock).to have_requested(:get, bucket_url)
258
+ .with(:query => query, :body => nil)
259
+
260
+ expect(website.enable).to eq(true)
261
+ expect(website.index).to eq('index.html')
262
+ expect(website.error).to eq('error.html')
263
+ end
264
+
265
+ it "should set referer" do
266
+ query = {'referer' => nil}
267
+
268
+ body = Nokogiri::XML::Builder.new do |xml|
269
+ xml.RefererConfiguration {
270
+ xml.AllowEmptyReferer 'true'
271
+ xml.RefererList {
272
+ xml.Referer 'http://www.aliyun.com'
273
+ }
274
+ }
275
+ end.to_xml
276
+
277
+ stub_request(:put, bucket_url)
278
+ .with(:query => query)
279
+ .to_return(status: 200, :body => nil)
280
+
281
+ @bucket.referer = BucketReferer.new(:allow_empty => true, :whitelist => ['http://www.aliyun.com'])
282
+
283
+ expect(WebMock).to have_requested(:put, bucket_url)
284
+ .with(:query => query, :body => body)
285
+ end
286
+
287
+ it "should get referer" do
288
+ query = {'referer' => nil}
289
+
290
+ body = Nokogiri::XML::Builder.new do |xml|
291
+ xml.RefererConfiguration {
292
+ xml.AllowEmptyReferer 'true'
293
+ xml.RefererList {
294
+ xml.Referer 'http://www.aliyun.com'
295
+ }
296
+ }
297
+ end.to_xml
298
+
299
+ stub_request(:get, bucket_url)
300
+ .with(:query => query)
301
+ .to_return(status: 200, :body => body)
302
+
303
+ referer = @bucket.referer
304
+
305
+ expect(WebMock).to have_requested(:get, bucket_url)
306
+ .with(:query => query, :body => nil)
307
+
308
+ expect(referer.allow_empty).to eq(true)
309
+ expect(referer.whitelist).to eq(['http://www.aliyun.com'])
310
+ end
311
+
312
+ it "should set cors" do
313
+ query = {'cors' => nil}
314
+
315
+ body = Nokogiri::XML::Builder.new do |xml|
316
+ xml.CORSConfiguration {
317
+ xml.CORSRule {
318
+ xml.AllowedOrigin '*'
319
+ xml.AllowedMethod 'PUT'
320
+ xml.AllowedHeader 'Authorization'
321
+ }
322
+ }
323
+ end.to_xml
324
+
325
+ stub_request(:put, bucket_url)
326
+ .with(:query => query)
327
+ .to_return(status: 200, :body => nil)
328
+
329
+ rules = [
330
+ CORSRule.new(
331
+ :allowed_origins => ['*'],
332
+ :allowed_methods => ['PUT'],
333
+ :allowed_headers => ['Authorization'],
334
+ :expose_headers =>[])
335
+ ]
336
+
337
+ @bucket.cors = rules
338
+
339
+ expect(WebMock).to have_requested(:put, bucket_url)
340
+ .with(:query => query, :body => body)
341
+ end
342
+
343
+ it "should delete cors" do
344
+ query = {'cors' => nil}
345
+
346
+ stub_request(:delete, bucket_url)
347
+ .with(:query => query)
348
+ .to_return(status: 204, :body => nil)
349
+
350
+ @bucket.cors = []
351
+
352
+ expect(WebMock).to have_requested(:delete, bucket_url)
353
+ .with(:query => query, :body => nil)
354
+ end
355
+
356
+ it "should get cors" do
357
+ query = {'cors' => nil}
358
+
359
+ body = Nokogiri::XML::Builder.new do |xml|
360
+ xml.CORSConfiguration {
361
+ xml.CORSRule {
362
+ xml.AllowedOrigin '*'
363
+ xml.AllowedMethod 'PUT'
364
+ xml.AllowedHeader 'Authorization'
365
+ }
366
+ }
367
+ end.to_xml
368
+
369
+ stub_request(:get, bucket_url)
370
+ .with(:query => query)
371
+ .to_return(status: 200, :body => body)
372
+
373
+ cors = @bucket.cors
374
+
375
+ expect(WebMock).to have_requested(:get, bucket_url)
376
+ .with(:query => query, :body => nil)
377
+ end
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
+
159
503
  it "should get bucket url" do
160
504
  expect(@bucket.bucket_url)
161
505
  .to eq('http://rubysdk-bucket.oss-cn-hangzhou.aliyuncs.com/')
@@ -611,6 +611,15 @@ module Aliyun
611
611
  })
612
612
  end
613
613
 
614
+ it "should raise Exception on error when setting invalid range" do
615
+ object_name = 'ruby'
616
+ url = get_request_path(object_name)
617
+ stub_request(:get, url)
618
+ expect {
619
+ @protocol.get_object(@bucket, object_name, {:range => [0, 10, 5]}) {}
620
+ }.to raise_error(ClientError)
621
+ end
622
+
614
623
  it "should match modify time and etag" do
615
624
  object_name = 'ruby'
616
625
  url = get_request_path(object_name)
@@ -662,6 +671,22 @@ module Aliyun
662
671
  .with(:body => nil, :query => query)
663
672
  end
664
673
 
674
+ it "should get object with headers" do
675
+ object_name = 'ruby'
676
+ url = get_request_path(object_name)
677
+ headers = {
678
+ 'Range' => 'bytes=0-9'
679
+ }
680
+ stub_request(:get, url)
681
+
682
+ @protocol.get_object(@bucket, object_name, {:headers => headers}) {}
683
+
684
+ expect(WebMock).to have_requested(:get, url)
685
+ .with(:body => nil, :query => {},
686
+ :headers => {
687
+ 'Range' => 'bytes=0-9'
688
+ })
689
+ end
665
690
 
666
691
  it "should raise crc exception on error" do
667
692
  object_name = 'ruby'
@@ -821,7 +846,7 @@ module Aliyun
821
846
 
822
847
  it "should batch delete objects" do
823
848
  url = get_request_path
824
- query = {'delete' => nil, 'encoding-type' => KeyEncoding::URL}
849
+ query = {'delete' => nil}
825
850
 
826
851
  object_names = (1..5).map do |i|
827
852
  "object-#{i}"
@@ -831,7 +856,7 @@ module Aliyun
831
856
  .with(:query => query)
832
857
  .to_return(:body => mock_delete_result(object_names))
833
858
 
834
- opts = {:quiet => false, :encoding => KeyEncoding::URL}
859
+ opts = {:quiet => false}
835
860
  deleted = @protocol.batch_delete_objects(@bucket, object_names, opts)
836
861
 
837
862
  expect(WebMock).to have_requested(:post, url)
@@ -861,6 +886,57 @@ module Aliyun
861
886
  .with(:query => query, :body => mock_delete(object_names, opts))
862
887
  expect(deleted).to match_array(object_names)
863
888
  end
889
+
890
+ it "should batch delete objects in quiet mode" do
891
+ url = get_request_path
892
+ query = {'delete' => nil}
893
+
894
+ object_names = (1..5).map do |i|
895
+ "object-#{i}"
896
+ end
897
+
898
+ stub_request(:post, url)
899
+ .with(:query => query)
900
+ .to_return(:body => "")
901
+
902
+ opts = {:quiet => true}
903
+ deleted = @protocol.batch_delete_objects(@bucket, object_names, opts)
904
+
905
+ expect(WebMock).to have_requested(:post, url)
906
+ .with(:query => query, :body => mock_delete(object_names, opts))
907
+ expect(deleted).to match_array([])
908
+ end
909
+
910
+ it "should rasie Exception wiht invalid responsed body" do
911
+ url = get_request_path
912
+ query = {'delete' => nil}
913
+ body = '<DeleteResult>
914
+ <EncodingType>invaid<EncodingType>
915
+ <Deleted>
916
+ <Key>multipart.data</Key>
917
+ </Deleted>
918
+ <Deleted>
919
+ <Key>test.jpg</Key>
920
+ </Deleted>
921
+ <Deleted>
922
+ <Key>demo.jpg</Key>
923
+ </Deleted>
924
+ </DeleteResult>'
925
+
926
+ object_names = (1..5).map do |i|
927
+ "object-#{i}"
928
+ end
929
+
930
+ stub_request(:post, url)
931
+ .with(:query => query)
932
+ .to_return(:body => body)
933
+
934
+ opts = {:quiet => false}
935
+ expect {
936
+ deleted = @protocol.batch_delete_objects(@bucket, object_names, opts)
937
+ }.to raise_error(ClientError)
938
+
939
+ end
864
940
  end # delete object
865
941
 
866
942
  context "acl" do
@@ -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.1
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: 2019-11-16 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