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.
- data/README.md +137 -151
- data/aliyun-oss.gemspec +1 -2
- data/lib/aliyun/oss/api/bucket_multiparts.rb +184 -0
- data/lib/aliyun/oss/api/bucket_objects.rb +204 -0
- data/lib/aliyun/oss/api/bucket_property.rb +246 -0
- data/lib/aliyun/oss/api/buckets.rb +89 -0
- data/lib/aliyun/oss/authorization.rb +21 -32
- data/lib/aliyun/oss/client.rb +11 -683
- data/lib/aliyun/oss/client/bucket_objects.rb +5 -6
- data/lib/aliyun/oss/client/buckets.rb +11 -5
- data/lib/aliyun/oss/http.rb +1 -1
- data/lib/aliyun/oss/struct/bucket.rb +4 -0
- data/lib/aliyun/oss/struct/directory.rb +25 -0
- data/lib/aliyun/oss/struct/file.rb +8 -0
- data/lib/aliyun/oss/struct/multipart.rb +2 -2
- data/lib/aliyun/oss/struct/object.rb +22 -1
- data/lib/aliyun/oss/struct/part.rb +2 -8
- data/lib/aliyun/oss/utils.rb +6 -0
- data/lib/aliyun/oss/version.rb +1 -1
- data/todo.md +1 -0
- data/wiki/multipart.md +3 -3
- data/wiki/object_based/bucket.md +125 -0
- data/wiki/object_based/cors.md +71 -0
- data/wiki/object_based/error.md +70 -0
- data/wiki/object_based/get_start.md +93 -0
- data/wiki/object_based/installation.md +15 -0
- data/wiki/object_based/lifecycle.md +88 -0
- data/wiki/object_based/multipart.md +133 -0
- data/wiki/object_based/object.md +324 -0
- metadata +69 -43
- checksums.yaml +0 -7
@@ -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
|
-
|
21
|
-
|
22
|
-
|
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 [
|
101
|
+
# @return [HTTParty::Response::Headers]
|
103
102
|
#
|
104
103
|
# @see Client#bucket_append_object
|
105
104
|
def append(*args)
|
106
|
-
|
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
|
-
|
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
|
data/lib/aliyun/oss/http.rb
CHANGED
@@ -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
|
@@ -28,9 +28,9 @@ module Aliyun
|
|
28
28
|
#
|
29
29
|
# @raise (see #bucket_multipart_upload)
|
30
30
|
#
|
31
|
-
# @return [
|
31
|
+
# @return [HTTParty::Response::Headers]
|
32
32
|
def upload(*args)
|
33
|
-
|
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 [
|
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
|
{
|
data/lib/aliyun/oss/utils.rb
CHANGED
data/lib/aliyun/oss/version.rb
CHANGED
data/todo.md
CHANGED
data/wiki/multipart.md
CHANGED
@@ -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,
|
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",
|
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"
|
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)
|