aliyun-sdk 0.1.8 → 0.2.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.
Files changed (39) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +5 -0
  3. data/README.md +28 -4
  4. data/examples/aliyun/oss/bucket.rb +1 -1
  5. data/examples/aliyun/oss/object.rb +1 -1
  6. data/examples/aliyun/oss/resumable_download.rb +1 -1
  7. data/examples/aliyun/oss/resumable_upload.rb +1 -1
  8. data/examples/aliyun/oss/streaming.rb +1 -1
  9. data/examples/aliyun/oss/using_sts.rb +48 -0
  10. data/examples/aliyun/sts/assume_role.rb +59 -0
  11. data/lib/aliyun/common.rb +6 -0
  12. data/lib/aliyun/common/exception.rb +18 -0
  13. data/lib/aliyun/{oss → common}/logging.rb +3 -3
  14. data/lib/aliyun/common/struct.rb +56 -0
  15. data/lib/aliyun/oss.rb +1 -2
  16. data/lib/aliyun/oss/bucket.rb +1 -1
  17. data/lib/aliyun/oss/client.rb +2 -2
  18. data/lib/aliyun/oss/config.rb +3 -2
  19. data/lib/aliyun/oss/download.rb +3 -0
  20. data/lib/aliyun/oss/exception.rb +2 -14
  21. data/lib/aliyun/oss/http.rb +3 -1
  22. data/lib/aliyun/oss/multipart.rb +2 -4
  23. data/lib/aliyun/oss/object.rb +1 -1
  24. data/lib/aliyun/oss/protocol.rb +1 -1
  25. data/lib/aliyun/oss/struct.rb +5 -54
  26. data/lib/aliyun/oss/upload.rb +3 -0
  27. data/lib/aliyun/oss/util.rb +1 -1
  28. data/lib/aliyun/sts.rb +9 -0
  29. data/lib/aliyun/sts/client.rb +38 -0
  30. data/lib/aliyun/sts/config.rb +21 -0
  31. data/lib/aliyun/sts/exception.rb +53 -0
  32. data/lib/aliyun/sts/protocol.rb +130 -0
  33. data/lib/aliyun/sts/struct.rb +64 -0
  34. data/lib/aliyun/sts/util.rb +48 -0
  35. data/lib/aliyun/{oss/version.rb → version.rb} +1 -3
  36. data/spec/aliyun/oss/client/client_spec.rb +21 -1
  37. data/spec/aliyun/sts/client_spec.rb +150 -0
  38. data/spec/aliyun/sts/util_spec.rb +39 -0
  39. metadata +21 -4
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5464cc7eae38bc5a03989a659ecebe5ad29b20c5
4
- data.tar.gz: 7b700d86e7985b53227aee212ed097d9d68f9c83
3
+ metadata.gz: 1f79884482b39489cdabb7123b28bb1dae2bb547
4
+ data.tar.gz: e7a09b6bc276c0f8651f6ead88be61e8fa7d70cd
5
5
  SHA512:
6
- metadata.gz: 3a98134dfb618c8359e66d8914918d05fc1b20efe2589cf3d9f094b001a0aab38c3c0292465f6457ae07cc2941c65843ed960ad88e83bd18c9eeabfbbb6c35c2
7
- data.tar.gz: 0c8d4c5b7aa671664c2f5f433d44c7307f2ad4b38387c839f2ca6b1d27c857fc106e6c2c03cf1d8be2ce7200a2de10c338f22665ac3e5e547729f73df0f81a71
6
+ metadata.gz: 3021afacd2b718ec6cc01e898fed4b0d3c909f1576d0744c84ad45c608b325f7ec047650b23c75176e1e7a27277afec31064f58d742998e359ffb9bcd31efa31
7
+ data.tar.gz: 91fca099332c548efc4d6fc0ae111aa4e7ffda04afdda041807d9da156e5ef5eecf002c2258dc5f086104ba4878cb956bac38b77ff0740ba73746bc02f486e1b
@@ -1,5 +1,10 @@
1
1
  ## Change Log
2
2
 
3
+ ### v0.2.0
4
+
5
+ - Add aliyun/sts
6
+ - OSS::Client support STS
7
+
3
8
  ### v0.1.8
4
9
 
5
10
  - Fix StreamWriter string encoding problem
data/README.md CHANGED
@@ -54,7 +54,7 @@ AccessKeySecret,在使用Aliyun OSS SDK时需要提供您的这两个信息。
54
54
 
55
55
  其中`endpoint`是OSS服务的地址,根据节点区域不同,这个地址可能不一样,例如
56
56
  杭州节点的地址是:`http://oss-cn-hangzhou.aliyuncs.com`,其他节点的地址见:
57
- [节点列表][1]
57
+ [节点列表][region-list]
58
58
 
59
59
  `access_key_id`和`access_key_secret`是您的服务凭证,在官网的“管理控制
60
60
  台”上面可以查看。**请妥善保管您的AccessKeySecret,泄露之后可能影响您的
@@ -65,7 +65,7 @@ AccessKeySecret,在使用Aliyun OSS SDK时需要提供您的这两个信息。
65
65
  OSS支持自定义域名绑定,允许用户将自己的域名指向阿里云OSS的服务地址
66
66
  (CNAME),这样用户迁移到OSS上时应用内资源的路径可以不用修改。绑定的域
67
67
  名指向OSS的一个bucket。绑定域名的操作只能在OSS控制台进行。更多关于自定
68
- 义域名绑定的内容请到官网了解:[OSS自定义域名绑定][2]
68
+ 义域名绑定的内容请到官网了解:[OSS自定义域名绑定][custom-domain]
69
69
 
70
70
  用户绑定了域名后,使用SDK时指定的endpoint可以使用标准的OSS服务地址,也
71
71
  可以使用用户绑定的域名:
@@ -84,6 +84,28 @@ OSS支持自定义域名绑定,允许用户将自己的域名指向阿里云OS
84
84
  3. 在{Aliyun::OSS::Client#get_bucket}时仍需要指定bucket名字,并且要与
85
85
  域名所绑定的bucket名字相同
86
86
 
87
+ #### 使用STS创建Client
88
+
89
+ OSS支持用户使用STS进行访问,更多有关STS的内容,请参考 [阿里云STS][aliyun-sts]。
90
+ 在使用STS之前需要先向STS申请一个临时token,
91
+ aliyun-sdk中包含了STS的SDK,使用时只需要`require 'aliyun/sts'`即可:
92
+
93
+ require 'aliyun/sts'
94
+ sts = Aliyun::STS::Client.new(
95
+ access_key_id: 'access_key_id',
96
+ access_key_secret: 'access_key_secret')
97
+
98
+ token = sts.assume_role('role-arn', 'my-app')
99
+
100
+ client = Aliyun::OSS::Client.new(
101
+ :endpoint => 'http://img.my-domain.com',
102
+ :access_key_id => token.access_key_id,
103
+ :access_key_secret => token.access_key_secret,
104
+ :sts_token => token.sts_token)
105
+
106
+ 注意使用STS时必须指定`:sts_token`参数。用户还可以通过`STS::Client`申请
107
+ 带Policy的token,细节请参考[API文档][sdk-api]。
108
+
87
109
  ### 列出当前所有的Bucket
88
110
 
89
111
  buckets = client.list_buckets
@@ -360,5 +382,7 @@ SDK采用rspec进行测试,如果要对SDK进行修改,请确保没有break
360
382
  - 阿里云官网文档:http://help.aliyun.com/product/8314910_oss.html
361
383
 
362
384
 
363
- [1]: http://help.aliyun.com/document_detail/oss/user_guide/endpoint_region.html
364
- [2]: http://help.aliyun.com/document_detail/oss/user_guide/oss_concept/oss_cname.html
385
+ [region-list]: https://help.aliyun.com/document_detail/oss/user_guide/endpoint_region.html
386
+ [custom-domain]: https://help.aliyun.com/document_detail/oss/user_guide/oss_concept/oss_cname.html
387
+ [aliyun-sts]: https://help.aliyun.com/document_detail/ram/intro/concepts.html
388
+ [sdk-api]: http://www.rubydoc.info/gems/aliyun-sdk/
@@ -5,7 +5,7 @@ require 'yaml'
5
5
  require 'aliyun/oss'
6
6
 
7
7
  # 初始化OSS client
8
- Aliyun::OSS::Logging.set_log_level(Logger::DEBUG)
8
+ Aliyun::Common::Logging.set_log_level(Logger::DEBUG)
9
9
  conf_file = '~/.oss.yml'
10
10
  conf = YAML.load(File.read(File.expand_path(conf_file)))
11
11
  client = Aliyun::OSS::Client.new(
@@ -5,7 +5,7 @@ require 'yaml'
5
5
  require 'aliyun/oss'
6
6
 
7
7
  # 初始化OSS client
8
- Aliyun::OSS::Logging.set_log_level(Logger::DEBUG)
8
+ Aliyun::Common::Logging.set_log_level(Logger::DEBUG)
9
9
  conf_file = '~/.oss.yml'
10
10
  conf = YAML.load(File.read(File.expand_path(conf_file)))
11
11
  bucket = Aliyun::OSS::Client.new(
@@ -5,7 +5,7 @@ require 'yaml'
5
5
  require 'aliyun/oss'
6
6
 
7
7
  # 初始化OSS Bucket
8
- Aliyun::OSS::Logging.set_log_level(Logger::DEBUG)
8
+ Aliyun::Common::Logging.set_log_level(Logger::DEBUG)
9
9
  conf_file = '~/.oss.yml'
10
10
  conf = YAML.load(File.read(File.expand_path(conf_file)))
11
11
  bucket = Aliyun::OSS::Client.new(
@@ -5,7 +5,7 @@ require 'yaml'
5
5
  require 'aliyun/oss'
6
6
 
7
7
  # 初始化OSS Bucket
8
- Aliyun::OSS::Logging.set_log_level(Logger::DEBUG)
8
+ Aliyun::Common::Logging.set_log_level(Logger::DEBUG)
9
9
  conf_file = '~/.oss.yml'
10
10
  conf = YAML.load(File.read(File.expand_path(conf_file)))
11
11
  bucket = Aliyun::OSS::Client.new(
@@ -23,7 +23,7 @@ require 'aliyun/oss'
23
23
  # 和下载。
24
24
 
25
25
  # 初始化OSS client
26
- Aliyun::OSS::Logging.set_log_level(Logger::DEBUG)
26
+ Aliyun::Common::Logging.set_log_level(Logger::DEBUG)
27
27
  conf_file = '~/.oss.yml'
28
28
  conf = YAML.load(File.read(File.expand_path(conf_file)))
29
29
  bucket = Aliyun::OSS::Client.new(
@@ -0,0 +1,48 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ $LOAD_PATH.unshift(File.expand_path("../../../../lib", __FILE__))
4
+ require 'yaml'
5
+ require 'aliyun/sts'
6
+ require 'aliyun/oss'
7
+
8
+ # 初始化OSS client
9
+ Aliyun::Common::Logging.set_log_level(Logger::DEBUG)
10
+ conf_file = '~/.sts.yml'
11
+ conf = YAML.load(File.read(File.expand_path(conf_file)))
12
+
13
+ # 辅助打印函数
14
+ def demo(msg)
15
+ puts "######### #{msg} ########"
16
+ puts
17
+ yield
18
+ puts "-------------------------"
19
+ puts
20
+ end
21
+
22
+ demo "Using STS" do
23
+ sts = Aliyun::STS::Client.new(
24
+ :access_key_id => conf['access_key_id'],
25
+ :access_key_secret => conf['access_key_secret'])
26
+
27
+ token = sts.assume_role(
28
+ 'acs:ram::52352:role/aliyunosstokengeneratorrole', 'app-1')
29
+
30
+ client = Aliyun::OSS::Client.new(
31
+ :endpoint => 'http://oss-cn-hangzhou.aliyuncs.com',
32
+ :sts_token => token.security_token,
33
+ :access_key_id => token.access_key_id,
34
+ :access_key_secret => token.access_key_secret)
35
+
36
+ unless client.bucket_exists?('bucket-for-sts-test')
37
+ client.create_bucket('bucket-for-sts-test')
38
+ end
39
+
40
+ bucket = client.get_bucket('bucket-for-sts-test')
41
+
42
+ bucket.put_object('hello') { |s| s << 'hello' }
43
+ bucket.put_object('world') { |s| s << 'world' }
44
+
45
+ bucket.list_objects.take(10).each do |obj|
46
+ puts "Object: #{obj.key}, size: #{obj.size}"
47
+ end
48
+ end
@@ -0,0 +1,59 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ $LOAD_PATH.unshift(File.expand_path("../../../../lib", __FILE__))
4
+ require 'yaml'
5
+ require 'aliyun/sts'
6
+
7
+ Aliyun::Common::Logging.set_log_level(Logger::DEBUG)
8
+ conf_file = '~/.sts.yml'
9
+ conf = YAML.load(File.read(File.expand_path(conf_file)))
10
+ client = Aliyun::STS::Client.new(
11
+ :access_key_id => conf['access_key_id'],
12
+ :access_key_secret => conf['access_key_secret'])
13
+
14
+ # 辅助打印函数
15
+ def demo(msg)
16
+ puts "######### #{msg} ########"
17
+ puts
18
+ yield
19
+ puts "-------------------------"
20
+ puts
21
+ end
22
+
23
+ token = client.assume_role(
24
+ 'acs:ram::52352:role/aliyunosstokengeneratorrole', 'app-1')
25
+
26
+ demo "Assume role" do
27
+ begin
28
+ token = client.assume_role(
29
+ 'acs:ram::52352:role/aliyunosstokengeneratorrole', 'app-1')
30
+
31
+ puts "Credentials for session: #{token.session_name}"
32
+ puts "access key id: #{token.access_key_id}"
33
+ puts "access key secret: #{token.access_key_secret}"
34
+ puts "security token: #{token.security_token}"
35
+ puts "expiration at: #{token.expiration}"
36
+ rescue => e
37
+ puts "AssumeRole failed: #{e.message}"
38
+ end
39
+ end
40
+
41
+ demo "Assume role with policy" do
42
+ begin
43
+ policy = Aliyun::STS::Policy.new
44
+ policy.allow(
45
+ ['oss:Get*', 'oss:PutObject'],
46
+ ['acs:oss:*:*:my-bucket', 'acs:oss:*:*:my-bucket/*'])
47
+
48
+ token = client.assume_role(
49
+ 'acs:ram::52352:role/aliyunosstokengeneratorrole', 'app-2', policy, 900)
50
+
51
+ puts "Credentials for session: #{token.session_name}"
52
+ puts "access key id: #{token.access_key_id}"
53
+ puts "access key secret: #{token.access_key_secret}"
54
+ puts "security token: #{token.security_token}"
55
+ puts "expiration at: #{token.expiration}"
56
+ rescue => e
57
+ puts "AssumeRole failed: #{e.message}"
58
+ end
59
+ end
@@ -0,0 +1,6 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ require_relative 'version'
4
+ require_relative 'common/struct'
5
+ require_relative 'common/logging'
6
+ require_relative 'common/exception'
@@ -0,0 +1,18 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ module Aliyun
4
+ module Common
5
+
6
+ ##
7
+ # Base exception class
8
+ #
9
+ class Exception < RuntimeError
10
+ attr_reader :message
11
+
12
+ def initialize(message)
13
+ @message = message
14
+ end
15
+ end
16
+
17
+ end # Common
18
+ end # Aliyun
@@ -3,7 +3,7 @@
3
3
  require 'logger'
4
4
 
5
5
  module Aliyun
6
- module OSS
6
+ module Common
7
7
  ##
8
8
  # Logging support
9
9
  # @example
@@ -11,7 +11,7 @@ module Aliyun
11
11
  # logger.info(xxx)
12
12
  module Logging
13
13
 
14
- DEFAULT_LOG_FILE = "./oss_sdk.log"
14
+ DEFAULT_LOG_FILE = "./aliyun_sdk.log"
15
15
  MAX_NUM_LOG = 100
16
16
  ROTATE_SIZE = 10 * 1024 * 1024
17
17
 
@@ -42,5 +42,5 @@ module Aliyun
42
42
  end
43
43
 
44
44
  end # logging
45
- end # OSS
45
+ end # Common
46
46
  end # Aliyun
@@ -0,0 +1,56 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ module Aliyun
4
+ module Common
5
+
6
+ # Common structs used. It provides a 'attrs' helper method for
7
+ # subclass to define its attributes. 'attrs' is based on
8
+ # attr_reader and provide additional functionalities for classes
9
+ # that inherits Struct::Base :
10
+ # * the constuctor is provided to accept options and set the
11
+ # corresponding attibute automatically
12
+ # * the #to_s method is rewrite to concatenate the defined
13
+ # attributes keys and values
14
+ # @example
15
+ # class X < Struct::Base
16
+ # attrs :foo, :bar
17
+ # end
18
+ #
19
+ # x.new(:foo => 'hello', :bar => 'world')
20
+ # x.foo # == "hello"
21
+ # x.bar # == "world"
22
+ # x.to_s # == "foo: hello, bar: world"
23
+ module Struct
24
+ class Base
25
+ module AttrHelper
26
+ def attrs(*s)
27
+ define_method(:attrs) {s}
28
+ attr_reader(*s)
29
+ end
30
+ end
31
+
32
+ extend AttrHelper
33
+
34
+ def initialize(opts = {})
35
+ extra_keys = opts.keys - attrs
36
+ unless extra_keys.empty?
37
+ fail Common::Exception,
38
+ "Unexpected extra keys: #{extra_keys.join(', ')}"
39
+ end
40
+
41
+ attrs.each do |attr|
42
+ instance_variable_set("@#{attr}", opts[attr])
43
+ end
44
+ end
45
+
46
+ def to_s
47
+ attrs.map do |attr|
48
+ v = instance_variable_get("@#{attr}")
49
+ "#{attr.to_s}: #{v}"
50
+ end.join(", ")
51
+ end
52
+ end # Base
53
+ end # Struct
54
+
55
+ end # Common
56
+ end # Aliyun
@@ -1,7 +1,6 @@
1
1
  # -*- encoding: utf-8 -*-
2
2
 
3
- require_relative 'oss/version'
4
- require_relative 'oss/logging'
3
+ require_relative 'common'
5
4
  require_relative 'oss/util'
6
5
  require_relative 'oss/exception'
7
6
  require_relative 'oss/struct'
@@ -8,7 +8,7 @@ module Aliyun
8
8
  # website, lifecycle, cors)
9
9
  # 2. object相关:上传、下载、追加、拷贝object等
10
10
  # 3. multipart相关:断点续传、断点续载
11
- class Bucket < Struct::Base
11
+ class Bucket < Common::Struct::Base
12
12
 
13
13
  attrs :name, :location, :creation_time
14
14
 
@@ -18,8 +18,6 @@ module Aliyun
18
18
  # bucket = client.get_bucket('my-bucket')
19
19
  class Client
20
20
 
21
- include Logging
22
-
23
21
  # 构造OSS client,用于操作buckets。
24
22
  # @param opts [Hash] 构造Client时的参数选项
25
23
  # @option opts [String] :endpoint [必填]OSS服务的地址,可以是以
@@ -30,6 +28,8 @@ module Aliyun
30
28
  # KEY SECRET,如果不填则会尝试匿名访问
31
29
  # @option opts [Boolean] :cname [可选] 指定endpoint是否是用户绑
32
30
  # 定的域名
31
+ # @option opts [Boolean] :sts_token [可选] 指定STS的
32
+ # SecurityToken,如果指定,则使用STS授权访问
33
33
  # @option opts [Fixnum] :open_timeout [可选] 指定建立连接的超时
34
34
  # 时间,默认为10秒
35
35
  # @option opts [Fixnum] :read_timeout [可选] 指定等待响应的超时
@@ -7,9 +7,10 @@ module Aliyun
7
7
  # A place to store various configurations: credentials, api
8
8
  # timeout, retry mechanism, etc
9
9
  #
10
- class Config < Struct::Base
10
+ class Config < Common::Struct::Base
11
11
 
12
- attrs :endpoint, :cname, :access_key_id, :access_key_secret,
12
+ attrs :endpoint, :cname, :sts_token,
13
+ :access_key_id, :access_key_secret,
13
14
  :open_timeout, :read_timeout
14
15
 
15
16
  def initialize(opts = {})
@@ -7,6 +7,9 @@ module Aliyun
7
7
  # A multipart download transaction
8
8
  #
9
9
  class Download < Transaction
10
+
11
+ include Common::Logging
12
+
10
13
  PART_SIZE = 10 * 1024 * 1024
11
14
  READ_SIZE = 16 * 1024
12
15
  NUM_THREAD = 10
@@ -5,20 +5,13 @@ require 'nokogiri'
5
5
  module Aliyun
6
6
  module OSS
7
7
 
8
- ##
9
- # Base exception class
10
- #
11
- class Exception < RuntimeError
12
- end
13
-
14
8
  ##
15
9
  # ServerError represents exceptions from the OSS
16
10
  # service. i.e. Client receives a HTTP response whose status is
17
11
  # NOT OK. #message provides the error message and #to_s gives
18
12
  # detailed information probably including the OSS request id.
19
13
  #
20
- class ServerError < Exception
21
- include Logging
14
+ class ServerError < Common::Exception
22
15
 
23
16
  attr_reader :http_code, :error_code, :message, :request_id
24
17
 
@@ -65,12 +58,7 @@ module Aliyun
65
58
  # ClientError represents client exceptions caused mostly by
66
59
  # invalid parameters.
67
60
  #
68
- class ClientError < Exception
69
- attr_reader :message
70
-
71
- def initialize(message)
72
- @message = message
73
- end
61
+ class ClientError < Common::Exception
74
62
  end # ClientError
75
63
 
76
64
  ##