oss 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +19 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +13 -0
- data/README.md +113 -0
- data/Rakefile +9 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/lib/oss.rb +43 -0
- data/lib/oss/api.rb +342 -0
- data/lib/oss/bucket.rb +473 -0
- data/lib/oss/client.rb +206 -0
- data/lib/oss/cors.rb +106 -0
- data/lib/oss/error.rb +20 -0
- data/lib/oss/multipart.rb +225 -0
- data/lib/oss/object.rb +318 -0
- data/lib/oss/service.rb +63 -0
- data/lib/oss/util.rb +59 -0
- data/lib/oss/version.rb +3 -0
- data/oss.gemspec +26 -0
- metadata +119 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: f3b2ed6f0211cebe848128e657d3d2de6c9de0ff
|
4
|
+
data.tar.gz: bd5887255606c389a5831a4b7bf1b3c24b8a07fa
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: af7a50af242a892367eea47d2781171153543b56dae24dad9718fe7fdf6f1b311e66df5d1c002e74884b2f31b20a57c0f6bf29b72a3079301bc540e683c05316
|
7
|
+
data.tar.gz: 1da0ae248563da50cb0c3a3d8b491187f67cbfdb5b11f0aefe51c04e2ad0f811daa4e08b10fa8cd91d06268b43fb65249b77a91d1935ecfe30368154d32c2584
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
Copyright 1999-2015 RaymondChou
|
2
|
+
|
3
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
4
|
+
you may not use this file except in compliance with the License.
|
5
|
+
You may obtain a copy of the License at
|
6
|
+
|
7
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
8
|
+
|
9
|
+
Unless required by applicable law or agreed to in writing, software
|
10
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
11
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12
|
+
See the License for the specific language governing permissions and
|
13
|
+
limitations under the License.
|
data/README.md
ADDED
@@ -0,0 +1,113 @@
|
|
1
|
+
# OSS Ruby SDK
|
2
|
+
|
3
|
+
Aliyun Open Storage Service Ruby SDK.
|
4
|
+
|
5
|
+
Being Aliyun-launched cloud storage service for users, the Open Storage Service (OSS for short) features mass capacity, security, high cost-effectiveness and reliability. It enables users to upload or download data to or from any device on the Internet at any time and place, and manage data on the WEB page by using the simple REST interface.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'oss'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install oss
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
### Create Account
|
26
|
+
|
27
|
+
Go to OSS website, create a new account for new user.
|
28
|
+
|
29
|
+
After account created, you can create the OSS instance and get the `accessKeyId` and `accessKeySecret`.
|
30
|
+
|
31
|
+
### Quick Start
|
32
|
+
|
33
|
+
#### 1. Initialize OSS API
|
34
|
+
|
35
|
+
```ruby
|
36
|
+
# require the gem if you not set in Gemfile
|
37
|
+
require 'oss'
|
38
|
+
|
39
|
+
endpoint = 'oss-cn-hangzhou.aliyuncs.com'
|
40
|
+
@api = Oss::Api.new(endpoint, access_key_id, access_key_secret)
|
41
|
+
```
|
42
|
+
|
43
|
+
Rails Application Config:
|
44
|
+
|
45
|
+
create `oss.yml` file in your rails application `config` path:
|
46
|
+
|
47
|
+
```yml
|
48
|
+
default: &default
|
49
|
+
access_key_id: ""
|
50
|
+
access_key_secret: ""
|
51
|
+
endpoint: ""
|
52
|
+
bucket: ""
|
53
|
+
|
54
|
+
production:
|
55
|
+
<<: *default
|
56
|
+
|
57
|
+
staging:
|
58
|
+
<<: *default
|
59
|
+
|
60
|
+
development:
|
61
|
+
<<: *default
|
62
|
+
|
63
|
+
test:
|
64
|
+
<<: *default
|
65
|
+
```
|
66
|
+
|
67
|
+
#### 2. Create New Bucket
|
68
|
+
|
69
|
+
```ruby
|
70
|
+
# set bucket name
|
71
|
+
bucket_name = 'bucket_name'
|
72
|
+
# set bucket acl
|
73
|
+
acl = 'public-read'
|
74
|
+
|
75
|
+
puts 'create bucket success' if @api.put_bucket(bucket_name, acl)
|
76
|
+
```
|
77
|
+
you can find more api options in API Doc `OSS::Bucket`
|
78
|
+
|
79
|
+
#### 3. Upload Object
|
80
|
+
|
81
|
+
```ruby
|
82
|
+
file = File.read('/path/to/your/file')
|
83
|
+
puts 'create object success' if @api.put_object('bucket_name', 'object_name', file)
|
84
|
+
```
|
85
|
+
|
86
|
+
you can find more api options in API Doc `OSS::Object`
|
87
|
+
|
88
|
+
#### 4. List All Objects
|
89
|
+
|
90
|
+
```ruby
|
91
|
+
bucket = @api.get_bucket('bucket_name')
|
92
|
+
p bucket.contents
|
93
|
+
```
|
94
|
+
you can find more api options in API Doc `OSS::Bucket`
|
95
|
+
|
96
|
+
#### 5. Get(download) A Object
|
97
|
+
|
98
|
+
```ruby
|
99
|
+
object = @api.get_object('bucket_name', 'object_name')
|
100
|
+
# show all file headers
|
101
|
+
p object.headers
|
102
|
+
|
103
|
+
# save file
|
104
|
+
file = File.new('/path/to/save', 'w')
|
105
|
+
file.puts object.body
|
106
|
+
file.close
|
107
|
+
```
|
108
|
+
you can find more api options in API Doc `OSS::Object`
|
109
|
+
|
110
|
+
## License
|
111
|
+
|
112
|
+
The gem is available as open source under the terms of the [Apache 2.0 License](http://opensource.org/licenses/Apache-2.0).
|
113
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "oss"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
data/lib/oss.rb
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
require 'oss/version'
|
2
|
+
require 'oss/api'
|
3
|
+
require 'oss/client'
|
4
|
+
require 'oss/service'
|
5
|
+
require 'oss/bucket'
|
6
|
+
require 'oss/object'
|
7
|
+
require 'oss/multipart'
|
8
|
+
require 'oss/cors'
|
9
|
+
|
10
|
+
module Oss
|
11
|
+
|
12
|
+
attr_reader :config
|
13
|
+
|
14
|
+
# load configs for Rails app
|
15
|
+
def self.config
|
16
|
+
@config ||= begin
|
17
|
+
if defined? Rails
|
18
|
+
config_file = Rails.root.join('config/oss.yml')
|
19
|
+
config = YAML.load(ERB.new(File.new(config_file).read).result)[Rails.env] if File.exist?(config_file)
|
20
|
+
end
|
21
|
+
|
22
|
+
config ||= config_from_environment
|
23
|
+
config.symbolize_keys!
|
24
|
+
config[:endpoint] ||= 'oss-cn-hangzhou.aliyuncs.com'
|
25
|
+
OpenStruct.new(config)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def self.api
|
30
|
+
@api ||= Api.new(config.endpoint, config.access_key_id, config.access_key_secret, config.bucket)
|
31
|
+
end
|
32
|
+
|
33
|
+
# load configs from environment
|
34
|
+
def self.config_from_environment
|
35
|
+
{
|
36
|
+
access_key_id: ENV['OSS_ID'],
|
37
|
+
access_key_secret: ENV['OSS_SECRET'],
|
38
|
+
endpoint: ENV['ENDPOINT'],
|
39
|
+
bucket: ENV['BUCKET'],
|
40
|
+
}
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
data/lib/oss/api.rb
ADDED
@@ -0,0 +1,342 @@
|
|
1
|
+
require 'oss/service'
|
2
|
+
require 'oss/bucket'
|
3
|
+
require 'oss/object'
|
4
|
+
require 'oss/multipart'
|
5
|
+
require 'oss/cors'
|
6
|
+
|
7
|
+
module Oss
|
8
|
+
|
9
|
+
class Api
|
10
|
+
|
11
|
+
attr_accessor :client, :bucket
|
12
|
+
|
13
|
+
def initialize(endpoint, access_key_id, access_key_secret, bucket = nil)
|
14
|
+
@bucket = bucket
|
15
|
+
@client = Client.new(endpoint, access_key_id, access_key_secret)
|
16
|
+
end
|
17
|
+
|
18
|
+
|
19
|
+
#######################
|
20
|
+
# Service
|
21
|
+
#######################
|
22
|
+
|
23
|
+
# params:
|
24
|
+
# - options:
|
25
|
+
# - prefix
|
26
|
+
# - marker
|
27
|
+
# - max_keys
|
28
|
+
def get_service(options = {})
|
29
|
+
Service.new(@client).get_service(options)
|
30
|
+
end
|
31
|
+
|
32
|
+
alias_method :list_bucket, :get_service
|
33
|
+
|
34
|
+
|
35
|
+
#######################
|
36
|
+
# Bucket
|
37
|
+
#######################
|
38
|
+
|
39
|
+
# params:
|
40
|
+
# - bucket_name
|
41
|
+
# - acl
|
42
|
+
def put_bucket(bucket_name, acl)
|
43
|
+
Bucket.new(@client).put_bucket(bucket_name, acl)
|
44
|
+
end
|
45
|
+
|
46
|
+
# params:
|
47
|
+
# - bucket_name
|
48
|
+
# - acl
|
49
|
+
def put_bucket_acl(bucket_name, acl)
|
50
|
+
Bucket.new(@client).put_bucket_acl(bucket_name, acl)
|
51
|
+
end
|
52
|
+
|
53
|
+
# params:
|
54
|
+
# - bucket_name
|
55
|
+
# - target_bucket
|
56
|
+
# - enable
|
57
|
+
# - target_prefix
|
58
|
+
def put_bucket_logging(bucket_name, target_bucket, enable = true, target_prefix = nil)
|
59
|
+
Bucket.new(@client).put_bucket_logging(bucket_name, target_bucket, enable, target_prefix)
|
60
|
+
end
|
61
|
+
|
62
|
+
# params:
|
63
|
+
# - bucket_name
|
64
|
+
# - index_doc
|
65
|
+
# - error_doc
|
66
|
+
def put_bucket_website(bucket_name, index_doc, error_doc)
|
67
|
+
Bucket.new(@client).put_bucket_website(bucket_name, index_doc, error_doc)
|
68
|
+
end
|
69
|
+
|
70
|
+
# params:
|
71
|
+
# - bucket_name
|
72
|
+
# - allow_empty
|
73
|
+
# - referer_list
|
74
|
+
def put_bucket_referer(bucket_name, allow_empty, referer_list = [])
|
75
|
+
Bucket.new(@client).put_bucket_referer(bucket_name, allow_empty, referer_list)
|
76
|
+
end
|
77
|
+
|
78
|
+
# params:
|
79
|
+
# - bucket_name
|
80
|
+
# - allow_empty
|
81
|
+
# - referer_list
|
82
|
+
def put_bucket_lifecycle(bucket_name, rules = [])
|
83
|
+
Bucket.new(@client).put_bucket_lifecycle(bucket_name, rules)
|
84
|
+
end
|
85
|
+
|
86
|
+
# params:
|
87
|
+
# - bucket_name
|
88
|
+
# - options:
|
89
|
+
# - delimiter
|
90
|
+
# - prefix
|
91
|
+
# - marker
|
92
|
+
# - max_keys
|
93
|
+
# - encoding_type
|
94
|
+
def get_bucket(bucket_name, options = {})
|
95
|
+
Bucket.new(@client).get_bucket(bucket_name, options)
|
96
|
+
end
|
97
|
+
|
98
|
+
# params:
|
99
|
+
# - bucket_name
|
100
|
+
def get_bucket_acl(bucket_name)
|
101
|
+
Bucket.new(@client).get_bucket_acl(bucket_name)
|
102
|
+
end
|
103
|
+
|
104
|
+
# params:
|
105
|
+
# - bucket_name
|
106
|
+
def get_bucket_location(bucket_name)
|
107
|
+
Bucket.new(@client).get_bucket_location(bucket_name)
|
108
|
+
end
|
109
|
+
|
110
|
+
# params:
|
111
|
+
# - bucket_name
|
112
|
+
def get_bucket_logging(bucket_name)
|
113
|
+
Bucket.new(@client).get_bucket_logging(bucket_name)
|
114
|
+
end
|
115
|
+
|
116
|
+
# params:
|
117
|
+
# - bucket_name
|
118
|
+
def get_bucket_website(bucket_name)
|
119
|
+
Bucket.new(@client).get_bucket_website(bucket_name)
|
120
|
+
end
|
121
|
+
|
122
|
+
# params:
|
123
|
+
# - bucket_name
|
124
|
+
def get_bucket_referer(bucket_name)
|
125
|
+
Bucket.new(@client).get_bucket_referer(bucket_name)
|
126
|
+
end
|
127
|
+
|
128
|
+
# params:
|
129
|
+
# - bucket_name
|
130
|
+
def get_bucket_lifecycle(bucket_name)
|
131
|
+
Bucket.new(@client).get_bucket_lifecycle(bucket_name)
|
132
|
+
end
|
133
|
+
|
134
|
+
# params:
|
135
|
+
# - bucket_name
|
136
|
+
def delete_bucket(bucket_name)
|
137
|
+
Bucket.new(@client).delete_bucket(bucket_name)
|
138
|
+
end
|
139
|
+
|
140
|
+
# params:
|
141
|
+
# - bucket_name
|
142
|
+
def delete_bucket_logging(bucket_name)
|
143
|
+
Bucket.new(@client).delete_bucket_logging(bucket_name)
|
144
|
+
end
|
145
|
+
|
146
|
+
# params:
|
147
|
+
# - bucket_name
|
148
|
+
def delete_bucket_website(bucket_name)
|
149
|
+
Bucket.new(@client).delete_bucket_website(bucket_name)
|
150
|
+
end
|
151
|
+
|
152
|
+
# params:
|
153
|
+
# - bucket_name
|
154
|
+
def delete_bucket_lifecycle(bucket_name)
|
155
|
+
Bucket.new(@client).delete_bucket_lifecycle(bucket_name)
|
156
|
+
end
|
157
|
+
|
158
|
+
|
159
|
+
#######################
|
160
|
+
# Object
|
161
|
+
#######################
|
162
|
+
|
163
|
+
# params:
|
164
|
+
# - bucket_name
|
165
|
+
# - object_name
|
166
|
+
# - file
|
167
|
+
# - options
|
168
|
+
def put_object(bucket_name, object_name, file, options = {})
|
169
|
+
Object.new(@client).put_object(bucket_name, object_name, file, options)
|
170
|
+
end
|
171
|
+
|
172
|
+
# params:
|
173
|
+
# - bucket_name
|
174
|
+
# - object_name
|
175
|
+
# - file
|
176
|
+
# - options
|
177
|
+
def copy_object(bucket_name, object, old_bucket, old_object, options = {})
|
178
|
+
Object.new(@client).copy_object(bucket_name, object, old_bucket, old_object, options)
|
179
|
+
end
|
180
|
+
|
181
|
+
# params:
|
182
|
+
# - bucket_name
|
183
|
+
# - object
|
184
|
+
# - options
|
185
|
+
def get_object(bucket_name, object, options = {})
|
186
|
+
Object.new(@client).get_object(bucket_name, object, options)
|
187
|
+
end
|
188
|
+
|
189
|
+
# params:
|
190
|
+
# - bucket_name
|
191
|
+
# - object_name
|
192
|
+
# - file
|
193
|
+
# - options
|
194
|
+
def append_object(bucket_name, object_name, file, position, options = {})
|
195
|
+
Object.new(@client).append_object(bucket_name, object_name, file, position, options)
|
196
|
+
end
|
197
|
+
|
198
|
+
# params:
|
199
|
+
# - bucket_name
|
200
|
+
# - object_name
|
201
|
+
def delete_object(bucket_name, object_name)
|
202
|
+
Object.new(@client).delete_object(bucket_name, object_name)
|
203
|
+
end
|
204
|
+
|
205
|
+
# params:
|
206
|
+
# - bucket_name
|
207
|
+
# - objects
|
208
|
+
def delete_multiple_objects(bucket_name, objects)
|
209
|
+
Object.new(@client).delete_multiple_objects(bucket_name, objects)
|
210
|
+
end
|
211
|
+
|
212
|
+
# params:
|
213
|
+
# - bucket_name
|
214
|
+
# - object_name
|
215
|
+
# - options
|
216
|
+
def head_object(bucket_name, object_name)
|
217
|
+
Object.new(@client).head_object(bucket_name, object_name)
|
218
|
+
end
|
219
|
+
|
220
|
+
# params:
|
221
|
+
# - bucket_name
|
222
|
+
# - object_name
|
223
|
+
# - acl
|
224
|
+
def put_object_acl(bucket_name, object_name, acl)
|
225
|
+
Object.new(@client).put_object_acl(bucket_name, object_name, acl)
|
226
|
+
end
|
227
|
+
|
228
|
+
# params:
|
229
|
+
# - bucket_name
|
230
|
+
# - object_name
|
231
|
+
# - acl
|
232
|
+
def post_object(bucket_name, key, options = {})
|
233
|
+
Object.new(@client).post_object(bucket_name, key, options)
|
234
|
+
end
|
235
|
+
|
236
|
+
|
237
|
+
#######################
|
238
|
+
# Multipart
|
239
|
+
#######################
|
240
|
+
|
241
|
+
# params:
|
242
|
+
# - bucket_name
|
243
|
+
# - object_name
|
244
|
+
# - options
|
245
|
+
def initiate_multipart_upload(bucket_name, object_name, options = {})
|
246
|
+
Multipart.new(@client).initiate_multipart_upload(bucket_name, object_name, options)
|
247
|
+
end
|
248
|
+
|
249
|
+
# params:
|
250
|
+
# - bucket_name
|
251
|
+
# - object_name
|
252
|
+
# - upload_id
|
253
|
+
# - file
|
254
|
+
# - part_number
|
255
|
+
# - options
|
256
|
+
def upload_part(bucket_name, object_name, upload_id, file, part_number = 1, options = {})
|
257
|
+
Multipart.new(@client).upload_part(bucket_name, object_name, upload_id, file, part_number, options)
|
258
|
+
end
|
259
|
+
|
260
|
+
# params:
|
261
|
+
# - bucket_name
|
262
|
+
# - object_name
|
263
|
+
# - upload_id
|
264
|
+
# - old_bucket
|
265
|
+
# - old_object
|
266
|
+
# - part_number
|
267
|
+
# - options
|
268
|
+
def upload_part_copy(bucket_name, object_name, upload_id, old_bucket, old_object, part_number = 1, options = {})
|
269
|
+
Multipart.new(@client).upload_part_copy(bucket_name, object_name, upload_id, old_bucket, old_object, part_number, options)
|
270
|
+
end
|
271
|
+
|
272
|
+
# params:
|
273
|
+
# - bucket_name
|
274
|
+
# - object_name
|
275
|
+
# - upload_id
|
276
|
+
# - parts
|
277
|
+
def complete_multipart_upload(bucket_name, object_name, upload_id, parts)
|
278
|
+
Multipart.new(@client).complete_multipart_upload(bucket_name, object_name, upload_id, parts)
|
279
|
+
end
|
280
|
+
|
281
|
+
# params:
|
282
|
+
# - bucket_name
|
283
|
+
# - object_name
|
284
|
+
# - upload_id
|
285
|
+
def abort_multipart_upload(bucket_name, object_name, upload_id)
|
286
|
+
Multipart.new(@client).abort_multipart_upload(bucket_name, object_name, upload_id)
|
287
|
+
end
|
288
|
+
|
289
|
+
# params:
|
290
|
+
# - bucket_name
|
291
|
+
# - options
|
292
|
+
def list_multipart_upload(bucket_name, options = {})
|
293
|
+
Multipart.new(@client).list_multipart_upload(bucket_name, options)
|
294
|
+
end
|
295
|
+
|
296
|
+
# params:
|
297
|
+
# - bucket_name
|
298
|
+
# - object_name
|
299
|
+
# - options
|
300
|
+
def list_parts(bucket_name, object_name, options = {})
|
301
|
+
Multipart.new(@client).list_parts(bucket_name, object_name, options)
|
302
|
+
end
|
303
|
+
|
304
|
+
|
305
|
+
#######################
|
306
|
+
# Cors
|
307
|
+
#######################
|
308
|
+
|
309
|
+
# params:
|
310
|
+
# - bucket_name
|
311
|
+
# - rules
|
312
|
+
def put_bucket_cors(bucket_name, rules)
|
313
|
+
Cors.new(@client).put_bucket_cors(bucket_name, rules)
|
314
|
+
end
|
315
|
+
|
316
|
+
# params:
|
317
|
+
# - bucket_name
|
318
|
+
# - rules
|
319
|
+
def get_bucket_cors(bucket_name)
|
320
|
+
Cors.new(@client).get_bucket_cors(bucket_name)
|
321
|
+
end
|
322
|
+
|
323
|
+
# params:
|
324
|
+
# - bucket_name
|
325
|
+
# - rules
|
326
|
+
def delete_bucket_cors(bucket_name)
|
327
|
+
Cors.new(@client).delete_bucket_cors(bucket_name)
|
328
|
+
end
|
329
|
+
|
330
|
+
# params:
|
331
|
+
# - bucket_name
|
332
|
+
# - object_name
|
333
|
+
# - origin
|
334
|
+
# - request_method
|
335
|
+
# - request_headers
|
336
|
+
def option_object(bucket_name, object_name, origin, request_method, request_headers)
|
337
|
+
Cors.new(@client).option_object(bucket_name, object_name, origin, request_method, request_headers)
|
338
|
+
end
|
339
|
+
|
340
|
+
end
|
341
|
+
|
342
|
+
end
|