aliyun-oss-ruby-sdk 0.4.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (61) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +95 -0
  3. data/README.md +423 -0
  4. data/examples/aliyun/oss/bucket.rb +144 -0
  5. data/examples/aliyun/oss/callback.rb +61 -0
  6. data/examples/aliyun/oss/object.rb +182 -0
  7. data/examples/aliyun/oss/resumable_download.rb +42 -0
  8. data/examples/aliyun/oss/resumable_upload.rb +49 -0
  9. data/examples/aliyun/oss/streaming.rb +124 -0
  10. data/examples/aliyun/oss/using_sts.rb +48 -0
  11. data/examples/aliyun/sts/assume_role.rb +59 -0
  12. data/lib/aliyun_sdk/common.rb +6 -0
  13. data/lib/aliyun_sdk/common/exception.rb +18 -0
  14. data/lib/aliyun_sdk/common/logging.rb +46 -0
  15. data/lib/aliyun_sdk/common/struct.rb +56 -0
  16. data/lib/aliyun_sdk/oss.rb +16 -0
  17. data/lib/aliyun_sdk/oss/bucket.rb +661 -0
  18. data/lib/aliyun_sdk/oss/client.rb +106 -0
  19. data/lib/aliyun_sdk/oss/config.rb +39 -0
  20. data/lib/aliyun_sdk/oss/download.rb +255 -0
  21. data/lib/aliyun_sdk/oss/exception.rb +108 -0
  22. data/lib/aliyun_sdk/oss/http.rb +338 -0
  23. data/lib/aliyun_sdk/oss/iterator.rb +92 -0
  24. data/lib/aliyun_sdk/oss/multipart.rb +74 -0
  25. data/lib/aliyun_sdk/oss/object.rb +15 -0
  26. data/lib/aliyun_sdk/oss/protocol.rb +1499 -0
  27. data/lib/aliyun_sdk/oss/struct.rb +208 -0
  28. data/lib/aliyun_sdk/oss/upload.rb +238 -0
  29. data/lib/aliyun_sdk/oss/util.rb +89 -0
  30. data/lib/aliyun_sdk/sts.rb +9 -0
  31. data/lib/aliyun_sdk/sts/client.rb +38 -0
  32. data/lib/aliyun_sdk/sts/config.rb +22 -0
  33. data/lib/aliyun_sdk/sts/exception.rb +53 -0
  34. data/lib/aliyun_sdk/sts/protocol.rb +130 -0
  35. data/lib/aliyun_sdk/sts/struct.rb +64 -0
  36. data/lib/aliyun_sdk/sts/util.rb +48 -0
  37. data/lib/aliyun_sdk/version.rb +7 -0
  38. data/spec/aliyun/oss/bucket_spec.rb +597 -0
  39. data/spec/aliyun/oss/client/bucket_spec.rb +554 -0
  40. data/spec/aliyun/oss/client/client_spec.rb +297 -0
  41. data/spec/aliyun/oss/client/resumable_download_spec.rb +220 -0
  42. data/spec/aliyun/oss/client/resumable_upload_spec.rb +413 -0
  43. data/spec/aliyun/oss/http_spec.rb +83 -0
  44. data/spec/aliyun/oss/multipart_spec.rb +686 -0
  45. data/spec/aliyun/oss/object_spec.rb +785 -0
  46. data/spec/aliyun/oss/service_spec.rb +142 -0
  47. data/spec/aliyun/oss/util_spec.rb +50 -0
  48. data/spec/aliyun/sts/client_spec.rb +150 -0
  49. data/spec/aliyun/sts/util_spec.rb +39 -0
  50. data/tests/config.rb +31 -0
  51. data/tests/test_content_encoding.rb +54 -0
  52. data/tests/test_content_type.rb +95 -0
  53. data/tests/test_custom_headers.rb +70 -0
  54. data/tests/test_encoding.rb +77 -0
  55. data/tests/test_large_file.rb +66 -0
  56. data/tests/test_multipart.rb +97 -0
  57. data/tests/test_object_acl.rb +49 -0
  58. data/tests/test_object_key.rb +68 -0
  59. data/tests/test_object_url.rb +69 -0
  60. data/tests/test_resumable.rb +40 -0
  61. metadata +240 -0
@@ -0,0 +1,95 @@
1
+ require 'minitest/autorun'
2
+ require 'yaml'
3
+ $LOAD_PATH.unshift(File.expand_path("../../lib", __FILE__))
4
+ require 'aliyun/oss'
5
+ require_relative 'config'
6
+
7
+ class TestContentType < Minitest::Test
8
+ def setup
9
+ AliyunSDK::Common::Logging.set_log_level(Logger::DEBUG)
10
+ client = AliyunSDK::OSS::Client.new(TestConf.creds)
11
+ @bucket = client.get_bucket(TestConf.bucket)
12
+
13
+ @types = {
14
+ "html" => "text/html",
15
+ "js" => "application/javascript",
16
+ "xlsx" => "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
17
+ "xltx" => "application/vnd.openxmlformats-officedocument.spreadsheetml.template",
18
+ "potx" => "application/vnd.openxmlformats-officedocument.presentationml.template",
19
+ "ppsx" => "application/vnd.openxmlformats-officedocument.presentationml.slideshow",
20
+ "pptx" => "application/vnd.openxmlformats-officedocument.presentationml.presentation",
21
+ "sldx" => "application/vnd.openxmlformats-officedocument.presentationml.slide",
22
+ "docx" => "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
23
+ "dotx" => "application/vnd.openxmlformats-officedocument.wordprocessingml.template",
24
+ "xlam" => "application/vnd.ms-excel.addin.macroEnabled.12",
25
+ "xlsb" => "application/vnd.ms-excel.sheet.binary.macroEnabled.12",
26
+ "apk" => "application/vnd.android.package-archive",
27
+ "" => "application/octet-stream"
28
+ }
29
+
30
+ @prefix = "tests/content_type/"
31
+ end
32
+
33
+ def get_key(p, k)
34
+ "#{@prefix}#{p}obj" + (k.empty? ? "" : ".#{k}")
35
+ end
36
+
37
+ def test_type_from_key
38
+ @types.each do |k, v|
39
+ key = get_key('from_key', k)
40
+ @bucket.put_object(key)
41
+ assert_equal v, @bucket.get_object(key).headers[:content_type]
42
+
43
+ copy_key = get_key('copy.from_key', k)
44
+ @bucket.copy_object(key, copy_key)
45
+ assert_equal v, @bucket.get_object(copy_key).headers[:content_type]
46
+
47
+ append_key = get_key('append.from_key', k)
48
+ @bucket.append_object(append_key, 0)
49
+ assert_equal v, @bucket.get_object(append_key).headers[:content_type]
50
+ end
51
+ end
52
+
53
+ def test_type_from_file
54
+ @types.each do |k, v|
55
+ upload_file = "/tmp/upload_file"
56
+ upload_file += ".#{k}" unless k.empty?
57
+ `touch #{upload_file}`
58
+
59
+ key = get_key('from_file', k)
60
+ @bucket.put_object(key, :file => upload_file)
61
+ assert_equal v, @bucket.get_object(key).headers[:content_type]
62
+
63
+ append_key = get_key('append.from_file', k)
64
+ @bucket.append_object(append_key, 0, :file => upload_file)
65
+ assert_equal v, @bucket.get_object(append_key).headers[:content_type]
66
+
67
+ multipart_key = get_key('multipart.from_file', k)
68
+ @bucket.resumable_upload(multipart_key, upload_file)
69
+ assert_equal v, @bucket.get_object(multipart_key).headers[:content_type]
70
+ end
71
+ end
72
+
73
+ def test_type_from_user
74
+ @types.each do |k, v|
75
+ upload_file = "/tmp/upload_file.html"
76
+ `touch #{upload_file}`
77
+
78
+ key = get_key('from_user', k)
79
+ @bucket.put_object(key, :file => upload_file, :content_type => v)
80
+ assert_equal v, @bucket.get_object(key).headers[:content_type]
81
+
82
+ copy_key = get_key('copy.from_user', k)
83
+ @bucket.copy_object(key, copy_key, :content_type => v)
84
+ assert_equal v, @bucket.get_object(copy_key).headers[:content_type]
85
+
86
+ append_key = get_key('append.from_user', k)
87
+ @bucket.append_object(append_key, 0, :file => upload_file, :content_type => v)
88
+ assert_equal v, @bucket.get_object(append_key).headers[:content_type]
89
+
90
+ multipart_key = get_key('multipart.from_file', k)
91
+ @bucket.resumable_upload(multipart_key, upload_file, :content_type => v)
92
+ assert_equal v, @bucket.get_object(multipart_key).headers[:content_type]
93
+ end
94
+ end
95
+ end
@@ -0,0 +1,70 @@
1
+ require 'minitest/autorun'
2
+ require 'yaml'
3
+ $LOAD_PATH.unshift(File.expand_path("../../lib", __FILE__))
4
+ require 'aliyun/oss'
5
+ require 'zlib'
6
+ require_relative 'config'
7
+
8
+ class TestCustomHeaders < Minitest::Test
9
+ def setup
10
+ AliyunSDK::Common::Logging.set_log_level(Logger::DEBUG)
11
+ client = AliyunSDK::OSS::Client.new(TestConf.creds)
12
+ @bucket = client.get_bucket(TestConf.bucket)
13
+
14
+ @prefix = "tests/custom_headers/"
15
+ end
16
+
17
+ def get_key(k)
18
+ "#{@prefix}#{k}"
19
+ end
20
+
21
+ def test_custom_headers
22
+ key = get_key('ruby')
23
+ cache_control = 'max-age: 3600'
24
+ @bucket.put_object(key, headers: {'cache-control' => cache_control})
25
+ obj = @bucket.get_object(key)
26
+ assert_equal cache_control, obj.headers[:cache_control]
27
+
28
+ content_disposition = 'attachment; filename="fname.ext"'
29
+ @bucket.put_object(
30
+ key,
31
+ headers: {'cache-control' => cache_control,
32
+ 'CONTENT-DISPOSITION' => content_disposition})
33
+ obj = @bucket.get_object(key)
34
+ assert_equal cache_control, obj.headers[:cache_control]
35
+ assert_equal content_disposition, obj.headers[:content_disposition]
36
+
37
+ content_encoding = 'deflate'
38
+ expires = (Time.now + 3600).httpdate
39
+ @bucket.put_object(
40
+ key,
41
+ headers: {'cache-control' => cache_control,
42
+ 'CONTENT-DISPOSITION' => content_disposition,
43
+ 'content-ENCODING' => content_encoding,
44
+ 'EXPIRES' => expires }) do |s|
45
+ s << Zlib::Deflate.deflate('hello world')
46
+ end
47
+
48
+ content = ''
49
+ obj = @bucket.get_object(key) { |c| content << c }
50
+ assert_equal 'hello world', content
51
+ assert_equal cache_control, obj.headers[:cache_control]
52
+ assert_equal content_disposition, obj.headers[:content_disposition]
53
+ assert_equal content_encoding, obj.headers[:content_encoding]
54
+ assert_equal expires, obj.headers[:expires]
55
+ end
56
+
57
+ def test_headers_overwrite
58
+ key = get_key('rails')
59
+ @bucket.put_object(
60
+ key,
61
+ content_type: 'text/html',
62
+ metas: {'hello' => 'world'},
63
+ headers: {'content-type' => 'application/json',
64
+ 'x-oss-meta-hello' => 'bar'}) { |s| s << 'hello world' }
65
+ obj = @bucket.get_object(key)
66
+
67
+ assert_equal 'application/json', obj.headers[:content_type]
68
+ assert_equal ({'hello' => 'bar'}), obj.metas
69
+ end
70
+ end
@@ -0,0 +1,77 @@
1
+ # coding: utf-8
2
+ require 'minitest/autorun'
3
+ require 'yaml'
4
+ $LOAD_PATH.unshift(File.expand_path("../../lib", __FILE__))
5
+ require 'aliyun/oss'
6
+ require_relative 'config'
7
+
8
+ class TestEncoding < Minitest::Test
9
+ def setup
10
+ AliyunSDK::Common::Logging.set_log_level(Logger::DEBUG)
11
+ client = AliyunSDK::OSS::Client.new(TestConf.creds)
12
+ @bucket = client.get_bucket(TestConf.bucket)
13
+
14
+ @prefix = 'tests/encoding/'
15
+ end
16
+
17
+ def get_key(k)
18
+ @prefix + k
19
+ end
20
+
21
+ def test_utf_8
22
+ key = get_key('utf-8')
23
+ @bucket.put_object(key) do |stream|
24
+ stream << '中国' << 'Ruby'
25
+ end
26
+ put = '中国Ruby'.force_encoding(Encoding::ASCII_8BIT)
27
+ got = ''
28
+ @bucket.get_object(key) { |c| got << c }
29
+ assert_equal put, got
30
+
31
+ File.open('/tmp/x', 'w') { |f| f.write('中国Ruby') }
32
+ @bucket.put_object(key, :file => '/tmp/x')
33
+ got = ''
34
+ @bucket.get_object(key) { |c| got << c }
35
+ assert_equal put, got
36
+ end
37
+
38
+ def test_gbk
39
+ key = get_key('gbk')
40
+ @bucket.put_object(key) do |stream|
41
+ stream << '中国'.encode(Encoding::GBK) << 'Ruby'.encode(Encoding::GBK)
42
+ end
43
+ put = '中国Ruby'.encode(Encoding::GBK).force_encoding(Encoding::ASCII_8BIT)
44
+ got = ''
45
+ @bucket.get_object(key) { |c| got << c }
46
+ assert_equal put, got
47
+
48
+ File.open('/tmp/x', 'w') { |f| f.write('中国Ruby'.encode(Encoding::GBK)) }
49
+ @bucket.put_object(key, :file => '/tmp/x')
50
+ got = ''
51
+ @bucket.get_object(key) { |c| got << c }
52
+ assert_equal put, got
53
+ end
54
+
55
+ def encode_number(i)
56
+ [i].pack('N')
57
+ end
58
+
59
+ def test_binary
60
+ key = get_key('bin')
61
+ @bucket.put_object(key) do |stream|
62
+ (0..1024).each { |i| stream << encode_number(i) }
63
+ end
64
+ put = (0..1024).reduce('') { |s, i| s << encode_number(i) }
65
+ got = ''
66
+ @bucket.get_object(key) { |c| got << c }
67
+ assert_equal put, got
68
+
69
+ File.open('/tmp/x', 'w') { |f|
70
+ (0..1024).each { |i| f.write(encode_number(i)) }
71
+ }
72
+ @bucket.put_object(key, :file => '/tmp/x')
73
+ got = ''
74
+ @bucket.get_object(key) { |c| got << c }
75
+ assert_equal put, got
76
+ end
77
+ end
@@ -0,0 +1,66 @@
1
+ # coding: utf-8
2
+ require 'minitest/autorun'
3
+ require 'benchmark'
4
+ require 'yaml'
5
+ $LOAD_PATH.unshift(File.expand_path("../../lib", __FILE__))
6
+ require 'aliyun/oss'
7
+ require_relative 'config'
8
+
9
+ class TestLargeFile < Minitest::Test
10
+ def setup
11
+ client = AliyunSDK::OSS::Client.new(TestConf.creds)
12
+ @bucket = client.get_bucket(TestConf.bucket)
13
+
14
+ @prefix = 'tests/large_file/'
15
+ end
16
+
17
+ def get_key(k)
18
+ @prefix + k
19
+ end
20
+
21
+ def test_large_file_1gb
22
+ skip "don't run it by default"
23
+
24
+ key = get_key("large_file_1gb")
25
+ Benchmark.bm(32) do |bm|
26
+ bm.report("Upload with put_object: ") do
27
+ @bucket.put_object(key, :file => './large_file_1gb')
28
+ end
29
+
30
+ bm.report("Upload with resumable_upload: ") do
31
+ @bucket.resumable_upload(key, './large_file_1gb')
32
+ end
33
+
34
+ bm.report("Download with get_object: ") do
35
+ @bucket.get_object(key, :file => './large_file_1gb')
36
+ end
37
+
38
+ bm.report("Download with resumable_download: ") do
39
+ @bucket.resumable_download(key, './large_file_1gb')
40
+ end
41
+ end
42
+ end
43
+
44
+ def test_large_file_8gb
45
+ skip "don't run it by default"
46
+
47
+ key = get_key("large_file_8gb")
48
+ Benchmark.bm(32) do |bm|
49
+ bm.report("Upload with put_object: ") do
50
+ @bucket.put_object(key, :file => './large_file_8gb')
51
+ end
52
+
53
+ bm.report("Upload with resumable_upload: ") do
54
+ @bucket.resumable_upload(key, './large_file_8gb')
55
+ end
56
+
57
+ bm.report("Download with get_object: ") do
58
+ @bucket.get_object(key, :file => './large_file_8gb')
59
+ end
60
+
61
+ bm.report("Download with resumable_download: ") do
62
+ @bucket.resumable_download(key, './large_file_8gb')
63
+ end
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,97 @@
1
+ # coding: utf-8
2
+ require 'minitest/autorun'
3
+ require 'yaml'
4
+ $LOAD_PATH.unshift(File.expand_path("../../lib", __FILE__))
5
+ require 'aliyun/oss'
6
+ require_relative 'config'
7
+
8
+ class TestMultipart < Minitest::Test
9
+ def setup
10
+ AliyunSDK::Common::Logging.set_log_level(Logger::DEBUG)
11
+ client = AliyunSDK::OSS::Client.new(TestConf.creds)
12
+ @bucket_name = TestConf.bucket
13
+ @bucket = client.get_bucket(TestConf.bucket)
14
+
15
+ @protocol = AliyunSDK::OSS::Protocol.new(AliyunSDK::OSS::Config.new(TestConf.creds))
16
+ @prefix = 'tests/multipart/'
17
+ end
18
+
19
+ def get_key(k)
20
+ @prefix + k
21
+ end
22
+
23
+ def clear_uploads
24
+ all = @bucket.list_uploads.to_a
25
+ all.each { |t| @bucket.abort_upload(t.id, t.object) }
26
+ end
27
+
28
+ def test_key_marker
29
+ clear_uploads
30
+
31
+ # initiate 5 uploads
32
+ ids = []
33
+ 5.times { |i|
34
+ id = @protocol.initiate_multipart_upload(@bucket_name, get_key("obj-#{i}"))
35
+ ids << id
36
+ }
37
+
38
+ all = @bucket.list_uploads(limit: 1).to_a
39
+ assert_equal ids, all.map(&:id)
40
+
41
+ after_1 = @bucket.list_uploads(key_marker: get_key("obj-0")).to_a
42
+ assert_equal ids[1, 5], after_1.map(&:id)
43
+
44
+ after_5 = @bucket.list_uploads(key_marker: get_key("obj-4")).to_a
45
+ assert after_5.empty?, after_5.to_s
46
+ end
47
+
48
+ def test_id_marker
49
+ clear_uploads
50
+
51
+ # initiate 5 uploads
52
+ ids = []
53
+ 5.times { |i|
54
+ id = @protocol.initiate_multipart_upload(@bucket_name, get_key("object"))
55
+ ids << id
56
+ }
57
+ ids.sort!
58
+
59
+ all = @bucket.list_uploads.to_a
60
+ assert_equal ids, all.map(&:id)
61
+
62
+ # id_marker is ignored
63
+ after_1 = @bucket.list_uploads(id_marker: ids[0]).to_a
64
+ assert_equal ids, after_1.map(&:id)
65
+
66
+ # id_marker is ignored
67
+ after_5 = @bucket.list_uploads(id_marker: ids[4]).to_a
68
+ assert_equal ids, after_5.map(&:id)
69
+ end
70
+
71
+ def test_id_key_marker
72
+ clear_uploads
73
+
74
+ # initiate 5 uploads
75
+ foo_ids = []
76
+ 5.times { |i|
77
+ id = @protocol.initiate_multipart_upload(@bucket_name, get_key("foo"))
78
+ foo_ids << id
79
+ }
80
+ foo_ids.sort!
81
+
82
+ bar_ids = []
83
+ 5.times { |i|
84
+ id = @protocol.initiate_multipart_upload(@bucket_name, get_key("bar"))
85
+ bar_ids << id
86
+ }
87
+ bar_ids.sort!
88
+
89
+ after_1 = @bucket.list_uploads(
90
+ id_marker: bar_ids[0], key_marker: get_key("bar"), limit: 1).to_a
91
+ assert_equal bar_ids[1, 5] + foo_ids, after_1.map(&:id)
92
+
93
+ after_5 = @bucket.list_uploads(
94
+ id_marker: bar_ids[4], key_marker: get_key("bar")).to_a
95
+ assert_equal foo_ids, after_5.map(&:id)
96
+ end
97
+ end
@@ -0,0 +1,49 @@
1
+ require 'minitest/autorun'
2
+ require 'yaml'
3
+ $LOAD_PATH.unshift(File.expand_path("../../lib", __FILE__))
4
+ require 'aliyun/oss'
5
+ require_relative 'config'
6
+
7
+ class TestObjectACL < Minitest::Test
8
+ def setup
9
+ AliyunSDK::Common::Logging.set_log_level(Logger::DEBUG)
10
+ client = AliyunSDK::OSS::Client.new(TestConf.creds)
11
+ @bucket = client.get_bucket(TestConf.bucket)
12
+
13
+ @prefix = "tests/object_acl/"
14
+ end
15
+
16
+ def get_key(k)
17
+ "#{@prefix}#{k}"
18
+ end
19
+
20
+ def test_put_object
21
+ key = get_key('put')
22
+
23
+ @bucket.put_object(key, acl: AliyunSDK::OSS::ACL::PRIVATE)
24
+ acl = @bucket.get_object_acl(key)
25
+
26
+ assert_equal AliyunSDK::OSS::ACL::PRIVATE, acl
27
+
28
+ @bucket.put_object(key, acl: AliyunSDK::OSS::ACL::PUBLIC_READ)
29
+ acl = @bucket.get_object_acl(key)
30
+
31
+ assert_equal AliyunSDK::OSS::ACL::PUBLIC_READ, acl
32
+ end
33
+
34
+ def test_append_object
35
+ key = get_key('append-1')
36
+
37
+ @bucket.append_object(key, 0, acl: AliyunSDK::OSS::ACL::PRIVATE)
38
+ acl = @bucket.get_object_acl(key)
39
+
40
+ assert_equal AliyunSDK::OSS::ACL::PRIVATE, acl
41
+
42
+ key = get_key('append-2')
43
+
44
+ @bucket.put_object(key, acl: AliyunSDK::OSS::ACL::PUBLIC_READ)
45
+ acl = @bucket.get_object_acl(key)
46
+
47
+ assert_equal AliyunSDK::OSS::ACL::PUBLIC_READ, acl
48
+ end
49
+ end