aliyunoss 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 +16 -0
- data/Gemfile +6 -0
- data/LICENSE.txt +22 -0
- data/README.md +92 -0
- data/Rakefile +8 -0
- data/aliyunoss.gemspec +24 -0
- data/lib/aliyunoss/api.rb +222 -0
- data/lib/aliyunoss/bucket.rb +131 -0
- data/lib/aliyunoss/config.rb +47 -0
- data/lib/aliyunoss/extension.rb +34 -0
- data/lib/aliyunoss/helper.rb +27 -0
- data/lib/aliyunoss/http_response_extension.rb +66 -0
- data/lib/aliyunoss/oss_request.rb +143 -0
- data/lib/aliyunoss/version.rb +5 -0
- data/lib/aliyunoss.rb +12 -0
- data/spec/aliyunoss/aliyun-config.yml.sample +2 -0
- data/spec/aliyunoss/api_spec.rb +123 -0
- data/spec/aliyunoss/bucket_spec.rb +109 -0
- data/spec/aliyunoss/config_spec.rb +47 -0
- data/spec/aliyunoss/incorrect-config.yml +4 -0
- data/spec/aliyunoss/multipart_api_spec.rb +103 -0
- data/spec/aliyunoss/oss_request_spec.rb +51 -0
- data/spec/aliyunoss/test-1.png +0 -0
- data/spec/aliyunoss/test-2.png +0 -0
- data/spec/aliyunoss/test-3.png +0 -0
- metadata +121 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: f6d5f91f7798d9c53064203c5d72a04c316f4e4c
|
4
|
+
data.tar.gz: a984576bb7a203542fe326c7620482ee8f245346
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 711cbd85c09bcef395be43cc99a65bdedf4435e44b1ac350e9a75c17835258d37f82b7a7e5b30204011779e62e27840f54e34f61dafe2a54873a6261f7e0d239
|
7
|
+
data.tar.gz: a384ed6fb2f98eaa46985b491df0a7369197e6ee26e7e037dc7bac821fa14b173088707da9b00dcbdb73716bfc69b6a9fd10aa931351d37cb8d9ea8faa20b17b
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2015 yijiecc
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,92 @@
|
|
1
|
+
# Aliyun::Oss
|
2
|
+
|
3
|
+
Ruby gem for [Aliyun Open Storage Service (OSS)][1]. This gem implemented API from [references of Aliyun OSS-API][2].
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'aliyunoss'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install aliyunoss
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
This gem provides high level interfaces which are built around class Bucket and low level interfaces which are wrappers for [OSS API][2].
|
24
|
+
|
25
|
+
### Load Access Key ID and Access Key Secret
|
26
|
+
|
27
|
+
Specify you access key and access secret before calling methods provided in this gem:
|
28
|
+
|
29
|
+
Aliyun::Oss::configure(:access_key_id => 'access key id from aliyun', :access_key_secret => 'access secret from aliyun')
|
30
|
+
|
31
|
+
### Low Level Interfaces
|
32
|
+
|
33
|
+
Low level interfaces are simple wrappers for [OSS API][2], you send requests with specified parameters and receive responses with a Net::HTTPResponse object. All parameters listed in [OSS API][2] are needed for every request, and context info between requests is maintained by yourself. In order to tell whether a a request is success, check code and body of returned Net::HTTPResponse object.
|
34
|
+
|
35
|
+
All [OSS API][2] listed are implemented except [Post Object][3] and [CORS APIs][4].
|
36
|
+
|
37
|
+
List all the buckets:
|
38
|
+
|
39
|
+
Aliyun::Oss::API.list_bucket
|
40
|
+
|
41
|
+
Upload data to specified bucket:
|
42
|
+
|
43
|
+
bucket = Aliyun::Oss::Bucket.new(:name => 'bucket-name', :location => 'oss-cn-beijing')
|
44
|
+
path = '/test.dat'
|
45
|
+
data = Random.new.bytes(1024 * rand(100..300))
|
46
|
+
Aliyun::Oss::API.put_object(bucket, path, data)
|
47
|
+
|
48
|
+
Download data from specified bucket:
|
49
|
+
|
50
|
+
bucket = Bucket.new(:name => 'bucket-name', :location => 'oss-cn-beijing')
|
51
|
+
path = '/test.dat'
|
52
|
+
Aliyun::Oss::API.get_object(bucket, path)
|
53
|
+
|
54
|
+
For more usage, see API documentation generated by rdoc.
|
55
|
+
|
56
|
+
### High Level Interfaces
|
57
|
+
|
58
|
+
High level interface are built around class Bucket. You create a new bucket, and upload, download or delete files in it. When needed, responses are parsed to array, hash or object etc, thus more meaningful. Exceptions will be raised when request failed.
|
59
|
+
|
60
|
+
# Create a bucket in OSS server
|
61
|
+
bucket = Aliyun::Oss::Bucket.create('aliyun-oss-gem-api-test','oss-cn-hangzhou')
|
62
|
+
|
63
|
+
# Upload a file
|
64
|
+
filename = '/some/file'
|
65
|
+
bucket.upload( IO.read(filename) )
|
66
|
+
|
67
|
+
# List all files in this bucket, returns a Hash array containing files info
|
68
|
+
files = bucket.list_files
|
69
|
+
|
70
|
+
# Download a file, returns raw data
|
71
|
+
data = bucket.download( '/some/path' )
|
72
|
+
|
73
|
+
For more usage, see API documentation generated by rdoc.
|
74
|
+
|
75
|
+
## Testing
|
76
|
+
|
77
|
+
This gem use rspec for testing. Most of testing needs a valid access key id and access key secret, you should get these info from Aliyun.
|
78
|
+
|
79
|
+
Create a file named aliyun-config.yml in path rspec/aliyun/oss, fill in valid access key id and access key secret and cotinue to test.
|
80
|
+
|
81
|
+
## Contributing
|
82
|
+
|
83
|
+
1. Fork it ( https://github.com/yijiecc/aliyunoss/fork )
|
84
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
85
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
86
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
87
|
+
5. Create a new Pull Request
|
88
|
+
|
89
|
+
[1]: http://www.aliyun.com/product/oss
|
90
|
+
[2]: http://docs.aliyun.com/?spm=5176.383663.9.2.F8rxxr#/oss/api-reference/abstract
|
91
|
+
[3]: http://docs.aliyun.com/?spm=5176.383663.9.2.F8rxxr#/oss/api-reference/object&PostObject
|
92
|
+
[4]: http://docs.aliyun.com/?spm=5176.383663.9.2.F8rxxr#/oss/api-reference/cors&abstract
|
data/Rakefile
ADDED
data/aliyunoss.gemspec
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'aliyunoss/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "aliyunoss"
|
8
|
+
spec.version = Aliyun::Oss::VERSION
|
9
|
+
spec.authors = ["yijiecc"]
|
10
|
+
spec.email = ["yijiecc@hotmail.com"]
|
11
|
+
spec.summary = %q{A gem for accessing Aliyun Open Storage Service.}
|
12
|
+
spec.description = %q{Access Aliyun-OSS service easily in ruby.}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.7"
|
22
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
23
|
+
spec.add_development_dependency "rspec", "~> 3.1"
|
24
|
+
end
|
@@ -0,0 +1,222 @@
|
|
1
|
+
module Aliyun
|
2
|
+
module Oss
|
3
|
+
|
4
|
+
module API
|
5
|
+
extend self
|
6
|
+
|
7
|
+
# Reference
|
8
|
+
# http://docs-aliyun-com-cn-b.oss-cn-hangzhou.aliyuncs.com/oss/pdf/oss_api-reference.pdf
|
9
|
+
|
10
|
+
# List all buckets
|
11
|
+
def list_bucket(params = {})
|
12
|
+
Aliyun::Oss::OssRequest.new(nil, '/', params).get
|
13
|
+
end
|
14
|
+
alias :get_service :list_bucket
|
15
|
+
|
16
|
+
# Create a new bucket
|
17
|
+
def put_bucket(name, location = 'oss-cn-hangzhou')
|
18
|
+
bucket = Bucket.new(name: name, location: location)
|
19
|
+
request = Aliyun::Oss::OssRequest.new(bucket, '/')
|
20
|
+
request.body = <<HERE
|
21
|
+
<?xml version="1.0" encoding="UTF-8"?>
|
22
|
+
<CreateBucketConfiguration>
|
23
|
+
<LocationConstraint>#{location}</LocationConstraint>
|
24
|
+
</CreateBucketConfiguration>
|
25
|
+
HERE
|
26
|
+
request.put
|
27
|
+
end
|
28
|
+
alias :create_bucket :put_bucket
|
29
|
+
|
30
|
+
# Delete a bucket
|
31
|
+
def delete_bucket(bucket)
|
32
|
+
Aliyun::Oss::OssRequest.new(bucket, '/').delete
|
33
|
+
end
|
34
|
+
|
35
|
+
# Delete bucket logging
|
36
|
+
def delete_logging(bucket)
|
37
|
+
Aliyun::Oss::OssRequest.new(bucket, '/', 'logging'=>nil).delete
|
38
|
+
end
|
39
|
+
|
40
|
+
# Delete bucket website
|
41
|
+
def delete_website(bucket)
|
42
|
+
Aliyun::Oss::OssRequest.new(bucket, '/', 'website'=>nil).delete
|
43
|
+
end
|
44
|
+
alias :disable_website :delete_website
|
45
|
+
|
46
|
+
# List objects in bucket
|
47
|
+
def list_object(bucket, queries = {})
|
48
|
+
Aliyun::Oss::OssRequest.new(bucket, '/', queries).get
|
49
|
+
end
|
50
|
+
|
51
|
+
# Get bucket acl
|
52
|
+
def get_bucket_acl(bucket)
|
53
|
+
Aliyun::Oss::OssRequest.new(bucket, '/', 'acl'=>nil).get
|
54
|
+
end
|
55
|
+
|
56
|
+
# Get bucket location
|
57
|
+
def get_bucket_location(bucket)
|
58
|
+
Aliyun::Oss::OssRequest.new(bucket, '/', 'location'=>nil).get
|
59
|
+
end
|
60
|
+
|
61
|
+
# Query bucket logging status
|
62
|
+
def get_bucket_logging(bucket)
|
63
|
+
Aliyun::Oss::OssRequest.new(bucket, '/', 'logging'=>nil).get
|
64
|
+
end
|
65
|
+
|
66
|
+
# Query bucket website status
|
67
|
+
def get_bucket_website(bucket)
|
68
|
+
Aliyun::Oss::OssRequest.new(bucket, '/', 'website'=>nil).get
|
69
|
+
end
|
70
|
+
|
71
|
+
# Set bucket acl permission
|
72
|
+
def put_bucket_acl(bucket, permission)
|
73
|
+
Aliyun::Oss::OssRequest.new(bucket, '/', {}, 'x-oss-acl'=> permission).put
|
74
|
+
end
|
75
|
+
alias :set_bucket_acl :put_bucket_acl
|
76
|
+
|
77
|
+
# Enable or disable bucket logging
|
78
|
+
def enable_bucket_logging(bucket, bucket_name_for_logging, log_prefix)
|
79
|
+
request = Aliyun::Oss::OssRequest.new(bucket, '/', 'logging'=>nil)
|
80
|
+
request.body = <<HERE
|
81
|
+
<?xml version="1.0" encoding="UTF-8"?>
|
82
|
+
<BucketLoggingStatus>
|
83
|
+
<LoggingEnabled>
|
84
|
+
<TargetBucket>#{bucket_name_for_logging}</TargetBucket>
|
85
|
+
<TargetPrefix>#{log_prefix}</TargetPrefix>
|
86
|
+
</LoggingEnabled>
|
87
|
+
</BucketLoggingStatus>
|
88
|
+
HERE
|
89
|
+
request['Content-Type'] = 'application/xml'
|
90
|
+
request.put
|
91
|
+
end
|
92
|
+
|
93
|
+
def disable_bucket_logging(bucket)
|
94
|
+
request = Aliyun::Oss::OssRequest.new(bucket, '/', 'logging'=>nil)
|
95
|
+
request.body = <<HERE
|
96
|
+
<?xml version="1.0" encoding="UTF-8"?>
|
97
|
+
<BucketLoggingStatus>
|
98
|
+
</BucketLoggingStatus>
|
99
|
+
HERE
|
100
|
+
request['Content-Type'] = 'application/xml'
|
101
|
+
request.put
|
102
|
+
end
|
103
|
+
|
104
|
+
# Set bucket website access
|
105
|
+
def put_bucket_website(bucket, index_page, error_page)
|
106
|
+
request = Aliyun::Oss::OssRequest.new(bucket, '/', 'website'=>nil)
|
107
|
+
request.body = <<HERE
|
108
|
+
<?xml version="1.0" encoding="UTF-8"?>
|
109
|
+
<WebsiteConfiguration>
|
110
|
+
<IndexDocument>
|
111
|
+
<Suffix>#{index_page}</Suffix>
|
112
|
+
</IndexDocument>
|
113
|
+
<ErrorDocument>
|
114
|
+
<Key>#{error_page}</Key>
|
115
|
+
</ErrorDocument>
|
116
|
+
</WebsiteConfiguration>
|
117
|
+
HERE
|
118
|
+
request['Content-Type'] = 'application/xml'
|
119
|
+
request.put
|
120
|
+
end
|
121
|
+
alias :set_bucket_website :put_bucket_website
|
122
|
+
|
123
|
+
# Copy Object
|
124
|
+
def copy_object(source_bucket, source_path, target_bucket, target_path, headers = {})
|
125
|
+
headers = headers.merge({'x-oss-copy-source'=> "/" + source_bucket.name + source_path})
|
126
|
+
Aliyun::Oss::OssRequest.new(target_bucket, target_path, {}, headers).put
|
127
|
+
end
|
128
|
+
|
129
|
+
# Delete Object
|
130
|
+
def delete_object(bucket, path)
|
131
|
+
Aliyun::Oss::OssRequest.new(bucket, path).delete
|
132
|
+
end
|
133
|
+
|
134
|
+
# Delete Multiple Object
|
135
|
+
def delete_multiple_objects(bucket, objects, quiet_mode = false)
|
136
|
+
xml = '<?xml version="1.0" encoding="UTF-8"?>'
|
137
|
+
xml << '<Delete>'
|
138
|
+
xml << "<Quiet>#{quiet_mode}</Quiet>"
|
139
|
+
objects.each {|o| xml << "<Object><Key>#{o}</Key></Object>"}
|
140
|
+
xml << '</Delete>'
|
141
|
+
request = Aliyun::Oss::OssRequest.new(bucket, '/', 'delete'=>nil)
|
142
|
+
request.body = xml
|
143
|
+
request.post
|
144
|
+
end
|
145
|
+
|
146
|
+
# Get Object
|
147
|
+
def get_object(bucket, path, headers = {})
|
148
|
+
Aliyun::Oss::OssRequest.new(bucket, path, {}, headers).get
|
149
|
+
end
|
150
|
+
|
151
|
+
# Head Object
|
152
|
+
def head_object(bucket, path, headers = {})
|
153
|
+
Aliyun::Oss::OssRequest.new(bucket, path, {}, headers).head
|
154
|
+
end
|
155
|
+
|
156
|
+
# Put Object
|
157
|
+
def put_object(bucket, path, data, headers = {})
|
158
|
+
request = Aliyun::Oss::OssRequest.new(bucket, path, {}, headers)
|
159
|
+
request.body = data
|
160
|
+
request.put
|
161
|
+
end
|
162
|
+
|
163
|
+
|
164
|
+
|
165
|
+
# Post Object
|
166
|
+
# Not implemented
|
167
|
+
|
168
|
+
# Post Policy
|
169
|
+
# Not implemented
|
170
|
+
|
171
|
+
# Post signature
|
172
|
+
# Not implemented
|
173
|
+
|
174
|
+
# Multipart Initiate
|
175
|
+
def multipart_upload_initiate(bucket, path)
|
176
|
+
Aliyun::Oss::OssRequest.new(bucket, path, 'uploads'=>nil).post
|
177
|
+
end
|
178
|
+
|
179
|
+
def multipart_upload_part(bucket, path, upload_id, data, part_number)
|
180
|
+
request = Aliyun::Oss::OssRequest.new(bucket, path, 'partNumber'=> part_number.to_s, 'uploadId'=> upload_id)
|
181
|
+
request.body = data
|
182
|
+
request.put
|
183
|
+
end
|
184
|
+
|
185
|
+
def multipart_upload_from_copy(upload_id, source_bucket, source_path, target_bucket, target_path, part_number, part_size, range = nil)
|
186
|
+
request = Aliyun::Oss::OssRequest.new(target_bucket, target_path, 'partNumber'=> part_number.to_s, 'uploadId'=> upload_id)
|
187
|
+
request['Content-Length'] = part_size
|
188
|
+
request['x-oss-copy-source'] = "/" + source_bucket.name + source_path
|
189
|
+
request['x-oss-copy-source-range'] = range if range
|
190
|
+
request.put
|
191
|
+
end
|
192
|
+
|
193
|
+
def multipart_upload_complete(bucket, path, upload_id, part_list)
|
194
|
+
request = Aliyun::Oss::OssRequest.new(bucket, path, 'uploadId'=> upload_id)
|
195
|
+
xml = '<?xml version="1.0" encoding="UTF-8"?>'
|
196
|
+
xml << '<CompleteMultipartUpload>'
|
197
|
+
part_list.each_pair {|k,v| xml << "<Part><PartNumber>#{k}</PartNumber><ETag>#{v}</ETag></Part>"}
|
198
|
+
xml << '</CompleteMultipartUpload>'
|
199
|
+
request.body = xml
|
200
|
+
request.post
|
201
|
+
end
|
202
|
+
|
203
|
+
def multipart_upload_abort(bucket, path, upload_id)
|
204
|
+
Aliyun::Oss::OssRequest.new(bucket, path, 'uploadId'=> upload_id).delete
|
205
|
+
end
|
206
|
+
|
207
|
+
def multipart_upload_finished_parts(bucket, path, upload_id)
|
208
|
+
Aliyun::Oss::OssRequest.new(bucket, path, 'uploadId'=> upload_id).get
|
209
|
+
end
|
210
|
+
|
211
|
+
def multipart_upload_unfinished_task(bucket)
|
212
|
+
Aliyun::Oss::OssRequest.new(bucket, '/', 'uploads'=>nil).get
|
213
|
+
end
|
214
|
+
|
215
|
+
end
|
216
|
+
|
217
|
+
end
|
218
|
+
end
|
219
|
+
|
220
|
+
|
221
|
+
|
222
|
+
|
@@ -0,0 +1,131 @@
|
|
1
|
+
module Aliyun
|
2
|
+
module Oss
|
3
|
+
|
4
|
+
class Bucket
|
5
|
+
attr_accessor :location, :name, :creation_date
|
6
|
+
|
7
|
+
def initialize(params = {})
|
8
|
+
params.each_pair {|k,v| send("#{k.to_s}=", v) }
|
9
|
+
if String === @creation_date
|
10
|
+
@creation_date =~ /(\d+) ([a-zA-Z]+) (\d+) (\d\d):(\d\d):(\d\d)/
|
11
|
+
@creation_date = Time.gm($3, $2, $1, $4, $5, $6)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
# Class methods
|
16
|
+
def self.all
|
17
|
+
Aliyun::Oss::API.list_bucket.to_buckets
|
18
|
+
end
|
19
|
+
|
20
|
+
def self.create(name, location = 'oss-cn-hangzhou')
|
21
|
+
if Aliyun::Oss::API.put_bucket(name, location).class == Net::HTTPOK
|
22
|
+
Bucket.new(:name => name, :location=> location, :creation_date => Time.now)
|
23
|
+
else
|
24
|
+
nil
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
# Instance Methods -- object download and upload
|
29
|
+
def list_files(options = {})
|
30
|
+
Aliyun::Oss::API.list_object(self, options).to_objects
|
31
|
+
end
|
32
|
+
|
33
|
+
def upload(data, path, options = {})
|
34
|
+
Aliyun::Oss::API.put_object(self, path, data, options).raise_if_not(Net::HTTPOK)
|
35
|
+
end
|
36
|
+
|
37
|
+
def download(path, options = {})
|
38
|
+
response = Aliyun::Oss::API.get_object(self, path, options)
|
39
|
+
response.raise_if_not(Net::HTTPOK)
|
40
|
+
response.body
|
41
|
+
end
|
42
|
+
|
43
|
+
def delete(path)
|
44
|
+
Aliyun::Oss::API.delete_object(self, path).raise_if_not(Net::HTTPNoContent)
|
45
|
+
end
|
46
|
+
|
47
|
+
# Multipart upload and copy
|
48
|
+
def multipart_pending
|
49
|
+
Aliyun::Oss::API.multipart_upload_unfinished_task(self).to_mutlipart_task
|
50
|
+
end
|
51
|
+
|
52
|
+
def multipart_abort(path, upload_id)
|
53
|
+
Aliyun::Oss::API.multipart_upload_abort(self, path, upload_id).raise_if_not(Net::HTTPNoContent)
|
54
|
+
end
|
55
|
+
|
56
|
+
def multipart_upload(data)
|
57
|
+
raise 'You must call multipart_upload_initiate before upload data.' unless @multipart_path
|
58
|
+
response = Aliyun::Oss::API.multipart_upload_part(self, @multipart_path,
|
59
|
+
@multipart_id,
|
60
|
+
data, @multipart_sequence)
|
61
|
+
response.raise_if_not(Net::HTTPOK)
|
62
|
+
@multipart_list[@multipart_sequence] = response['ETag']
|
63
|
+
@multipart_sequence = @multipart_sequence + 1
|
64
|
+
end
|
65
|
+
|
66
|
+
def multipart_copy_from(source_bucket, source_path, size, range = nil)
|
67
|
+
response = Aliyun::Oss::API.multipart_upload_from_copy(@multipart_id,
|
68
|
+
source_bucket, source_path,
|
69
|
+
self, @multipart_path,
|
70
|
+
@multipart_sequence,
|
71
|
+
size, range)
|
72
|
+
response.raise_if_not(Net::HTTPOK)
|
73
|
+
@multipart_list[@multipart_sequence] = response['ETag']
|
74
|
+
@multipart_sequence = @multipart_sequence + 1
|
75
|
+
end
|
76
|
+
|
77
|
+
def multipart_upload_initiate(path)
|
78
|
+
@multipart_path = path
|
79
|
+
@multipart_id = Aliyun::Oss::API.multipart_upload_initiate(self, path).to_multipart_id
|
80
|
+
@multipart_list = {}
|
81
|
+
@multipart_sequence = 1
|
82
|
+
end
|
83
|
+
|
84
|
+
def multipart_upload_complete
|
85
|
+
Aliyun::Oss::API.multipart_upload_complete(self, @multipart_path,
|
86
|
+
@multipart_id,
|
87
|
+
@multipart_list)
|
88
|
+
.raise_if_not(Net::HTTPOK)
|
89
|
+
@multipart_path = nil
|
90
|
+
end
|
91
|
+
|
92
|
+
# Instance Methods -- miscellaneous
|
93
|
+
def delete!
|
94
|
+
Aliyun::Oss::API.delete_bucket(self).raise_if_not(Net::HTTPNoContent)
|
95
|
+
end
|
96
|
+
|
97
|
+
def disable_logging
|
98
|
+
Aliyun::Oss::API.delete_logging(self).raise_if_not(Net::HTTPNoContent)
|
99
|
+
end
|
100
|
+
|
101
|
+
def enable_logging(target_bucket_name, log_prefix)
|
102
|
+
Aliyun::Oss::API.enable_bucket_logging(self, target_bucket_name, log_prefix).raise_if_not(Net::HTTPOK)
|
103
|
+
end
|
104
|
+
|
105
|
+
def logging_status
|
106
|
+
Aliyun::Oss::API.get_bucket_logging(self).to_logging_status
|
107
|
+
end
|
108
|
+
|
109
|
+
def disable_website_access
|
110
|
+
Aliyun::Oss::API.delete_website(self).raise_if_not(Net::HTTPNoContent)
|
111
|
+
end
|
112
|
+
|
113
|
+
def enable_website_access(index_page, error_page)
|
114
|
+
Aliyun::Oss::API.put_bucket_website(self, index_page, error_page).raise_if_not(Net::HTTPOK)
|
115
|
+
end
|
116
|
+
|
117
|
+
def website_access_status
|
118
|
+
Aliyun::Oss::API.get_bucket_website(self).to_website_status
|
119
|
+
end
|
120
|
+
|
121
|
+
def set_acl(permission)
|
122
|
+
Aliyun::Oss::API.put_bucket_acl(self, permission).raise_if_not(Net::HTTPOK)
|
123
|
+
end
|
124
|
+
|
125
|
+
def get_acl
|
126
|
+
Aliyun::Oss::API.get_bucket_acl(self).to_acl_status
|
127
|
+
end
|
128
|
+
end
|
129
|
+
|
130
|
+
end
|
131
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
require 'yaml'
|
2
|
+
require 'logger'
|
3
|
+
|
4
|
+
module Aliyun
|
5
|
+
module Oss
|
6
|
+
|
7
|
+
@config = {
|
8
|
+
:log_level => 'info',
|
9
|
+
:host => 'aliyuncs.com',
|
10
|
+
:access_key_id => nil,
|
11
|
+
:access_key_secret => nil
|
12
|
+
}
|
13
|
+
|
14
|
+
@_logger = nil
|
15
|
+
|
16
|
+
@valid_config_keys = @config.keys
|
17
|
+
|
18
|
+
def self.configure(opts = {})
|
19
|
+
@_logger = nil
|
20
|
+
opts.each {|k,v| @config[k.to_sym] = v if @valid_config_keys.include?(k.to_sym)}
|
21
|
+
end
|
22
|
+
|
23
|
+
def self.configure_with(yaml_file)
|
24
|
+
begin
|
25
|
+
config = YAML::load(IO.read(yaml_file))
|
26
|
+
configure(config)
|
27
|
+
rescue Errno::ENOENT
|
28
|
+
logger.warn("YAML configuration file couldn't be found. Using defaults.")
|
29
|
+
rescue Psych::SyntaxError
|
30
|
+
logger.warn("YAML configuration file contains invalid syntax. Using defaults.")
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def self.config
|
35
|
+
@config
|
36
|
+
end
|
37
|
+
|
38
|
+
def self.logger
|
39
|
+
unless @_logger
|
40
|
+
@_logger ||= Logger.new(STDOUT)
|
41
|
+
@_logger.level = Logger.const_get(@config[:log_level].upcase) rescue Logger::INFO
|
42
|
+
end
|
43
|
+
@_logger
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
class Hash
|
2
|
+
# Convert hash to http query string
|
3
|
+
def to_query_string
|
4
|
+
array = Array.new
|
5
|
+
self.each_pair {|k,v| array << (if v then k+"="+v else k end)}
|
6
|
+
array.join('&')
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
class String
|
11
|
+
# Convert http query string to hash
|
12
|
+
def query_string_to_hash
|
13
|
+
hash = Hash.new
|
14
|
+
self.split('&').each do |q|
|
15
|
+
if q["="]
|
16
|
+
k,v = q.split('=')
|
17
|
+
hash[k] = v
|
18
|
+
else
|
19
|
+
hash[q] = nil
|
20
|
+
end
|
21
|
+
end
|
22
|
+
hash
|
23
|
+
end
|
24
|
+
|
25
|
+
# Convert CamelCase to ruby_case
|
26
|
+
def underscore
|
27
|
+
self.gsub(/::/, '/').
|
28
|
+
gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
|
29
|
+
gsub(/([a-z\d])([A-Z])/,'\1_\2').
|
30
|
+
tr("-", "_").
|
31
|
+
downcase
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module Aliyun
|
2
|
+
module Oss
|
3
|
+
|
4
|
+
module ConfigHelper
|
5
|
+
def logger
|
6
|
+
Aliyun::Oss.logger
|
7
|
+
end
|
8
|
+
|
9
|
+
def config
|
10
|
+
Aliyun::Oss.config
|
11
|
+
end
|
12
|
+
|
13
|
+
def access_key_id
|
14
|
+
Aliyun::Oss.config[:access_key_id]
|
15
|
+
end
|
16
|
+
|
17
|
+
def access_key_secret
|
18
|
+
Aliyun::Oss.config[:access_key_secret]
|
19
|
+
end
|
20
|
+
|
21
|
+
def host
|
22
|
+
Aliyun::Oss.config[:host]
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|