aliyunoss 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f6d5f91f7798d9c53064203c5d72a04c316f4e4c
4
- data.tar.gz: a984576bb7a203542fe326c7620482ee8f245346
3
+ metadata.gz: 716bf19a85202b9dd54f69809b80edd10413d239
4
+ data.tar.gz: e83b18486d76552130a8e897b9e8ea7c01cf90f9
5
5
  SHA512:
6
- metadata.gz: 711cbd85c09bcef395be43cc99a65bdedf4435e44b1ac350e9a75c17835258d37f82b7a7e5b30204011779e62e27840f54e34f61dafe2a54873a6261f7e0d239
7
- data.tar.gz: a384ed6fb2f98eaa46985b491df0a7369197e6ee26e7e037dc7bac821fa14b173088707da9b00dcbdb73716bfc69b6a9fd10aa931351d37cb8d9ea8faa20b17b
6
+ metadata.gz: 8dba8f48ad17fafee1979d6dbf5d4fa60aa004c2fd6e0a67dd4345a6d8cb798017c2e204eab77bd72844f03956799de62e74f229fb81f79ff7e23dd0aae1fd58
7
+ data.tar.gz: 804cd0ae4c9c6db0a8ad3702290be2eeb0d669a1a73df999a98565ecf8ea73fa44bbea870394095cc037d5a2d773c258d17de7677c31a4466133b76287f8ffa3
data/Gemfile CHANGED
@@ -1,6 +1,8 @@
1
- source 'http://rubygems.org'
1
+ source 'https://rubygems.org'
2
2
 
3
3
  # Specify your gem's dependencies in aliyun-oss.gemspec
4
4
  gemspec
5
5
 
6
6
  gem 'nokogiri', '~> 1.6'
7
+
8
+ gem 'byebug', :group => ['development', 'test']
data/README.md CHANGED
@@ -76,7 +76,7 @@ For more usage, see API documentation generated by rdoc.
76
76
 
77
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
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.
79
+ Create a file named aliyun-config.yml in path rspec/aliyunoss/config, fill in valid access key id and access key secret and cotinue to test.
80
80
 
81
81
  ## Contributing
82
82
 
data/lib/aliyunoss.rb CHANGED
@@ -3,10 +3,10 @@ require 'nokogiri'
3
3
  require 'net/http'
4
4
  require 'logger'
5
5
 
6
- require 'aliyunoss/helper'
7
6
  require 'aliyunoss/config'
7
+ require 'aliyunoss/config_helper'
8
8
  require 'aliyunoss/extension'
9
- require 'aliyunoss/http_response_extension'
10
9
  require 'aliyunoss/oss_request'
10
+ require 'aliyunoss/oss_exception'
11
11
  require 'aliyunoss/api'
12
12
  require 'aliyunoss/bucket'
data/lib/aliyunoss/api.rb CHANGED
@@ -160,7 +160,10 @@ HERE
160
160
  request.put
161
161
  end
162
162
 
163
-
163
+ # get share url for specified object
164
+ def generate_share_url(bucket, path, expires_in = 3600)
165
+ Aliyun::Oss::OssRequest.new(bucket, path).url_for_sharing(expires_in)
166
+ end
164
167
 
165
168
  # Post Object
166
169
  # Not implemented
@@ -186,13 +189,13 @@ HERE
186
189
  request = Aliyun::Oss::OssRequest.new(target_bucket, target_path, 'partNumber'=> part_number.to_s, 'uploadId'=> upload_id)
187
190
  request['Content-Length'] = part_size
188
191
  request['x-oss-copy-source'] = "/" + source_bucket.name + source_path
189
- request['x-oss-copy-source-range'] = range if range
192
+ request['x-oss-copy-source-range'] = range if range
190
193
  request.put
191
194
  end
192
195
 
193
196
  def multipart_upload_complete(bucket, path, upload_id, part_list)
194
197
  request = Aliyun::Oss::OssRequest.new(bucket, path, 'uploadId'=> upload_id)
195
- xml = '<?xml version="1.0" encoding="UTF-8"?>'
198
+ xml = '<?xml version="1.0" encoding="UTF-8"?>'
196
199
  xml << '<CompleteMultipartUpload>'
197
200
  part_list.each_pair {|k,v| xml << "<Part><PartNumber>#{k}</PartNumber><ETag>#{v}</ETag></Part>"}
198
201
  xml << '</CompleteMultipartUpload>'
@@ -14,43 +14,47 @@ module Aliyun
14
14
 
15
15
  # Class methods
16
16
  def self.all
17
- Aliyun::Oss::API.list_bucket.to_buckets
17
+ Aliyun::Oss::API.list_bucket.raise_unless(Net::HTTPOK).to_buckets
18
18
  end
19
19
 
20
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
21
+ Aliyun::Oss::API.put_bucket(name, location).raise_unless(Net::HTTPOK)
22
+ Bucket.new(:name => name, :location=> location, :creation_date => Time.now)
26
23
  end
27
24
 
28
25
  # Instance Methods -- object download and upload
29
26
  def list_files(options = {})
30
- Aliyun::Oss::API.list_object(self, options).to_objects
27
+ Aliyun::Oss::API.list_object(self, options).raise_unless(Net::HTTPOK).to_objects
31
28
  end
32
29
 
33
30
  def upload(data, path, options = {})
34
- Aliyun::Oss::API.put_object(self, path, data, options).raise_if_not(Net::HTTPOK)
31
+ Aliyun::Oss::API.put_object(self, path, data, options).raise_unless(Net::HTTPOK)
35
32
  end
36
33
 
37
34
  def download(path, options = {})
38
- response = Aliyun::Oss::API.get_object(self, path, options)
39
- response.raise_if_not(Net::HTTPOK)
40
- response.body
35
+ Aliyun::Oss::API.get_object(self, path, options)
36
+ .raise_unless(Net::HTTPOK)
37
+ .body
38
+ end
39
+
40
+ def share(path, expires_in = 3600)
41
+ Aliyun::Oss::API.generate_share_url(self, path, expires_in)
41
42
  end
42
43
 
43
44
  def delete(path)
44
- Aliyun::Oss::API.delete_object(self, path).raise_if_not(Net::HTTPNoContent)
45
+ Aliyun::Oss::API.delete_object(self, path).raise_unless(Net::HTTPNoContent)
45
46
  end
46
47
 
47
48
  # Multipart upload and copy
48
49
  def multipart_pending
49
- Aliyun::Oss::API.multipart_upload_unfinished_task(self).to_mutlipart_task
50
+ Aliyun::Oss::API.multipart_upload_unfinished_task(self)
51
+ .raise_unless(Net::HTTPOK)
52
+ .to_mutlipart_task
50
53
  end
51
54
 
52
55
  def multipart_abort(path, upload_id)
53
- Aliyun::Oss::API.multipart_upload_abort(self, path, upload_id).raise_if_not(Net::HTTPNoContent)
56
+ Aliyun::Oss::API.multipart_upload_abort(self, path, upload_id)
57
+ .raise_unless(Net::HTTPNoContent)
54
58
  end
55
59
 
56
60
  def multipart_upload(data)
@@ -58,7 +62,7 @@ module Aliyun
58
62
  response = Aliyun::Oss::API.multipart_upload_part(self, @multipart_path,
59
63
  @multipart_id,
60
64
  data, @multipart_sequence)
61
- response.raise_if_not(Net::HTTPOK)
65
+ response.raise_unless(Net::HTTPOK)
62
66
  @multipart_list[@multipart_sequence] = response['ETag']
63
67
  @multipart_sequence = @multipart_sequence + 1
64
68
  end
@@ -69,14 +73,16 @@ module Aliyun
69
73
  self, @multipart_path,
70
74
  @multipart_sequence,
71
75
  size, range)
72
- response.raise_if_not(Net::HTTPOK)
76
+ response.raise_unless(Net::HTTPOK)
73
77
  @multipart_list[@multipart_sequence] = response['ETag']
74
78
  @multipart_sequence = @multipart_sequence + 1
75
79
  end
76
80
 
77
81
  def multipart_upload_initiate(path)
78
82
  @multipart_path = path
79
- @multipart_id = Aliyun::Oss::API.multipart_upload_initiate(self, path).to_multipart_id
83
+ @multipart_id = Aliyun::Oss::API.multipart_upload_initiate(self, path)
84
+ .raise_unless(Net::HTTPOK)
85
+ .to_multipart_id
80
86
  @multipart_list = {}
81
87
  @multipart_sequence = 1
82
88
  end
@@ -85,45 +91,45 @@ module Aliyun
85
91
  Aliyun::Oss::API.multipart_upload_complete(self, @multipart_path,
86
92
  @multipart_id,
87
93
  @multipart_list)
88
- .raise_if_not(Net::HTTPOK)
94
+ .raise_unless(Net::HTTPOK)
89
95
  @multipart_path = nil
90
96
  end
91
97
 
92
98
  # Instance Methods -- miscellaneous
93
99
  def delete!
94
- Aliyun::Oss::API.delete_bucket(self).raise_if_not(Net::HTTPNoContent)
100
+ Aliyun::Oss::API.delete_bucket(self).raise_unless(Net::HTTPNoContent)
95
101
  end
96
102
 
97
103
  def disable_logging
98
- Aliyun::Oss::API.delete_logging(self).raise_if_not(Net::HTTPNoContent)
104
+ Aliyun::Oss::API.delete_logging(self).raise_unless(Net::HTTPNoContent)
99
105
  end
100
106
 
101
107
  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)
108
+ Aliyun::Oss::API.enable_bucket_logging(self, target_bucket_name, log_prefix).raise_unless(Net::HTTPOK)
103
109
  end
104
110
 
105
111
  def logging_status
106
- Aliyun::Oss::API.get_bucket_logging(self).to_logging_status
112
+ Aliyun::Oss::API.get_bucket_logging(self).raise_unless(Net::HTTPOK).to_logging_status
107
113
  end
108
114
 
109
115
  def disable_website_access
110
- Aliyun::Oss::API.delete_website(self).raise_if_not(Net::HTTPNoContent)
116
+ Aliyun::Oss::API.delete_website(self).raise_unless(Net::HTTPNoContent)
111
117
  end
112
118
 
113
119
  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)
120
+ Aliyun::Oss::API.put_bucket_website(self, index_page, error_page).raise_unless(Net::HTTPOK)
115
121
  end
116
122
 
117
123
  def website_access_status
118
- Aliyun::Oss::API.get_bucket_website(self).to_website_status
124
+ Aliyun::Oss::API.get_bucket_website(self).raise_unless(Net::HTTPOK).to_website_status
119
125
  end
120
126
 
121
127
  def set_acl(permission)
122
- Aliyun::Oss::API.put_bucket_acl(self, permission).raise_if_not(Net::HTTPOK)
128
+ Aliyun::Oss::API.put_bucket_acl(self, permission).raise_unless(Net::HTTPOK)
123
129
  end
124
130
 
125
131
  def get_acl
126
- Aliyun::Oss::API.get_bucket_acl(self).to_acl_status
132
+ Aliyun::Oss::API.get_bucket_acl(self).raise_unless(Net::HTTPOK).to_acl_status
127
133
  end
128
134
  end
129
135
 
File without changes
@@ -32,3 +32,72 @@ class String
32
32
  end
33
33
 
34
34
  end
35
+
36
+ # add methods to HTTPResponse for parsing results
37
+ module Net
38
+
39
+ class HTTPResponse
40
+
41
+ def raise_unless(status)
42
+ raise OssException.new(self) unless status === self
43
+ self
44
+ end
45
+
46
+ def to_buckets
47
+ raise message unless Net::HTTPOK === self
48
+ nodes = Nokogiri::XML(self.body).xpath('//Bucket') rescue []
49
+ nodes.map do |node|
50
+ bucket = Aliyun::Oss::Bucket.new
51
+ node.elements.each {|e| bucket.send("#{e.name.underscore}=".to_sym, e.content)}
52
+ bucket
53
+ end
54
+ end
55
+
56
+ def to_objects
57
+ raise message unless Net::HTTPOK === self
58
+ nodes = Nokogiri::XML(self.body).xpath('//Contents') rescue []
59
+ nodes.map do |node|
60
+ hash = Hash.new
61
+ node.elements.each {|e| hash[e.name.underscore] = e.content unless e.name == 'Owner' }
62
+ hash
63
+ end
64
+ end
65
+
66
+ def to_logging_status
67
+ raise message unless Net::HTTPOK === self
68
+ node = Nokogiri::XML(self.body).xpath('//LoggingEnabled') rescue []
69
+ hash = Hash.new
70
+ node[0].elements.each {|e| hash[e.name.underscore] = e.content }
71
+ hash
72
+ end
73
+
74
+ def to_website_status
75
+ raise message unless Net::HTTPOK === self
76
+ xml = Nokogiri::XML(self.body)
77
+ {'index_page' => xml.xpath('//Suffix')[0].content, 'error_page' => xml.xpath('//Key')[0].content } rescue {}
78
+ end
79
+
80
+ def to_acl_status
81
+ raise message unless Net::HTTPOK === self
82
+ Nokogiri::XML(self.body).xpath('//Grant')[0].content
83
+ end
84
+
85
+ def to_multipart_id
86
+ raise message unless Net::HTTPOK === self
87
+ Nokogiri::XML(body).xpath('//UploadId').first.content
88
+ end
89
+
90
+ def to_mutlipart_task
91
+ raise message unless Net::HTTPOK === self
92
+ tasks = Array.new
93
+ nodes = Nokogiri::XML(response.body).xpath('//Upload')
94
+ nodes.each do |node|
95
+ path = '/' + node.xpath('Key').first.content
96
+ id = node.xpath('UploadId').first.content
97
+ tasks << {'path'=> path, 'upload_id' => id}
98
+ end
99
+ tasks
100
+ end
101
+ end
102
+
103
+ end
@@ -0,0 +1,17 @@
1
+ class OssException < RuntimeError
2
+ def initialize(http_response)
3
+ @response = http_response
4
+ end
5
+
6
+ def inspect
7
+ "Status: #{@response.class.to_s}\nBody: #{@response.body}"
8
+ end
9
+
10
+ def to_s
11
+ inspect
12
+ end
13
+
14
+ def message
15
+ inspect
16
+ end
17
+ end
@@ -52,7 +52,6 @@ module Aliyun
52
52
  Net::HTTP.start(uri.host, uri.port) do |http|
53
53
  response = http.request(request)
54
54
  logger.info(response.code.to_s + ' ' + response.message)
55
- logger.debug(response.body)
56
55
  end
57
56
  response
58
57
  end
@@ -64,8 +63,16 @@ module Aliyun
64
63
  add_operation :head
65
64
  add_operation :post
66
65
 
67
- def url_for_sharing(expires)
68
- raise "not implemented"
66
+ def url_for_sharing(expires_in)
67
+ uri = get_uri
68
+ request = Net::HTTP::Get.new(uri)
69
+ @headers.each_pair {|k,v| request[k] = v}
70
+ expires_at = (Time.now + expires_in).utc.to_i
71
+ request["Date"] = expires_at
72
+ uri.query = URI.encode_www_form({"OSSAccessKeyId" => access_key_id,
73
+ "Expires" => expires_at,
74
+ "Signature" => signature(request)})
75
+ uri.to_s
69
76
  end
70
77
 
71
78
  def [](key)
@@ -112,10 +119,7 @@ module Aliyun
112
119
  end
113
120
 
114
121
  return "" if hash.count == 0
115
-
116
- array = Array.new
117
- hash.each_pair {|k,v| array << k + ":" + v }
118
- array.sort.join("\n") << "\n"
122
+ hash.keys.sort.map{|k| "#{k}:#{hash[k]}"}.join("\n") << "\n"
119
123
  end
120
124
 
121
125
  # oss api 20140828.pdf - 4.2
@@ -131,9 +135,11 @@ module Aliyun
131
135
  def canonicalized_resource()
132
136
  return @path unless @bucket
133
137
  return "/#{@bucket.name}#{@path}" if @queries.count == 0
134
-
135
- array = Array.new
136
- @queries.each_pair {|k,v| array << (if v then k+"="+v else k end)}
138
+
139
+ array = @queries.keys.sort.map do |k|
140
+ if @queries[k] then "#{k}=#{@queries[k]}" else "#{k}" end
141
+ end
142
+
137
143
  "/#{@bucket.name}#{@path}?#{array.sort.join('&')}"
138
144
  end
139
145
 
@@ -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
@@ -1,19 +1,18 @@
1
1
  require 'rspec'
2
2
  require 'aliyunoss'
3
+ require 'spec_helper'
3
4
 
4
5
  describe Aliyun::Oss::API do
5
6
 
6
7
  before :all do
7
- Aliyun::Oss.configure_with('spec/aliyunoss/aliyun-config.yml')
8
- # Aliyun::Oss.configure(:log_level=> 'debug')
9
- bucket_name = 'aliyun-oss-gem-api-test'
10
- response = Aliyun::Oss::API.put_bucket(bucket_name)
8
+ Aliyun::Oss.configure_with('spec/aliyunoss/config/aliyun-config.yml')
9
+ bucket_name = "aliyunoss-gem-test-#{rand.to_s.delete('0.')}"
10
+ location = 'oss-cn-beijing'
11
+ response = Aliyun::Oss::API.put_bucket(bucket_name, location)
11
12
  expect(response).to be_a(Net::HTTPOK)
12
- end
13
13
 
14
- before :each do
15
14
  @api = Aliyun::Oss::API
16
- @bucket = Aliyun::Oss::Bucket.new(name: 'aliyun-oss-gem-api-test', location: 'oss-cn-hangzhou')
15
+ @bucket = Aliyun::Oss::Bucket.new(name: bucket_name, location: location)
17
16
  end
18
17
 
19
18
  it 'should get bucket list using GetService api' do
@@ -44,7 +43,7 @@ describe Aliyun::Oss::API do
44
43
  response = @api.get_bucket_location(@bucket)
45
44
  expect(response).to be_a(Net::HTTPOK)
46
45
  node = Nokogiri::XML(response.body).xpath('//LocationConstraint')[0]
47
- expect(node.content).to eq('oss-cn-hangzhou')
46
+ expect(node.content).to include('oss-cn-')
48
47
  end
49
48
 
50
49
  it 'should get bucket logging status' do
@@ -74,7 +73,7 @@ describe Aliyun::Oss::API do
74
73
  end
75
74
 
76
75
  it 'should upload file' do
77
- files = ['test-1.png','test-2.png','test-3.png'].map {|f| File.join(__dir__, f)}
76
+ files = ['test-1.png','test-2.png','test-3.png'].map {|f| File.join(__dir__, 'png', f)}
78
77
  files.each do |f|
79
78
  data = IO.read(f)
80
79
  response = @api.put_object(@bucket, "/" + f[/test-\d\.png/], data, 'Content-Type'=>'image/png')
@@ -82,6 +81,13 @@ describe Aliyun::Oss::API do
82
81
  end
83
82
  end
84
83
 
84
+ it 'should generate share url for uploaded file' do
85
+ path = '/test-1.png'
86
+ url = @api.generate_share_url(@bucket, path, 7200)
87
+ data = Net::HTTP.get(URI(url))
88
+ expect(data.length).to eq(File.open( File.join(__dir__, 'png', path)).size)
89
+ end
90
+
85
91
  it 'should copy object' do
86
92
  sources = ['/test-1.png','/test-2.png','/test-3.png']
87
93
  sources.each do |source_path|
@@ -96,7 +102,7 @@ describe Aliyun::Oss::API do
96
102
 
97
103
  it 'should get object from oss' do
98
104
  response = @api.get_object(@bucket, '/copy-test-1.png')
99
- file_name = File.join(__dir__, 'test-1.png')
105
+ file_name = File.join(__dir__, 'png', 'test-1.png')
100
106
  expect(response['Content-Length']).to eq(File.size(file_name).to_s)
101
107
  expect(response['Content-Type']).to eq('image/png')
102
108
  end
@@ -110,13 +116,12 @@ describe Aliyun::Oss::API do
110
116
 
111
117
  it 'should delete multiple objects' do
112
118
  response = @api.delete_multiple_objects(@bucket, ['copy-test-2.png','copy-test-3.png', 'copy-test-1.png'])
113
- puts response.body
114
119
  expect(response).to be_a(Net::HTTPOK)
115
120
  end
116
121
 
117
122
  it 'should delete this bucket' do
118
- response = @api.delete_bucket(@bucket)
119
- expect(response).to be_a(Net::HTTPNoContent)
123
+ response = @api.delete_bucket(@bucket)
124
+ expect(response).to be_a(Net::HTTPNoContent)
120
125
  end
121
126
 
122
127
  end
@@ -1,16 +1,14 @@
1
1
  require 'rspec'
2
2
  require 'aliyunoss'
3
+ require 'spec_helper'
3
4
 
4
5
  describe Aliyun::Oss::Bucket do
5
6
 
6
7
  before :all do
7
- Aliyun::Oss.configure_with('spec/aliyunoss/aliyun-config.yml')
8
- end
9
-
10
- before :each do
11
- @bucket_name = 'aliyun-oss-gem-api-test'
12
- @location = 'oss-cn-hangzhou'
13
- @bucket = Aliyun::Oss::Bucket.all.select {|b| b.name == @bucket_name} .first
8
+ Aliyun::Oss.configure_with('spec/aliyunoss/config/aliyun-config.yml')
9
+ @bucket_name = "aliyunoss-gem-test-#{rand.to_s.delete('0.')}"
10
+ @location = 'oss-cn-beijing'
11
+ @bucket = Aliyun::Oss::Bucket.new( location: @location, name: @bucket_name)
14
12
  end
15
13
 
16
14
  it 'should create a bucket' do
@@ -29,13 +27,19 @@ describe Aliyun::Oss::Bucket do
29
27
  end
30
28
 
31
29
  it 'should upload data to server' do
32
- files = ['test-1.png','test-2.png','test-3.png'].map {|f| File.join(__dir__, f)}
30
+ files = ['test-1.png','test-2.png','test-3.png'].map {|f| File.join(__dir__, 'png',f)}
33
31
  files.each do |f|
34
32
  data = IO.read(f)
35
33
  @bucket.upload(data, "/" + f[/test-\d\.png/], 'Content-Type'=>'image/png')
36
34
  end
37
35
  end
38
36
 
37
+ it 'should generate share url' do
38
+ url = @bucket.share("/test-1.png")
39
+ data = Net::HTTP.get(URI(url))
40
+ expect(data.length).to eq(File.open( File.join(__dir__, "png", "test-1.png")).size)
41
+ end
42
+
39
43
  it 'should upload data to server in multipart way' do
40
44
  path = '/multi-part-test.dat'
41
45
  @bucket.multipart_upload_initiate(path)
@@ -48,7 +52,6 @@ describe Aliyun::Oss::Bucket do
48
52
  end
49
53
 
50
54
  it 'should copy data in multipart way' do
51
- pending 'I get Forbidden using x-oss-copy-source-range'
52
55
  source_path = '/multi-part-test.dat'
53
56
  source_object = @bucket.list_files.select {|f| '/' + f['key'] == source_path} .first
54
57
  source_size = source_object['size'].to_i
@@ -62,7 +65,7 @@ describe Aliyun::Oss::Bucket do
62
65
  it 'should download file from server' do
63
66
  ['/test-1.png','/test-2.png','/test-3.png'].each do |path|
64
67
  remote_data = @bucket.download(path)
65
- local_data = IO.read(File.join(__dir__, path))
68
+ local_data = IO.read(File.join(__dir__, 'png', path))
66
69
  md5 = OpenSSL::Digest::MD5
67
70
  expect(md5.digest(remote_data)).to eq(md5.digest(local_data))
68
71
  end
@@ -75,25 +78,38 @@ describe Aliyun::Oss::Bucket do
75
78
  end
76
79
 
77
80
  it 'should get and modify logging status' do
78
- @bucket.enable_logging('aliyun-oss-gem-api-test', 'access_log')
79
- sleep(2)
80
- expect(@bucket.logging_status['target_bucket']).to eq('aliyun-oss-gem-api-test')
81
- expect(@bucket.logging_status['target_prefix']).to eq('access_log')
81
+ @bucket.enable_logging(@bucket_name, 'access_log')
82
+ status = @bucket.logging_status
83
+ # it seems that changes do not take effect immediately
84
+ # aliyun return loggint enabled status but without target_prefix
85
+ # so we retry
86
+ status = @bucket.logging_status
87
+ status = @bucket.logging_status if status.count == 0
88
+ status = @bucket.logging_status if status.count == 0
89
+ expect(status['target_bucket']).to eq(@bucket_name)
90
+ expect(status['target_prefix']).to eq('access_log')
82
91
  @bucket.disable_logging
83
92
  end
84
93
 
85
94
  it 'should get and modify website access status' do
86
95
  @bucket.enable_website_access('index.html','error.html')
87
- sleep(2)
88
- expect(@bucket.website_access_status['index_page']).to eq('index.html')
89
- expect(@bucket.website_access_status['error_page']).to eq('error.html')
96
+ retries = 0
97
+ begin
98
+ status = @bucket.website_access_status
99
+ rescue
100
+ retry if (retries+=1) <= 5
101
+ end
102
+ expect(status['index_page']).to eq('index.html')
103
+ expect(status['error_page']).to eq('error.html')
90
104
  @bucket.disable_website_access
91
105
  end
92
106
 
93
107
  it 'should get and set access control list' do
94
108
  @bucket.set_acl('public-read')
95
- sleep(2)
96
- expect(@bucket.get_acl).to eq('public-read')
109
+ status = @bucket.get_acl
110
+ status = @bucket.get_acl if status == 'private'
111
+ status = @bucket.get_acl if status == 'private'
112
+ expect(status).to eq('public-read')
97
113
  @bucket.set_acl('private')
98
114
  end
99
115
 
@@ -1,2 +1,3 @@
1
1
  access_key_id: "access key _id from aliyun"
2
2
  access_key_secret: "access key secret from aliyun"
3
+ log_level: "warn"
@@ -0,0 +1,3 @@
1
+ this is incorrect yaml
2
+ format file
3
+ access_key_id: "1234567890"
@@ -1,12 +1,12 @@
1
1
  require 'rspec'
2
2
  require 'aliyunoss'
3
+ require 'spec_helper'
3
4
 
4
5
  describe 'Aliyun::Oss Configuration' do
5
6
 
6
7
  before :each do
7
8
  config = Aliyun::Oss.config
8
9
  config[:log_level] = 'info'
9
- config[:host] = 'aliyuncs.com'
10
10
  config[:access_key_id] = nil
11
11
  config[:access_key_secret] = nil
12
12
  end
@@ -14,34 +14,28 @@ describe 'Aliyun::Oss Configuration' do
14
14
  it 'should load default options' do
15
15
  default_config = Aliyun::Oss.config
16
16
  expect(default_config).to include(:log_level)
17
- expect(default_config).to include(:host)
18
17
  expect(default_config).to include(:access_key_id)
19
18
  end
20
19
 
21
20
  it 'should accept configuration from #configure' do
22
21
  url = "http://bucket_name.region.aliyuncs.com"
23
22
  access_key = "access_key_from_aliyun"
24
- Aliyun::Oss.configure(:log_level => 'debug', :host => url, :access_key_id => access_key, :not_used_para => "not used")
23
+ Aliyun::Oss.configure(:log_level => 'debug', :access_key_id => access_key, :not_used_para => "not used")
25
24
  config = Aliyun::Oss.config
26
25
  expect(config.keys).not_to include(:not_used_para)
27
- expect(config[:host]).to eq(url)
28
26
  expect(config[:access_key_id]).to eq(access_key)
29
27
  expect(config[:log_level]).to eq('debug')
30
28
  end
31
29
 
32
30
  it 'should load yaml config file if specified' do
33
- Aliyun::Oss.configure_with('spec/aliyunoss/aliyun-config.yml.sample')
31
+ Aliyun::Oss.configure_with('spec/aliyunoss/config/aliyun-config.yml.sample')
34
32
  config = Aliyun::Oss.config
35
- expect(config[:host]).to eq('bucket_name.region.aliyuncs.com')
36
33
  expect(config[:access_key_id]).to eq('access key _id from aliyun')
37
34
  end
38
35
 
39
36
  it 'should load default configuration if incorrect yaml file specified' do
40
- Aliyun::Oss.configure_with('spec/aliyunoss/incorrect-config.yml')
37
+ Aliyun::Oss.configure_with('spec/aliyunoss/config/incorrect-config.yml')
41
38
  config = Aliyun::Oss.config
42
- expect(config[:host]).not_to eq('aliyun-yaml.com')
43
39
  expect(config[:access_key_id]).not_to eq('1234567890')
44
40
  end
45
-
46
- it 'should load configuration from RAILS_ROOT/config/aliyun-oss.yml in Rails project'
47
41
  end
@@ -1,19 +1,17 @@
1
1
  require 'rspec'
2
2
  require 'aliyunoss'
3
+ require 'spec_helper'
3
4
 
4
- describe Aliyun::Oss::API do
5
+ describe "multipart api" do
5
6
 
6
7
  before :all do
7
- Aliyun::Oss.configure_with('spec/aliyunoss/aliyun-config.yml')
8
- Aliyun::Oss.configure(:log_level=> 'debug')
9
- bucket_name = 'aliyun-oss-gem-api-test'
10
- response = Aliyun::Oss::API.put_bucket(bucket_name)
11
- expect(response).to be_a(Net::HTTPOK)
12
- end
13
-
14
- before :each do
8
+ Aliyun::Oss.configure_with('spec/aliyunoss/config/aliyun-config.yml')
9
+ bucket_name = "aliyunoss-gem-test-#{rand.to_s.delete('0.')}"
10
+ location = 'oss-cn-beijing'
11
+ response = Aliyun::Oss::API.put_bucket(bucket_name, location)
12
+ expect(response).to be_a(Net::HTTPOK)
15
13
  @api = Aliyun::Oss::API
16
- @bucket = Aliyun::Oss::Bucket.new(name: 'aliyun-oss-gem-api-test', location: 'oss-cn-hangzhou')
14
+ @bucket = Aliyun::Oss::Bucket.new(name: bucket_name, location: location)
17
15
  end
18
16
 
19
17
  it 'should upload data in multipart way' do
@@ -67,8 +65,6 @@ describe Aliyun::Oss::API do
67
65
  upload_id = Nokogiri::XML(response.body).xpath('//UploadId')[0].content
68
66
  expect(upload_id).to_not be_nil
69
67
 
70
- data = Random.new.bytes(1024 * rand(100..300))
71
-
72
68
  response = @api.multipart_upload_from_copy(upload_id, @bucket, '/multi-part-test.dat',
73
69
  @bucket, path, 1, 1024*500)
74
70
  expect(response).to be_a(Net::HTTPOK)
@@ -96,8 +92,8 @@ describe Aliyun::Oss::API do
96
92
  end
97
93
 
98
94
  it 'should delete this bucket' do
99
- response = Aliyun::Oss::API.delete_bucket(@bucket)
100
- expect(response).to be_a(Net::HTTPNoContent)
95
+ response = Aliyun::Oss::API.delete_bucket(@bucket)
96
+ expect(response).to be_a(Net::HTTPNoContent)
101
97
  end
102
-
98
+
103
99
  end
File without changes
File without changes
File without changes
@@ -0,0 +1,3 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+ Bundler.require(:default, "test")
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: aliyunoss
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - yijiecc
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-05-02 00:00:00.000000000 Z
11
+ date: 2015-07-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -69,21 +69,22 @@ files:
69
69
  - lib/aliyunoss/api.rb
70
70
  - lib/aliyunoss/bucket.rb
71
71
  - lib/aliyunoss/config.rb
72
+ - lib/aliyunoss/config_helper.rb
72
73
  - lib/aliyunoss/extension.rb
73
- - lib/aliyunoss/helper.rb
74
- - lib/aliyunoss/http_response_extension.rb
74
+ - lib/aliyunoss/oss_exception.rb
75
75
  - lib/aliyunoss/oss_request.rb
76
76
  - lib/aliyunoss/version.rb
77
- - spec/aliyunoss/aliyun-config.yml.sample
78
77
  - spec/aliyunoss/api_spec.rb
79
78
  - spec/aliyunoss/bucket_spec.rb
79
+ - spec/aliyunoss/config/aliyun-config.yml.sample
80
+ - spec/aliyunoss/config/incorrect-config.yml
80
81
  - spec/aliyunoss/config_spec.rb
81
- - spec/aliyunoss/incorrect-config.yml
82
82
  - spec/aliyunoss/multipart_api_spec.rb
83
83
  - spec/aliyunoss/oss_request_spec.rb
84
- - spec/aliyunoss/test-1.png
85
- - spec/aliyunoss/test-2.png
86
- - spec/aliyunoss/test-3.png
84
+ - spec/aliyunoss/png/test-1.png
85
+ - spec/aliyunoss/png/test-2.png
86
+ - spec/aliyunoss/png/test-3.png
87
+ - spec/spec_helper.rb
87
88
  homepage: ''
88
89
  licenses:
89
90
  - MIT
@@ -109,13 +110,14 @@ signing_key:
109
110
  specification_version: 4
110
111
  summary: A gem for accessing Aliyun Open Storage Service.
111
112
  test_files:
112
- - spec/aliyunoss/aliyun-config.yml.sample
113
113
  - spec/aliyunoss/api_spec.rb
114
114
  - spec/aliyunoss/bucket_spec.rb
115
+ - spec/aliyunoss/config/aliyun-config.yml.sample
116
+ - spec/aliyunoss/config/incorrect-config.yml
115
117
  - spec/aliyunoss/config_spec.rb
116
- - spec/aliyunoss/incorrect-config.yml
117
118
  - spec/aliyunoss/multipart_api_spec.rb
118
119
  - spec/aliyunoss/oss_request_spec.rb
119
- - spec/aliyunoss/test-1.png
120
- - spec/aliyunoss/test-2.png
121
- - spec/aliyunoss/test-3.png
120
+ - spec/aliyunoss/png/test-1.png
121
+ - spec/aliyunoss/png/test-2.png
122
+ - spec/aliyunoss/png/test-3.png
123
+ - spec/spec_helper.rb
@@ -1,66 +0,0 @@
1
- module Net
2
-
3
- class HTTPResponse
4
-
5
- def raise_if_not(status)
6
- raise message unless status === self
7
- end
8
-
9
- def to_buckets
10
- raise message unless Net::HTTPOK === self
11
- nodes = Nokogiri::XML(self.body).xpath('//Bucket') rescue []
12
- nodes.map do |node|
13
- bucket = Aliyun::Oss::Bucket.new
14
- node.elements.each {|e| bucket.send("#{e.name.underscore}=".to_sym, e.content)}
15
- bucket
16
- end
17
- end
18
-
19
- def to_objects
20
- raise message unless Net::HTTPOK === self
21
- nodes = Nokogiri::XML(self.body).xpath('//Contents') rescue []
22
- nodes.map do |node|
23
- hash = Hash.new
24
- node.elements.each {|e| hash[e.name.underscore] = e.content unless e.name == 'Owner' }
25
- hash
26
- end
27
- end
28
-
29
- def to_logging_status
30
- raise message unless Net::HTTPOK === self
31
- node = Nokogiri::XML(self.body).xpath('//LoggingEnabled') rescue []
32
- hash = Hash.new
33
- node[0].elements.each {|e| hash[e.name.underscore] = e.content }
34
- hash
35
- end
36
-
37
- def to_website_status
38
- raise message unless Net::HTTPOK === self
39
- xml = Nokogiri::XML(self.body)
40
- {'index_page' => xml.xpath('//Suffix')[0].content, 'error_page' => xml.xpath('//Key')[0].content } rescue {}
41
- end
42
-
43
- def to_acl_status
44
- raise message unless Net::HTTPOK === self
45
- Nokogiri::XML(self.body).xpath('//Grant')[0].content
46
- end
47
-
48
- def to_multipart_id
49
- raise message unless Net::HTTPOK === self
50
- Nokogiri::XML(body).xpath('//UploadId').first.content
51
- end
52
-
53
- def to_mutlipart_task
54
- raise message unless Net::HTTPOK === self
55
- tasks = Array.new
56
- nodes = Nokogiri::XML(response.body).xpath('//Upload')
57
- nodes.each do |node|
58
- path = '/' + node.xpath('Key').first.content
59
- id = node.xpath('UploadId').first.content
60
- tasks << {'path'=> path, 'upload_id' => id}
61
- end
62
- tasks
63
- end
64
- end
65
-
66
- end
@@ -1,4 +0,0 @@
1
- this is incorrect yaml
2
- format file
3
- host: incorrect-yaml.com
4
- access_key_id: "incorrect-1234567890"