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
@@ -0,0 +1,70 @@
|
|
1
|
+
## Error
|
2
|
+
|
3
|
+
### Handle Error
|
4
|
+
|
5
|
+
If a error occurs when visit the OSS, the OSS will be return a error code and error message, making it easy for users to locate problems, and make the appropriate treatment. For code not 2XX, you can get information:
|
6
|
+
|
7
|
+
require 'aliyun/oss'
|
8
|
+
|
9
|
+
client = Aliyun::OSS::Client.new('ACCESS_KEY', 'SECRET_KEY', host: 'oss-cn-hangzhou.aliyuncs.com', bucket: 'oss-sdk-dev-hangzhou')
|
10
|
+
|
11
|
+
begin
|
12
|
+
client.buckets.create("invalid_bucket_name")
|
13
|
+
rescue Aliyun::Oss::RequestError => e
|
14
|
+
puts "Code: #{e.code}"
|
15
|
+
puts "Message: #{e.message}"
|
16
|
+
puts "Request id: #{e.request_id}"
|
17
|
+
end
|
18
|
+
|
19
|
+
Here,
|
20
|
+
|
21
|
+
+ Code: the error code
|
22
|
+
+ Message: the error message
|
23
|
+
+ requestId: It's the UUID to uniquely identifies this request; When you can't solve the problem, can the RequestId to request help from the OSS development engineer.
|
24
|
+
|
25
|
+
|
26
|
+
# Error Code
|
27
|
+
|
28
|
+
| code | summary | HTTP Status|
|
29
|
+
|---|---|
|
30
|
+
|AccessDenied |Access denied | 403|
|
31
|
+
|BucketAlreadyExists | Bucket Already Exist| 409|
|
32
|
+
|BucketNotEmpty |Bucket Not Empty| 409|
|
33
|
+
|EntityTooLarge | Entry Too Large| 400|
|
34
|
+
|EntityTooSmall | Entry Too Small| 400|
|
35
|
+
|FileGroupTooLarge |File Group Too Large| 400|
|
36
|
+
|InvalidLinkName | Object Link Same With Object| 400|
|
37
|
+
|LinkPartNotExist | Object Not Exist| 400|
|
38
|
+
|ObjectLinkTooLarge | Object Too Much | 400|
|
39
|
+
|FieldItemTooLong | Field Too Large| 400|
|
40
|
+
|FilePartInterity | File Part Already Changed| 400|
|
41
|
+
|FilePartNotExist |File Part Not Exist| 400|
|
42
|
+
|FilePartStale | File Part Expired| 400|
|
43
|
+
|IncorrectNumberOfFilesInPOSTRequest| File Count Invalid| 400|
|
44
|
+
|InvalidArgument |Invalid Argument| 400|
|
45
|
+
|InvalidAccessKeyId | Access Key ID Not Exist| 403|
|
46
|
+
|InvalidBucketName | The specified bucket is not valid.| 400|
|
47
|
+
|InvalidDigest | Invalid Digest | 400|
|
48
|
+
|InvalidEncryptionAlgorithmError | Specified Encoding-Type Error | 400|
|
49
|
+
|InvalidObjectName |Invalid Object Name| 400
|
50
|
+
|InvalidPart | Invalid Part| 400|
|
51
|
+
|InvalidPartOrder |Invalid Part Order| 400|
|
52
|
+
|InvalidPolicyDocument | Invalid Policy| 400|
|
53
|
+
|InvalidTargetBucketForLogging |Invalid Target Bucket For Logging| 400|
|
54
|
+
|InternalError |Internal Error| 500|
|
55
|
+
|MalformedXML | XML Invalid| 400|
|
56
|
+
|MalformedPOSTRequest | Requested XML Invalid | 400|
|
57
|
+
|MaxPOSTPreDataLengthExceededError | Body except file Too Large | 400|
|
58
|
+
|MethodNotAllowed |Method Not Allowed| 405|
|
59
|
+
|MissingArgument |Missing Argument| 411|
|
60
|
+
|MissingContentLength |Missing Content Length| 411|
|
61
|
+
|NoSuchBucket |No Such Bucket| 404|
|
62
|
+
|NoSuchKey |No Such Key| 404|
|
63
|
+
|NoSuchUpload |Multipart Upload ID Not Exist| 404|
|
64
|
+
|NotImplemented |Not Implemented| 501|
|
65
|
+
|PreconditionFailed |Precondition Failed| 412|
|
66
|
+
|RequestTimeTooSkewed |Request Time Large Than 15 minutes| 403|
|
67
|
+
|RequestTimeout |Request Timeout| 400|
|
68
|
+
|RequestIsNotMultiPartContent | Content-Type Invalid| 400|
|
69
|
+
|SignatureDoesNotMatch |Signature Does Not Match|403|
|
70
|
+
|TooManyBuckets |Too Many Buckets| 400|
|
@@ -0,0 +1,93 @@
|
|
1
|
+
## Getting started
|
2
|
+
|
3
|
+
Here, you can know how to do some basic operation with Aliyun OSS SDK.
|
4
|
+
|
5
|
+
|
6
|
+
### Step-1. Init a client
|
7
|
+
|
8
|
+
Mostly OSS API are handled by [Aliyun::Oss::Client](http://www.rubydoc.info/gems/aliyun-oss-sdk/Aliyun/Oss/Client) class, Let's create a instance first:
|
9
|
+
|
10
|
+
require 'aliyun/oss'
|
11
|
+
|
12
|
+
access_key, secret_key = "your id", "your secret"
|
13
|
+
host = "oss-cn-hangzhou.aliyuncs.com"
|
14
|
+
bucket = "bucket-name"
|
15
|
+
client = Aliyun::Oss::Client.new(access_key, secret_key, host: host, bucket: bucket)
|
16
|
+
|
17
|
+
Here, access_key/secret_key is is your access credentials, Aliyun provide three ways to get access credentials, get more information [here](https://docs.aliyun.com/#/pub/oss/product-documentation/acl&RESTAuthentication).
|
18
|
+
|
19
|
+
|
20
|
+
### Step-2. Create Bucket
|
21
|
+
|
22
|
+
Buckets are global object in OSS, so find a uniqueness name for your bucket, Or it fail when create. It can used to store objects. Now, we create a bucket:
|
23
|
+
|
24
|
+
require 'aliyun/oss'
|
25
|
+
|
26
|
+
access_key, secret_key = "your id", "your secret"
|
27
|
+
host = "oss-cn-hangzhou.aliyuncs.com"
|
28
|
+
client = Aliyun::Oss::Client.new(access_key, secret_key, host: host)
|
29
|
+
|
30
|
+
# create a private bucket on oss-cn-beijing
|
31
|
+
begin
|
32
|
+
client.buckets.create('new-bucket', 'oss-cn-beijing', 'private')
|
33
|
+
rescue Aliyun::Oss::RequestError => e
|
34
|
+
puts "Bucket create fail", e.code, e.message, e.request_id
|
35
|
+
end
|
36
|
+
|
37
|
+
|
38
|
+
### Step-3 Upload Object
|
39
|
+
|
40
|
+
Object is the most basic unit of data in OSS, you can simple imagine it's just a file. here, we upload a object to OSS:
|
41
|
+
|
42
|
+
require 'aliyun/oss'
|
43
|
+
|
44
|
+
access_key, secret_key = "your id", "your secret"
|
45
|
+
host, bucket = "oss-cn-hangzhou.aliyuncs.com", "bucket-name"
|
46
|
+
client = Aliyun::Oss::Client.new(access_key, secret_key, host: host, bucket: bucket)
|
47
|
+
|
48
|
+
file = File.new("path/to/test.txt")
|
49
|
+
begin
|
50
|
+
client.bucket_objects.create("test.txt", file)
|
51
|
+
rescue Aliyun::Oss::RequestError => e
|
52
|
+
puts "Upload Object fail", e.code, e.message, e.request_id
|
53
|
+
end
|
54
|
+
|
55
|
+
### Step-4 list all object
|
56
|
+
|
57
|
+
After you complete some upload, maybe you want to list the objects in the bucket:
|
58
|
+
|
59
|
+
|
60
|
+
require 'aliyun/oss'
|
61
|
+
|
62
|
+
access_key, secret_key = "your id", "your secret"
|
63
|
+
host, bucket = "oss-cn-hangzhou.aliyuncs.com", "bucket-name"
|
64
|
+
client = Aliyun::Oss::Client.new(access_key, secret_key, host: host, bucket: bucket)
|
65
|
+
|
66
|
+
begin
|
67
|
+
objects = client.bucket_objects.list
|
68
|
+
rescue Aliyun::Oss::RequestError => e
|
69
|
+
puts "Cannot list objects", e.code, e.message, e.request_id
|
70
|
+
end
|
71
|
+
|
72
|
+
With correct parameters, you can get more flexible result. you can get detailed Paramters [here](http://www.rubydoc.info/gems/aliyun-oss-sdk/Aliyun%2FOss%2FClient%3Abucket_list_objects).
|
73
|
+
|
74
|
+
|
75
|
+
### Step-5. Get special object
|
76
|
+
|
77
|
+
Now, you want to get a special object:
|
78
|
+
|
79
|
+
require 'aliyun/oss'
|
80
|
+
|
81
|
+
access_key, secret_key = "your id", "your secret"
|
82
|
+
host, bucket = "oss-cn-hangzhou.aliyuncs.com", "bucket-name"
|
83
|
+
client = Aliyun::Oss::Client.new(access_key, secret_key, host: host, bucket: bucket)
|
84
|
+
|
85
|
+
begin
|
86
|
+
body = client.bucket_objects.get("test.txt")
|
87
|
+
# save the response to your local file system
|
88
|
+
File.open("test.txt", "wb") { |f| f << body.read }
|
89
|
+
rescue Aliyun::Oss::RequestError => e
|
90
|
+
puts "Get object fail", e.code, e.message, e.request_id
|
91
|
+
end
|
92
|
+
|
93
|
+
Next, Visit more about [Bucket](./bucket.md)
|
@@ -0,0 +1,15 @@
|
|
1
|
+
## Installation
|
2
|
+
|
3
|
+
It's a Ruby Gem, so you can install it like any Gem:
|
4
|
+
|
5
|
+
$ gem install aliyun-oss-sdk
|
6
|
+
|
7
|
+
If you use Gemfile manage your Gems, Add below to your Gemfile.
|
8
|
+
|
9
|
+
gem "aliyun-oss-sdk"
|
10
|
+
|
11
|
+
And run:
|
12
|
+
|
13
|
+
$ bundle install
|
14
|
+
|
15
|
+
Now, [Getting started](./get_start.md)
|
@@ -0,0 +1,88 @@
|
|
1
|
+
## LifeCycle
|
2
|
+
|
3
|
+
OSS provide LifeCycle to help user manage lifecycle of object. User can create LifeCycle rules to manage objects. At present, user can create rule to auto delete Objects. Each rule is composed by following several parts:
|
4
|
+
|
5
|
+
+ Prefix of Object name, only match the prefix will apply this rule.
|
6
|
+
+ Operation, user want to take for the matched objects.
|
7
|
+
+ Date or Days, which user can specify expired date or days to expire.
|
8
|
+
|
9
|
+
|
10
|
+
### Set LifeCycle
|
11
|
+
|
12
|
+
|
13
|
+
In the LifeCycle, you can contains 1000 rules at max.
|
14
|
+
|
15
|
+
Each rule contains:
|
16
|
+
|
17
|
+
+ ID: each rule ID must keep uniqueness and cannot contain others(eg: abc and abcd).
|
18
|
+
+ Prefix: it can used to apply rule for object with the prefix
|
19
|
+
+ Status: defined the status for the rule, Only support Enabled and Disabled.
|
20
|
+
+ Expiration: Date or Days, used to specify expired date or specify expired after x days from last modified date.
|
21
|
+
|
22
|
+
In our Library, to use Struct::LifeCycle to define a rule:
|
23
|
+
|
24
|
+
# Define a rule to auto delete objects with prefix: logs-prod- after 7 days since last modified date
|
25
|
+
rule1 = Aliyun::Oss::Struct::LifeCycle.new({ prefix: 'logs-prod-', days: 7, enable: true })
|
26
|
+
|
27
|
+
# Define a rule to auto delete objects with prefix: logs-dev- at Time.now + 24*60*60
|
28
|
+
rule2 = Aliyun::Oss::Struct::LifeCycle.new({ prefix: 'logs-dev', date: Time.now + 24*60*60, enable: true })
|
29
|
+
|
30
|
+
|
31
|
+
To set your LifeCycle with this rules:
|
32
|
+
|
33
|
+
require 'aliyun/oss'
|
34
|
+
|
35
|
+
access_key, secret_key = "your id", "your secret"
|
36
|
+
host = "oss-cn-hangzhou.aliyuncs.com"
|
37
|
+
bucket = "bucket-name"
|
38
|
+
client = Aliyun::Oss::Client.new(access_key, secret_key, host: host, bucket: bucket)
|
39
|
+
|
40
|
+
begin
|
41
|
+
bucket = Aliyun::Oss::Struct::Bucket.new(client: client)
|
42
|
+
bucket.enable_lifecycle([rule1, rule2])
|
43
|
+
rescue Aliyun::Oss::RequestError => e
|
44
|
+
puts "Enable Lifecycle fail", e.code, e.message, e.request_id
|
45
|
+
end
|
46
|
+
|
47
|
+
### Get LifeCycle
|
48
|
+
|
49
|
+
To get LifeCycle for bucket, use Client#bucket_get_lifecycle:
|
50
|
+
|
51
|
+
require 'aliyun/oss'
|
52
|
+
|
53
|
+
access_key, secret_key = "your id", "your secret"
|
54
|
+
host = "oss-cn-hangzhou.aliyuncs.com"
|
55
|
+
bucket = "bucket-name"
|
56
|
+
client = Aliyun::Oss::Client.new(access_key, secret_key, host: host, bucket: bucket)
|
57
|
+
|
58
|
+
begin
|
59
|
+
bucket = Aliyun::Oss::Struct::Bucket.new(client: client)
|
60
|
+
bucket.lifecycle!
|
61
|
+
rescue Aliyun::Oss::RequestError => e
|
62
|
+
puts "Enable Lifecycle fail", e.code, e.message, e.request_id
|
63
|
+
end
|
64
|
+
|
65
|
+
|
66
|
+
### Disable LifeCycle
|
67
|
+
|
68
|
+
|
69
|
+
With Client#bucket_disable_lifecycle, you can disable LifeCycle:
|
70
|
+
|
71
|
+
|
72
|
+
require 'aliyun/oss'
|
73
|
+
|
74
|
+
access_key, secret_key = "your id", "your secret"
|
75
|
+
host = "oss-cn-hangzhou.aliyuncs.com"
|
76
|
+
bucket = "bucket-name"
|
77
|
+
client = Aliyun::Oss::Client.new(access_key, secret_key, host: host, bucket: bucket)
|
78
|
+
|
79
|
+
begin
|
80
|
+
bucket = Aliyun::Oss::Struct::Bucket.new(client: client)
|
81
|
+
bucket.disable_lifecycle
|
82
|
+
rescue Aliyun::Oss::RequestError => e
|
83
|
+
puts "Disable Lifecycle fail", e.code, e.message, e.request_id
|
84
|
+
end
|
85
|
+
|
86
|
+
|
87
|
+
Next, Let's discuss about [Error](./error.md)
|
88
|
+
|
@@ -0,0 +1,133 @@
|
|
1
|
+
## Multipart Upload
|
2
|
+
|
3
|
+
Besides simple upload via put, OSS provide another way to upload large file -- Multipart Upload, Here we list some normal application scenarios:
|
4
|
+
|
5
|
+
+ To support breakpoint upload
|
6
|
+
+ Upload file large than 100 MB
|
7
|
+
+ Network is bad, and the connection between the OSS server often disconnected
|
8
|
+
+ Upload a file before, unable to determine the size of the uploaded files
|
9
|
+
|
10
|
+
|
11
|
+
Now, Let's start party!
|
12
|
+
|
13
|
+
|
14
|
+
### Initialize
|
15
|
+
|
16
|
+
Before start a Multipart Upload, we need first initialize a event:
|
17
|
+
|
18
|
+
|
19
|
+
require 'aliyun/oss'
|
20
|
+
|
21
|
+
access_key, secret_key = "your id", "your secret"
|
22
|
+
host = "oss-cn-hangzhou.aliyuncs.com"
|
23
|
+
bucket = "bucket-name"
|
24
|
+
client = Aliyun::Oss::Client.new(access_key, secret_key, host: host, bucket: bucket)
|
25
|
+
|
26
|
+
begin
|
27
|
+
# Step-1 Init a Multipart Upload
|
28
|
+
multipart = client.bucket_multiparts.init("Exciting-Ruby.mp4", { 'Content-Type' => 'video/mp4' })
|
29
|
+
puts "Upload ID: #{multipart.upload_id}"
|
30
|
+
rescue Aliyun::Oss::RequestError => e
|
31
|
+
puts "Init Multipart fail", e.code, e.message, e.request_id
|
32
|
+
end
|
33
|
+
|
34
|
+
Upload ID is the UUID for the Multipart Upload Event, store it for use later.
|
35
|
+
|
36
|
+
### Upload Part from local
|
37
|
+
|
38
|
+
require 'aliyun/oss'
|
39
|
+
|
40
|
+
access_key, secret_key = "your id", "your secret"
|
41
|
+
host = "oss-cn-hangzhou.aliyuncs.com"
|
42
|
+
bucket = "bucket-name"
|
43
|
+
client = Aliyun::Oss::Client.new(access_key, secret_key, host: host, bucket: bucket)
|
44
|
+
|
45
|
+
begin
|
46
|
+
multipart = client.bucket_multiparts.init("Exciting-Ruby.mp4", { 'Content-Type' => 'video/mp4' })
|
47
|
+
headers = multipart.upload("Exciting-Ruby.mp4", 1, file_or_bin)
|
48
|
+
puts "etag: #{headers['etag']}"
|
49
|
+
rescue Aliyun::Oss::RequestError => e
|
50
|
+
puts "Upload to Multipart fail", e.code, e.message, e.request_id
|
51
|
+
end
|
52
|
+
|
53
|
+
Store the etag, it will used for complete a Multipart Upload.
|
54
|
+
|
55
|
+
It can used to upload part to a object. Please note:
|
56
|
+
|
57
|
+
+ Multipart Upload requirements every parts greater than 100 KB except last
|
58
|
+
+ In order to ensure that data safe when network transmission, strongly recommend to include meta: content-md5, after receiving the data, OSS using the md5 value to prove the validity of the upload data, if they are inconsistent returns InvalidDigest.
|
59
|
+
+ The Part number range is 1~10000. If beyond this range, the OSS will return InvalidArgument.
|
60
|
+
+ If you upload from the same file, be careful for the upload position
|
61
|
+
|
62
|
+
### Complete Multipart Upload
|
63
|
+
|
64
|
+
require 'aliyun/oss'
|
65
|
+
|
66
|
+
access_key, secret_key = "your id", "your secret"
|
67
|
+
host = "oss-cn-hangzhou.aliyuncs.com"
|
68
|
+
bucket = "bucket-name"
|
69
|
+
client = Aliyun::Oss::Client.new(access_key, secret_key, host: host, bucket: bucket)
|
70
|
+
|
71
|
+
part1 = Aliyun::Oss::Struct::Part.new({ number: 1, etag: 'etag1' })
|
72
|
+
part2 = Aliyun::Oss::Struct::Part.new({ number: 2, etag: 'etag2' })
|
73
|
+
part3 = Aliyun::Oss::Struct::Part.new({ number: 3, etag: 'etag3' })
|
74
|
+
begin
|
75
|
+
multipart.complete([part1, part2, part3])
|
76
|
+
rescue Aliyun::Oss::RequestError => e
|
77
|
+
puts "Complete Multipart fail", e.code, e.message, e.request_id
|
78
|
+
end
|
79
|
+
|
80
|
+
|
81
|
+
Here, we create Aliyun::Oss::Struct::Part to build your part, use Part#valid? to valid the object.
|
82
|
+
|
83
|
+
### Abort Multipart Upload
|
84
|
+
|
85
|
+
If some Problem occurs, you may want to abort a Multipart Upload:
|
86
|
+
|
87
|
+
require 'aliyun/oss'
|
88
|
+
|
89
|
+
access_key, secret_key = "your id", "your secret"
|
90
|
+
host = "oss-cn-hangzhou.aliyuncs.com"
|
91
|
+
bucket = "bucket-name"
|
92
|
+
client = Aliyun::Oss::Client.new(access_key, secret_key, host: host, bucket: bucket)
|
93
|
+
|
94
|
+
begin
|
95
|
+
multipart.abort
|
96
|
+
rescue Aliyun::Oss::RequestError => e
|
97
|
+
puts "Upload to Multipart fail", e.code, e.message, e.request_id
|
98
|
+
end
|
99
|
+
|
100
|
+
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.
|
101
|
+
|
102
|
+
### List Multipart Upload
|
103
|
+
|
104
|
+
To get all Multipart Upload in this Bucket:
|
105
|
+
|
106
|
+
require 'aliyun/oss'
|
107
|
+
|
108
|
+
access_key, secret_key = "your id", "your secret"
|
109
|
+
host = "oss-cn-hangzhou.aliyuncs.com"
|
110
|
+
bucket = "bucket-name"
|
111
|
+
client = Aliyun::Oss::Client.new(access_key, secret_key, host: host, bucket: bucket)
|
112
|
+
|
113
|
+
multiparts = client.bucket_multiparts.list
|
114
|
+
|
115
|
+
Same with all other list method, it support prefix, delimiter, marker to get flexible results.
|
116
|
+
|
117
|
+
|
118
|
+
### List Uploaded Parts
|
119
|
+
|
120
|
+
Sometimes, you want to know which parts are uploaded.
|
121
|
+
|
122
|
+
require 'aliyun/oss'
|
123
|
+
|
124
|
+
access_key, secret_key = "your id", "your secret"
|
125
|
+
host = "oss-cn-hangzhou.aliyuncs.com"
|
126
|
+
bucket = "bucket-name"
|
127
|
+
client = Aliyun::Oss::Client.new(access_key, secret_key, host: host, bucket: bucket)
|
128
|
+
|
129
|
+
parts = multipart.list_parts
|
130
|
+
|
131
|
+
|
132
|
+
OK, It's time to visit [CORS](./cors.md)
|
133
|
+
|
@@ -0,0 +1,324 @@
|
|
1
|
+
## Object
|
2
|
+
|
3
|
+
In Aliyun OSS, the basic data unit that user operation is the Object. A single Object maximum size varies according to the way of uploading data, Put the Object way most can't more than 5 GB, using multipart Object way to upload size must not exceed 48.8 TB. Object contains the key, meta, and data. Among them, the key is the name of the Object. Meta is user's description of the object consists of a series of name-value pairs; The data is the object data.
|
4
|
+
|
5
|
+
|
6
|
+
## Name Spec
|
7
|
+
|
8
|
+
+ Use utf-8 encoding
|
9
|
+
+ Length must be between 1 to 1023 bytes
|
10
|
+
+ Don't begin with "/" or "\" character
|
11
|
+
+ Can not contain "\r" or "\n"
|
12
|
+
|
13
|
+
|
14
|
+
## Upload Object
|
15
|
+
|
16
|
+
### Simple upload
|
17
|
+
|
18
|
+
BucketObjects#create support file or bin data to upload.
|
19
|
+
|
20
|
+
require 'aliyun/oss'
|
21
|
+
|
22
|
+
access_key, secret_key = "your id", "your secret"
|
23
|
+
host = "oss-cn-hangzhou.aliyuncs.com"
|
24
|
+
bucket = "bucket-name"
|
25
|
+
client = Aliyun::Oss::Client.new(access_key, secret_key, host: host, bucket: bucket)
|
26
|
+
|
27
|
+
file = File.new("path/to/image.png")
|
28
|
+
begin
|
29
|
+
client.bucket_objects.create("image.png", file, { 'Content-Type' => 'image/png' })
|
30
|
+
rescue Aliyun::Oss::RequestError => e
|
31
|
+
puts "Create Object fail", e.code, e.message, e.request_id
|
32
|
+
end
|
33
|
+
|
34
|
+
begin
|
35
|
+
client.bucket_objects.create("hello.txt", "Hello World", { 'Content-Type' => 'text/plain' })
|
36
|
+
rescue Aliyun::Oss::RequestError => e
|
37
|
+
puts "Create Object fail", e.code, e.message, e.request_id
|
38
|
+
end
|
39
|
+
|
40
|
+
|
41
|
+
The Upload limit Data to 5 GB, if large than, visit [Multipart Upload](./multipart.md).
|
42
|
+
|
43
|
+
|
44
|
+
### Create a folder
|
45
|
+
|
46
|
+
To Create a folder, it's easy, just pass key end with "/":
|
47
|
+
|
48
|
+
require 'aliyun/oss'
|
49
|
+
|
50
|
+
access_key, secret_key = "your id", "your secret"
|
51
|
+
host = "oss-cn-hangzhou.aliyuncs.com"
|
52
|
+
bucket = "bucket-name"
|
53
|
+
client = Aliyun::Oss::Client.new(access_key, secret_key, host: host, bucket: bucket)
|
54
|
+
|
55
|
+
begin
|
56
|
+
client.bucket_objects.create("images/", "")
|
57
|
+
rescue Aliyun::Oss::RequestError => e
|
58
|
+
puts "Create folder fail", e.code, e.message, e.request_id
|
59
|
+
end
|
60
|
+
|
61
|
+
Create simulations folder nature created a object with size equals 0. Uploads and downloads, for this object can only console to end with "/" object to display the folder. So the user can use this way to implement to create simulation folder. And access to the folder can see files below the folder.
|
62
|
+
|
63
|
+
|
64
|
+
### Customize Http Header for object
|
65
|
+
|
66
|
+
OSS allow users to customize the http headers of object. The following code set the expiration time for the Object:
|
67
|
+
|
68
|
+
require 'aliyun/oss'
|
69
|
+
|
70
|
+
access_key, secret_key = "your id", "your secret"
|
71
|
+
host = "oss-cn-hangzhou.aliyuncs.com"
|
72
|
+
bucket = "bucket-name"
|
73
|
+
client = Aliyun::Oss::Client.new(access_key, secret_key, host: host, bucket: bucket)
|
74
|
+
|
75
|
+
file = File.new("path/to/image.png")
|
76
|
+
begin
|
77
|
+
client.bucket_objects.create("image.png", file, { 'Content-Type' => 'image/png', "Expires" => "Sun, 25 Oct 2015 05:38:42 GMT" })
|
78
|
+
rescue Aliyun::Oss::RequestError => e
|
79
|
+
puts "Create Object fail", e.code, e.message, e.request_id
|
80
|
+
end
|
81
|
+
|
82
|
+
Except Expires, it also support Cache-Control, Content-Disposition, Content-Encoding, Content-MD5, more details visit: [BucketObjects#create](http://www.rubydoc.info/gems/aliyun-oss-sdk/0.1.1/Aliyun/Oss/Client/BucketObjects#create-instance_method).
|
83
|
+
|
84
|
+
|
85
|
+
### Set User Meta
|
86
|
+
|
87
|
+
OSS Support set some user meta information for object. Here we set x-oss-meta-user to username for object:
|
88
|
+
|
89
|
+
require 'aliyun/oss'
|
90
|
+
|
91
|
+
access_key, secret_key = "your id", "your secret"
|
92
|
+
host = "oss-cn-hangzhou.aliyuncs.com"
|
93
|
+
bucket = "bucket-name"
|
94
|
+
client = Aliyun::Oss::Client.new(access_key, secret_key, host: host, bucket: bucket)
|
95
|
+
|
96
|
+
file = File.new("path/to/image.png")
|
97
|
+
begin
|
98
|
+
client.bucket_objects.create("image.png", file, { 'Content-Type' => 'image/png', 'x-oss-meta-user' => 'baymax' })
|
99
|
+
rescue Aliyun::Oss::RequestError => e
|
100
|
+
puts "Create Object fail", e.code, e.message, e.request_id
|
101
|
+
end
|
102
|
+
|
103
|
+
|
104
|
+
user meta information stored as headers with prefix: "x-oss-meta", the maxinum limit is 2KB for all meta information.
|
105
|
+
|
106
|
+
Note: the user meta key is case-insensitive, but value is case-sensitive.
|
107
|
+
|
108
|
+
|
109
|
+
### Append Upload
|
110
|
+
|
111
|
+
OSS Allow users to append data to a object, but only for appendable object, Objects created with Append Upload is Appendable object, Upload via Simple Upload is Normal object:
|
112
|
+
|
113
|
+
|
114
|
+
require 'aliyun/oss'
|
115
|
+
|
116
|
+
access_key, secret_key = "your id", "your secret"
|
117
|
+
host = "oss-cn-hangzhou.aliyuncs.com"
|
118
|
+
bucket = "bucket-name"
|
119
|
+
client = Aliyun::Oss::Client.new(access_key, secret_key, host: host, bucket: bucket)
|
120
|
+
|
121
|
+
# Step-1 create a appendable object
|
122
|
+
begin
|
123
|
+
position = 0
|
124
|
+
headers = client.bucket_objects.append("secret.zip", "init information", position)
|
125
|
+
# Step-2 get next append position
|
126
|
+
position = headers['x-oss-next-append-position']
|
127
|
+
# Step-3 append upload
|
128
|
+
client.bucket_objects.append("secret.zip", "append information", position)
|
129
|
+
rescue Aliyun::Oss::RequestError => e
|
130
|
+
puts "Append Object fail", e.code, e.message, e.request_id
|
131
|
+
end
|
132
|
+
|
133
|
+
Users upload with Append mode, the most important is to set position correctly: When a user creates an appendable object, should set position to 0; When append content for the object, the postion is the current size of the object.
|
134
|
+
|
135
|
+
There are two ways to get the object size: one is the return of BucketObject#append. Another is invoke Object#meta! with the object.
|
136
|
+
|
137
|
+
Note: You can set meta information only when create appendable object(the first append). Later, if you want to change the meta, use BucketObject#copy(set source and destination to the same object).
|
138
|
+
|
139
|
+
|
140
|
+
|
141
|
+
## List objects in Bucket
|
142
|
+
|
143
|
+
|
144
|
+
### List Objects
|
145
|
+
|
146
|
+
require 'aliyun/oss'
|
147
|
+
|
148
|
+
access_key, secret_key = "your id", "your secret"
|
149
|
+
host = "oss-cn-hangzhou.aliyuncs.com"
|
150
|
+
bucket = "bucket-name"
|
151
|
+
client = Aliyun::Oss::Client.new(access_key, secret_key, host: host, bucket: bucket)
|
152
|
+
|
153
|
+
objects = client.bucket_objects.list
|
154
|
+
|
155
|
+
|
156
|
+
### More Parameters
|
157
|
+
|
158
|
+
the method support many Parameters to get flexible results:
|
159
|
+
|
160
|
+
require 'aliyun/oss'
|
161
|
+
|
162
|
+
access_key, secret_key = "your id", "your secret"
|
163
|
+
host = "oss-cn-hangzhou.aliyuncs.com"
|
164
|
+
bucket = "bucket-name"
|
165
|
+
client = Aliyun::Oss::Client.new(access_key, secret_key, host: host, bucket: bucket)
|
166
|
+
|
167
|
+
# list objects with prefix: pic and end with "/"
|
168
|
+
objects = client.bucket_objects.list(prefix: 'pic', delimiter: '/')
|
169
|
+
|
170
|
+
|
171
|
+
It list results with prefix: pic and end with "/", for example: "pic-people/". More about the Paramters, visit: [BucketObjects#list](http://www.rubydoc.info/gems/aliyun-oss-sdk/0.1.1/Aliyun/Oss/Client/BucketObjects#list-instance_method)
|
172
|
+
|
173
|
+
|
174
|
+
### Simulate Directory
|
175
|
+
|
176
|
+
In OSS, there is not real directory, the direcory we have seen actually is object with key end with /.
|
177
|
+
|
178
|
+
So, we can list objects by directory with prefix and delimiter.
|
179
|
+
|
180
|
+
For example, We have four objects:
|
181
|
+
|
182
|
+
+ fun/movie/001.avi
|
183
|
+
+ fun/movie/007.avi
|
184
|
+
+ fun/test.jpg
|
185
|
+
+ oss.jpg
|
186
|
+
|
187
|
+
There are two directory object: fun/ and fun/movie/;
|
188
|
+
|
189
|
+
Now, If we invoke:
|
190
|
+
|
191
|
+
1. list() will return 4 objects + two directory object;
|
192
|
+
2. list(prefix: 'fun/') will return first 3 objects and two directory object;
|
193
|
+
3. list(prefix: 'fun/', delimiter='/') will only return fun/test.jpg, fun/ and fun/movie/.
|
194
|
+
|
195
|
+
So we can use this two paramters to list objects by directory.
|
196
|
+
|
197
|
+
require 'aliyun/oss'
|
198
|
+
|
199
|
+
access_key, secret_key = "your id", "your secret"
|
200
|
+
host = "oss-cn-hangzhou.aliyuncs.com"
|
201
|
+
bucket = "bucket-name"
|
202
|
+
client = Aliyun::Oss::Client.new(access_key, secret_key, host: host, bucket: bucket)
|
203
|
+
|
204
|
+
# list objects with prefix: fun/ and end with "/"
|
205
|
+
results = client.bucket_objects.list(prefix: 'fun/', delimiter: '/')
|
206
|
+
|
207
|
+
results.each do |object|
|
208
|
+
if object.is_a?(Aliyun::Oss::Struct::Directory)
|
209
|
+
puts object.key
|
210
|
+
sub_objects = object.list(delimiter: '/')
|
211
|
+
else
|
212
|
+
puts object.key
|
213
|
+
end
|
214
|
+
end
|
215
|
+
|
216
|
+
Note: the results maybe instance of Aliyun::Oss::Struct::File or Aliyun::Oss::Struct::Directory, they are both subclass of Aliyun::Oss::Struct::Object, but Directory has method: #list to list objects under it.
|
217
|
+
|
218
|
+
|
219
|
+
### Get Object
|
220
|
+
|
221
|
+
require 'aliyun/oss'
|
222
|
+
|
223
|
+
access_key, secret_key = "your id", "your secret"
|
224
|
+
host = "oss-cn-hangzhou.aliyuncs.com"
|
225
|
+
bucket = "bucket-name"
|
226
|
+
client = Aliyun::Oss::Client.new(access_key, secret_key, host: host, bucket: bucket)
|
227
|
+
|
228
|
+
begin
|
229
|
+
body = client.bucket_objects.get('image.png')
|
230
|
+
File.open('image.png', 'wb') {|f| f.write body.read }
|
231
|
+
rescue Aliyun::Oss::RequestError => e
|
232
|
+
puts "Get Object fail", e.code, e.message, e.request_id
|
233
|
+
end
|
234
|
+
|
235
|
+
It Support Parameters, Range, If-Modified-Since, If-Unmodified-Since, If-Match, If-None-Match. With Range, we can get range data from a object, it's useful for download partly and so on.
|
236
|
+
|
237
|
+
|
238
|
+
### Get Meta Object
|
239
|
+
|
240
|
+
To get meta information of a object, use Client#get_meta_object:
|
241
|
+
|
242
|
+
require 'aliyun/oss'
|
243
|
+
|
244
|
+
access_key, secret_key = "your id", "your secret"
|
245
|
+
host = "oss-cn-hangzhou.aliyuncs.com"
|
246
|
+
bucket = "bucket-name"
|
247
|
+
client = Aliyun::Oss::Client.new(access_key, secret_key, host: host, bucket: bucket)
|
248
|
+
|
249
|
+
begin
|
250
|
+
headers = client.bucket_objects.meta!('image.png')
|
251
|
+
rescue Aliyun::Oss::RequestError => e
|
252
|
+
puts "Get Object meta information fail", e.code, e.message, e.request_id
|
253
|
+
end
|
254
|
+
|
255
|
+
### Delete Object
|
256
|
+
|
257
|
+
require 'aliyun/oss'
|
258
|
+
|
259
|
+
access_key, secret_key = "your id", "your secret"
|
260
|
+
host = "oss-cn-hangzhou.aliyuncs.com"
|
261
|
+
bucket = "bucket-name"
|
262
|
+
client = Aliyun::Oss::Client.new(access_key, secret_key, host: host, bucket: bucket)
|
263
|
+
|
264
|
+
# Delete one object
|
265
|
+
begin
|
266
|
+
client.bucket_objects.delete('image.png')
|
267
|
+
rescue Aliyun::Oss::RequestError => e
|
268
|
+
puts "Delete Object fail", e.code, e.message, e.request_id
|
269
|
+
end
|
270
|
+
|
271
|
+
|
272
|
+
# Delete many objects at once
|
273
|
+
# the second Paramter used to control the response information. Quiet or Verbose
|
274
|
+
begin
|
275
|
+
client.bucket_objects.delete_multiple(['image1.png', 'image2.png'], true)
|
276
|
+
rescue Aliyun::Oss::RequestError => e
|
277
|
+
puts "Delete Objects fail", e.code, e.message, e.request_id
|
278
|
+
end
|
279
|
+
|
280
|
+
|
281
|
+
### Copy Object
|
282
|
+
|
283
|
+
|
284
|
+
With BucketObjects#copy, we can copy objects from some bucket to others.
|
285
|
+
|
286
|
+
require 'aliyun/oss'
|
287
|
+
|
288
|
+
access_key, secret_key = "your id", "your secret"
|
289
|
+
host = "oss-cn-hangzhou.aliyuncs.com"
|
290
|
+
bucket = "bucket-name"
|
291
|
+
client = Aliyun::Oss::Client.new(access_key, secret_key, host: host, bucket: bucket)
|
292
|
+
|
293
|
+
begin
|
294
|
+
client.bucket_objects.copy('new_image.png', 'origin-bucket-name', 'origin.png')
|
295
|
+
rescue Aliyun::Oss::RequestError => e
|
296
|
+
puts "Copy Objects fail", e.code, e.message, e.request_id
|
297
|
+
end
|
298
|
+
|
299
|
+
Note: the origin bucket and target bucket must locate at same region.
|
300
|
+
|
301
|
+
Now, it allow to modify User meta information.
|
302
|
+
|
303
|
+
|
304
|
+
### Modify Object Meta
|
305
|
+
|
306
|
+
With Copy object, specify the source object and target object to the same one, we can implement modify user meta information.
|
307
|
+
|
308
|
+
require 'aliyun/oss'
|
309
|
+
|
310
|
+
access_key, secret_key = "your id", "your secret"
|
311
|
+
host = "oss-cn-hangzhou.aliyuncs.com"
|
312
|
+
bucket = "bucket-name"
|
313
|
+
client = Aliyun::Oss::Client.new(access_key, secret_key, host: host, bucket: bucket)
|
314
|
+
|
315
|
+
begin
|
316
|
+
headers = { "Content-Type" => "image/japeg" }
|
317
|
+
client.bucket_objects.copy('image.png', 'bucket-name', 'image.png', headers)
|
318
|
+
rescue Aliyun::Oss::RequestError => e
|
319
|
+
puts "Copy Objects fail", e.code, e.message, e.request_id
|
320
|
+
end
|
321
|
+
|
322
|
+
|
323
|
+
|
324
|
+
That's it, Here we visit [Multipart Upload](./multipart.md)
|