aliyun-oss-sdk 0.1.1 → 0.1.2

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.
@@ -5,7 +5,6 @@ module Aliyun
5
5
  # List objects of bucket
6
6
  #
7
7
  # @param (see #bucket_list_objects)
8
- #
9
8
  # @option (see #bucket_list_objects)
10
9
  #
11
10
  # @raise [RequestError]
@@ -17,9 +16,9 @@ module Aliyun
17
16
  result = client.bucket_list_objects(*args).parsed_response
18
17
 
19
18
  object_keys = %w(ListBucketResult Contents)
20
- Utils.wrap(Utils.dig_value(result, *object_keys)).map do |object|
21
- Struct::Object.new(object.merge(client: client))
22
- end
19
+ directory_keys = %w(ListBucketResult CommonPrefixes)
20
+ Struct::Object.init_from_response(result, object_keys, client) + \
21
+ Struct::Object.init_from_response(result, directory_keys, client)
23
22
  end
24
23
 
25
24
  # create object of bucket
@@ -99,11 +98,11 @@ module Aliyun
99
98
  #
100
99
  # @raise (see #bucket_append_object)
101
100
  #
102
- # @return [true]
101
+ # @return [HTTParty::Response::Headers]
103
102
  #
104
103
  # @see Client#bucket_append_object
105
104
  def append(*args)
106
- !!client.bucket_append_object(*args)
105
+ client.bucket_append_object(*args).headers
107
106
  end
108
107
  end
109
108
  end
@@ -15,11 +15,7 @@ module Aliyun
15
15
 
16
16
  bucket_keys = %w(ListAllMyBucketsResult Buckets Bucket)
17
17
  Utils.wrap(Utils.dig_value(result, *bucket_keys)).map do |bucket_hash|
18
- Struct::Bucket.new(bucket_hash).tap do |bucket|
19
- dup_client = client.clone
20
- dup_client.bucket = bucket.name
21
- bucket.client = dup_client
22
- end
18
+ build_bucket(bucket_hash, client)
23
19
  end
24
20
  end
25
21
 
@@ -44,6 +40,16 @@ module Aliyun
44
40
  def delete(*args)
45
41
  !!client.bucket_delete(*args)
46
42
  end
43
+
44
+ private
45
+
46
+ def build_bucket(bucket_hash, client)
47
+ Struct::Bucket.new(bucket_hash).tap do |bucket|
48
+ bucket.client = Client.new(
49
+ client.access_key, client.secret_key, host: bucket.host, bucket: bucket.name
50
+ )
51
+ end
52
+ end
47
53
  end
48
54
  end
49
55
  end
@@ -119,7 +119,7 @@ module Aliyun
119
119
 
120
120
  def default_content_type
121
121
  {
122
- 'Content-Type' => 'application/x-www-form-urlencoded'
122
+ 'Content-Type' => 'application/xml'
123
123
  }
124
124
  end
125
125
 
@@ -14,6 +14,10 @@ module Aliyun
14
14
  # reference to client
15
15
  attr_accessor :client
16
16
 
17
+ def host
18
+ "#{location}.aliyuncs.com"
19
+ end
20
+
17
21
  # Get the location
18
22
  #
19
23
  # @return [String]
@@ -0,0 +1,25 @@
1
+ module Aliyun
2
+ module Oss
3
+ module Struct
4
+ class Directory < Object
5
+ # prefix in CommonPrefixes is key of Directory object
6
+ alias_method :prefix=, :key=
7
+
8
+ # List objects under directory
9
+ #
10
+ # @see #bucket_list_objects
11
+ #
12
+ # @param (see #bucket_list_objects)
13
+ # @option (see #bucket_list_objects)
14
+ #
15
+ # @raise [RequestError]
16
+ #
17
+ # @return [Array<Aliyun::Oss::Struct::Object>]
18
+ def list(options = {})
19
+ Utils.stringify_keys!(options)
20
+ client.bucket_objects.list(options.merge('prefix' => key))
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,8 @@
1
+ module Aliyun
2
+ module Oss
3
+ module Struct
4
+ class File < Object
5
+ end
6
+ end
7
+ end
8
+ end
@@ -28,9 +28,9 @@ module Aliyun
28
28
  #
29
29
  # @raise (see #bucket_multipart_upload)
30
30
  #
31
- # @return [true]
31
+ # @return [HTTParty::Response::Headers]
32
32
  def upload(*args)
33
- !!client.bucket_multipart_upload(*args.unshift(upload_id, key))
33
+ client.bucket_multipart_upload(*args.unshift(upload_id, key)).headers
34
34
  end
35
35
 
36
36
  # Copy exsting object to Multipart Upload Event
@@ -61,11 +61,32 @@ module Aliyun
61
61
  #
62
62
  # @raise [RequestError]
63
63
  #
64
- # @return [Hash]
64
+ # @return [HTTParty::Response::Headers]
65
65
  def meta!(*args)
66
66
  client.bucket_get_meta_object(*args.unshift(key)).headers
67
67
  end
68
+
69
+ class << self
70
+ def init_from_response(result, keys, client)
71
+ Utils.wrap(Utils.dig_value(result, *keys)).map do |object|
72
+ init_from_object(object, client)
73
+ end
74
+ end
75
+
76
+ def init_from_object(object, client)
77
+ if object.key?('Key') && object['Key'].end_with?('/')
78
+ Struct::Directory.new(object.merge(client: client))
79
+ elsif object.key?('Prefix') && object['Prefix'].end_with?('/')
80
+ Struct::Directory.new(object.merge(client: client))
81
+ else
82
+ Struct::File.new(object.merge(client: client))
83
+ end
84
+ end
85
+ end
68
86
  end
69
87
  end
70
88
  end
71
89
  end
90
+
91
+ require 'aliyun/oss/struct/file'
92
+ require 'aliyun/oss/struct/directory'
@@ -4,9 +4,11 @@ module Aliyun
4
4
  class Part < Base
5
5
  # [Integer] :number the part number
6
6
  attr_accessor :number
7
+ alias_method :part_number=, :number=
7
8
 
8
9
  # [String] :etag the etag for the part
9
10
  attr_accessor :etag
11
+ alias_method :e_tag=, :etag=
10
12
 
11
13
  # Last Modified time
12
14
  attr_accessor :last_modified
@@ -18,14 +20,6 @@ module Aliyun
18
20
  @last_modified = Time.parse(last_modified)
19
21
  end
20
22
 
21
- def part_number=(part_number)
22
- @number = part_number
23
- end
24
-
25
- def e_tag=(e_tag)
26
- @etag = e_tag
27
- end
28
-
29
23
  def to_hash
30
24
  if valid?
31
25
  {
@@ -85,6 +85,12 @@ module Aliyun
85
85
  [object]
86
86
  end
87
87
  end
88
+
89
+ def self.stringify_keys!(hash)
90
+ hash.keys.each do |key|
91
+ hash[key.to_s] = hash.delete(key)
92
+ end
93
+ end
88
94
  end
89
95
  end
90
96
  end
@@ -1,5 +1,5 @@
1
1
  module Aliyun
2
2
  module Oss
3
- VERSION = '0.1.1'
3
+ VERSION = '0.1.2'
4
4
  end
5
5
  end
data/todo.md CHANGED
@@ -3,3 +3,4 @@
3
3
  + Command Line Tool
4
4
  + Custom Domain Support
5
5
  + Get Download URL
6
+ + Multipart#upload return headers
@@ -42,7 +42,7 @@ Upload ID is the UUID for the Multipart Upload Event, store it for use later.
42
42
  bucket = "bucket-name"
43
43
  client = Aliyun::Oss::Client.new(access_key, secret_key, host: host, bucket: bucket)
44
44
 
45
- res = client.bucket_multipart_upload("Exciting-Ruby.mp4", 1, "Upload ID", file_or_bin)
45
+ res = client.bucket_multipart_upload("Upload ID", "Exciting-Ruby.mp4", 1, file_or_bin)
46
46
 
47
47
  if res.success?
48
48
  puts "etag: #{res.headers['etag']}"
@@ -71,7 +71,7 @@ It can used to upload part to a object. Please note:
71
71
  part1 = Aliyun::Oss::Struct::Part.new({ number: 1, etag: 'etag1' })
72
72
  part2 = Aliyun::Oss::Struct::Part.new({ number: 2, etag: 'etag2' })
73
73
  part3 = Aliyun::Oss::Struct::Part.new({ number: 3, etag: 'etag3' })
74
- res = client.bucket_complete_multipart("Exciting-Ruby.mp4", "Upload ID", [part1, part2, part3])
74
+ res = client.bucket_complete_multipart("Upload ID", "Exciting-Ruby.mp4", [part1, part2, part3])
75
75
 
76
76
 
77
77
  Here, we create Aliyun::Oss::Struct::Part to build your part, use Part#valid? to valid the object.
@@ -87,7 +87,7 @@ If some Problem occurs, you may want to abort a Multipart Upload:
87
87
  bucket = "bucket-name"
88
88
  client = Aliyun::Oss::Client.new(access_key, secret_key, host: host, bucket: bucket)
89
89
 
90
- res = client.bucket_abort_multipart("Exciting-Ruby.mp4", "Upload ID")
90
+ res = client.bucket_abort_multipart("Upload ID", "Exciting-Ruby.mp4")
91
91
 
92
92
  After abort a multipart, all uploaded parts will be destroyed, But Note: If some others are upload parts to this object when your abort, they may be missing, so invoke a few times if you have access in concurrent.
93
93
 
@@ -0,0 +1,125 @@
1
+ ## Bucket
2
+
3
+ Bucket is a namespace in OSS, as well as management entity for high functions such as pricing, access control, logging; Bucket names are global uniqueness throughout the OSS services, and cannot be modified. Each Object stored in the OSS must contained in a Bucket. An application, such as the picture sharing website, can correspond to one or more Bucket. A user can create up to 10 Bucket, but each bucket can store unlimit objects, there is no limit to the number of storage capacity each buckte highest support 2 PB.
4
+
5
+ ### Name Spec
6
+
7
+ + Only contains lowercase letters, Numbers, dash (-)
8
+ + Must begin with lowercase letters or Numbers
9
+ + Length must be between 3-63 bytes
10
+
11
+
12
+ ### Create Bucket
13
+
14
+ require 'aliyun/oss'
15
+
16
+ access_key, secret_key = "your id", "your secret"
17
+ host = "oss-cn-hangzhou.aliyuncs.com"
18
+ client = Aliyun::Oss::Client.new(access_key, secret_key, host: host)
19
+
20
+ # create a private bucket on oss-cn-beijing
21
+ begin
22
+ client.buckets.create('new-bucket', 'oss-cn-beijing', 'private')
23
+ rescue Aliyun::Oss::RequestError => e
24
+ puts "Bucket create fail", e.code, e.message, e.request_id
25
+ end
26
+
27
+ You can specify bucket name, location(default 'oss-cn-hangzhou') and acl(default: 'private') when create new bucket.
28
+
29
+
30
+ ### List all buckets
31
+
32
+ To get all buckets use Client#list_buckets:
33
+
34
+
35
+ require 'aliyun/oss'
36
+
37
+ access_key, secret_key = "your id", "your secret"
38
+ host = "oss-cn-hangzhou.aliyuncs.com"
39
+ client = Aliyun::Oss::Client.new(access_key, secret_key, host: host)
40
+
41
+ begin
42
+ buckets = client.buckets.list
43
+ rescue Aliyun::Oss::RequestError => e
44
+ puts "List Buckets fail", e.code, e.message, e.request_id
45
+ end
46
+
47
+ ### Set ACL
48
+
49
+ With Client#bucket_set_acl you can modify the ACL:
50
+
51
+ require 'aliyun/oss'
52
+
53
+ access_key, secret_key = "your id", "your secret"
54
+ host, bucket = "oss-cn-hangzhou.aliyuncs.com", "bucket-name"
55
+ client = Aliyun::Oss::Client.new(access_key, secret_key, host: host, bucket: bucket)
56
+
57
+ bucket = Aliyun::Oss::Struct::Bucket.new(client: client)
58
+ # supported value: public-read-write | public-read | private
59
+ begin
60
+ bucket.set_acl('public-read')
61
+ rescue Aliyun::Oss::RequestError => e
62
+ puts "Set ACL fail", e.code, e.message, e.request_id
63
+ end
64
+
65
+ Now, it support public-read-write | public-read | private, more detail visit: [Bucket ACL](https://docs.aliyun.com/#/pub/oss/product-documentation/acl&bucket-acl)
66
+
67
+
68
+ ### Get ACL
69
+
70
+ To get current ACL of Bucket, use Client#bucket_get_acl:
71
+
72
+ require 'aliyun/oss'
73
+
74
+ access_key, secret_key = "your id", "your secret"
75
+ host, bucket = "oss-cn-hangzhou.aliyuncs.com", "bucket-name"
76
+ client = Aliyun::Oss::Client.new(access_key, secret_key, host: host, bucket: bucket)
77
+
78
+ bucket = Aliyun::Oss::Struct::Bucket.new(client: client)
79
+ begin
80
+ acl = bucket.acl!
81
+ rescue Aliyun::Oss::RequestError => e
82
+ puts "Get ACL fail", e.code, e.message, e.request_id
83
+ end
84
+
85
+ ### Get Bucket Location
86
+
87
+ Get bucket's data center location, use Client#bucket_get_location:
88
+
89
+ require 'aliyun/oss'
90
+
91
+ access_key, secret_key = "your id", "your secret"
92
+ host, bucket = "oss-cn-hangzhou.aliyuncs.com", "bucket-name"
93
+ client = Aliyun::Oss::Client.new(access_key, secret_key, host: host, bucket: bucket)
94
+
95
+ bucket = Aliyun::Oss::Struct::Bucket.new(client: client)
96
+ begin
97
+ location = bucket.location!
98
+ rescue Aliyun::Oss::RequestError => e
99
+ puts "Get Location fail", e.code, e.message, e.request_id
100
+ end
101
+
102
+ To get more bucket information, visit Bucket#xxx! methods [here](http://www.rubydoc.info/gems/aliyun-oss-sdk/0.1.1/Aliyun/Oss/Struct/Bucket).
103
+
104
+
105
+ ### Delete Bucket
106
+
107
+ If you do need one bucket, delete it with Client#bucket_delete:
108
+
109
+ require 'aliyun/oss'
110
+
111
+ access_key, secret_key = "your id", "your secret"
112
+ host = "oss-cn-hangzhou.aliyuncs.com"
113
+ client = Aliyun::Oss::Client.new(access_key, secret_key, host: host)
114
+
115
+ begin
116
+ client.buckets.delete("deleted-bucket-name")
117
+ rescue Aliyun::Oss::RequestError => e
118
+ puts "Delete Bucket fail", e.code, e.message, e.request_id
119
+ end
120
+
121
+ Note: when the bucket is not empty(existing object or [Multipart Uploaded](./multipart.md) parts), the delete will fail.
122
+
123
+
124
+ OK, Let's visit [Objects](./object.md)
125
+
@@ -0,0 +1,71 @@
1
+ ## CORS
2
+
3
+ CORS allow web application visit resources not belongs it's domain. OSS provide interface to help developer control the premissions.
4
+
5
+
6
+ ### Set CORS
7
+
8
+
9
+ With Bucket#enable_cors, you can set cors easily:
10
+
11
+ require 'aliyun/oss'
12
+
13
+ access_key, secret_key = "your id", "your secret"
14
+ host = "oss-cn-hangzhou.aliyuncs.com"
15
+ bucket = "bucket-name"
16
+ client = Aliyun::Oss::Client.new(access_key, secret_key, host: host, bucket: bucket)
17
+
18
+ begin
19
+ bucket = Aliyun::Oss::Struct::Bucket.new(client: client)
20
+ rule = Aliyun::Oss::Struct::Cors.new(allowed_methods: ['get'], allowed_origins: ['*'])
21
+ bucket.enable_cors([rule])
22
+ rescue Aliyun::Oss::RequestError => e
23
+ puts "Set CORS fail", e.code, e.message, e.request_id
24
+ end
25
+
26
+ More about the rules, visit [OSS API](https://docs.aliyun.com/#/pub/oss/api-reference/cors&PutBucketcors) and [Struct::Cors](http://www.rubydoc.info/gems/aliyun-oss-sdk/0.1.1/Aliyun/Oss/Struct/Cors)
27
+
28
+
29
+ ### Get CORS Rules
30
+
31
+ To get current cors rules, you can use Client#bucket_get_cors:
32
+
33
+
34
+ require 'aliyun/oss'
35
+
36
+ access_key, secret_key = "your id", "your secret"
37
+ host = "oss-cn-hangzhou.aliyuncs.com"
38
+ bucket = "bucket-name"
39
+ client = Aliyun::Oss::Client.new(access_key, secret_key, host: host, bucket: bucket)
40
+
41
+ begin
42
+ bucket = Aliyun::Oss::Struct::Bucket.new(client: client)
43
+ cors = bucket.cors!
44
+ rescue Aliyun::Oss::RequestError => e
45
+ puts "Get CORS fail", e.code, e.message, e.request_id
46
+ end
47
+
48
+
49
+ ### Disable CORS
50
+
51
+ If you want to diable CORS, just like below:
52
+
53
+ require 'aliyun/oss'
54
+
55
+ access_key, secret_key = "your id", "your secret"
56
+ host = "oss-cn-hangzhou.aliyuncs.com"
57
+ bucket = "bucket-name"
58
+ client = Aliyun::Oss::Client.new(access_key, secret_key, host: host, bucket: bucket)
59
+
60
+ # create a private bucket on oss-cn-beijing
61
+ begin
62
+ bucket = Aliyun::Oss::Struct::Bucket.new(client: client)
63
+ bucket.disable_cors
64
+ rescue Aliyun::Oss::RequestError => e
65
+ puts "Disable CORS fail", e.code, e.message, e.request_id
66
+ end
67
+
68
+ Note: disable CORS will remove all existing CORS Rules.
69
+
70
+
71
+ Now, Let's go to next section: [LifeCycle](./lifecycle.md)