ucloud_storage 0.0.14 → 0.0.15
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.
- checksums.yaml +7 -0
- data/CHANGELOG +7 -0
- data/README.md +1 -0
- data/lib/ucloud_storage.rb +1 -1
- data/lib/ucloud_storage/configuration.rb +1 -0
- data/lib/ucloud_storage/ucloud_storage.rb +26 -12
- data/lib/ucloud_storage/version.rb +1 -1
- data/spec/ucloud_storage_spec.rb +55 -80
- data/ucloud_storage.gemspec +2 -0
- metadata +37 -26
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 5d303cb69666f5c64756360a01a70a6f361ef97f
|
4
|
+
data.tar.gz: 268a5bebec78662393ec8e825e910eb39efe4391
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: beb96c80c80a0aaccde84472692b27fa9c449f139e8ee9407575ee621c339487ba286aa8157218affed5d2cfa27c26f54e8849aea9789335e94cbede93c4bda3
|
7
|
+
data.tar.gz: e99b54106c7728d65cda37db9a2c4929ad66793c9cb0bb13625c971ce1e87241bf985cc53661d0b855f0e307b05f483b3a4a9fc7713e9fdc9b63406f803375d7
|
data/CHANGELOG
CHANGED
data/README.md
CHANGED
data/lib/ucloud_storage.rb
CHANGED
@@ -6,18 +6,18 @@ module UcloudStorage
|
|
6
6
|
class NotAuthorized < StandardError; end
|
7
7
|
|
8
8
|
class UcloudStorage
|
9
|
-
attr_accessor :user, :pass, :storage_url, :auth_token
|
9
|
+
attr_accessor :user, :pass, :type, :storage_url, :auth_token
|
10
10
|
|
11
11
|
def initialize(options={})
|
12
12
|
@user = options.fetch(:user) { Configuration.user }
|
13
13
|
@pass = options.fetch(:pass) { Configuration.pass }
|
14
|
+
@type = options.fetch(:type) { Configuration.type || 'standard' }
|
14
15
|
@authorized = false
|
15
16
|
end
|
16
17
|
|
17
18
|
def authorize
|
18
|
-
response = HTTParty.get("
|
19
|
-
|
20
|
-
"X-Storage-Pass" => pass })
|
19
|
+
response = HTTParty.get(auth_url, headers: { "X-Storage-User" => user,
|
20
|
+
"X-Storage-Pass" => pass })
|
21
21
|
|
22
22
|
yield response if block_given?
|
23
23
|
|
@@ -39,14 +39,15 @@ module UcloudStorage
|
|
39
39
|
raise NotAuthorized if storage_url.nil?
|
40
40
|
|
41
41
|
file = File.new(file_path)
|
42
|
-
content_type =
|
42
|
+
content_type = get_content_type(file_path)
|
43
43
|
upload_blob(file.read, box_name, destination, content_type, &block)
|
44
44
|
end
|
45
45
|
|
46
46
|
def upload_blob(blob, box_name, destination, content_type, &block)
|
47
47
|
raise NotAuthorized if storage_url.nil?
|
48
48
|
|
49
|
-
|
49
|
+
target_url = File.join(storage_url, box_name, destination).to_s
|
50
|
+
response = HTTParty.put(target_url,
|
50
51
|
headers: {
|
51
52
|
"X-Auth-Token" => auth_token,
|
52
53
|
"Content-Type" => content_type,
|
@@ -75,10 +76,23 @@ module UcloudStorage
|
|
75
76
|
end
|
76
77
|
|
77
78
|
private
|
79
|
+
|
80
|
+
def auth_url
|
81
|
+
case type
|
82
|
+
when "standard"
|
83
|
+
"https://api.ucloudbiz.olleh.com/storage/v1/auth"
|
84
|
+
when 'standard-jpn'
|
85
|
+
"https://api.ucloudbiz.olleh.com/storage/v1/authjp"
|
86
|
+
when "lite"
|
87
|
+
"https://api.ucloudbiz.olleh.com/storage/v1/authlite"
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
78
91
|
def request(method, box_name, destination, success_code = [200], &block)
|
79
92
|
raise NotAuthorized if storage_url.nil?
|
80
93
|
|
81
|
-
|
94
|
+
target_url = File.join(storage_url, box_name, destination)
|
95
|
+
response = HTTParty.send(method, target_url, headers: { "X-Auth-Token" => auth_token })
|
82
96
|
return request(method, box_name, destination, success_code) if response.code == 401 and authorize
|
83
97
|
|
84
98
|
yield response if block_given?
|
@@ -86,19 +100,19 @@ module UcloudStorage
|
|
86
100
|
end
|
87
101
|
|
88
102
|
# stolen from http://stackoverflow.com/a/16636012/1802026
|
89
|
-
def
|
103
|
+
def get_content_type(local_file_path)
|
90
104
|
png = Regexp.new("\x89PNG".force_encoding("binary"))
|
91
105
|
jpg = Regexp.new("\xff\xd8\xff\xe0\x00\x10JFIF".force_encoding("binary"))
|
92
106
|
jpg2 = Regexp.new("\xff\xd8\xff\xe1(.*){2}Exif".force_encoding("binary"))
|
93
107
|
case IO.read(local_file_path, 10)
|
94
108
|
when /^GIF8/
|
95
|
-
'gif'
|
109
|
+
'image/gif'
|
96
110
|
when /^#{png}/
|
97
|
-
'png'
|
111
|
+
'image/png'
|
98
112
|
when /^#{jpg}/
|
99
|
-
'
|
113
|
+
'image/jpeg'
|
100
114
|
when /^#{jpg2}/
|
101
|
-
'
|
115
|
+
'image/jpeg'
|
102
116
|
else
|
103
117
|
if local_file_path.end_with? '.txt'
|
104
118
|
'text/plain'
|
data/spec/ucloud_storage_spec.rb
CHANGED
@@ -33,6 +33,14 @@ describe UcloudStorage do
|
|
33
33
|
pass: auth_info["valid_user"]["pass"])
|
34
34
|
end
|
35
35
|
|
36
|
+
let(:valid_ucloud_lite) do
|
37
|
+
file = File.open(File.join(File.dirname(__FILE__), "/support/auth_info.yml"))
|
38
|
+
auth_info = YAML.load(file)
|
39
|
+
ucloud = UcloudStorage.new(user: auth_info["valid_user"]["user"],
|
40
|
+
pass: auth_info["valid_user"]["pass"],
|
41
|
+
type: 'lite')
|
42
|
+
end
|
43
|
+
|
36
44
|
let(:invalid_ucloud) do
|
37
45
|
invlaid_ucloud = UcloudStorage.new
|
38
46
|
invlaid_ucloud.user = "invalid_user@mintshop.com"
|
@@ -40,6 +48,10 @@ describe UcloudStorage do
|
|
40
48
|
invlaid_ucloud
|
41
49
|
end
|
42
50
|
|
51
|
+
let(:file_path) { File.join(File.dirname(__FILE__), "/fixtures/sample_file.txt") }
|
52
|
+
let(:box) { 'dev_box' }
|
53
|
+
let(:destination) { 'cropped_images/'+Pathname(file_path).basename.to_s }
|
54
|
+
|
43
55
|
describe '#authorize' do
|
44
56
|
it "can authorize with valid user/pass" do
|
45
57
|
VCR.use_cassette("storage/v1/auth") do
|
@@ -51,6 +63,16 @@ describe UcloudStorage do
|
|
51
63
|
end
|
52
64
|
end
|
53
65
|
|
66
|
+
it 'should be authorize with lite storage' do
|
67
|
+
VCR.use_cassette("storage/v1/authlite") do
|
68
|
+
valid_ucloud_lite.is_authorized?.should_not == true
|
69
|
+
valid_ucloud_lite.authorize.should == true
|
70
|
+
valid_ucloud_lite.storage_url.should_not be_nil
|
71
|
+
valid_ucloud_lite.auth_token.should_not be_nil
|
72
|
+
valid_ucloud_lite.is_authorized?.should == true
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
54
76
|
it "cannot authorize with invalid user/pass" do
|
55
77
|
VCR.use_cassette("storage/v1/auth_fail") do
|
56
78
|
invalid_ucloud.authorize.should == false
|
@@ -70,115 +92,90 @@ describe UcloudStorage do
|
|
70
92
|
|
71
93
|
describe "#upload" do
|
72
94
|
it "can upload a file" do
|
73
|
-
VCR.use_cassette(
|
95
|
+
VCR.use_cassette("v1/put_storage_object") do
|
74
96
|
valid_ucloud.authorize
|
97
|
+
valid_ucloud.upload(file_path, box, destination).should be_true
|
98
|
+
uploaded_url = "https://ssproxy.ucloudbiz.olleh.com/v1/AUTH_f46e842e-c688-460e-a70b-e6a4d30e9885/dev_box/cropped_images/sample_file.txt"
|
99
|
+
HTTParty.get(uploaded_url).header.code_type.should == Net::HTTPOK
|
100
|
+
valid_ucloud.delete(box, destination)
|
75
101
|
end
|
102
|
+
end
|
76
103
|
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
104
|
+
it "can upload a file with proper url" do
|
105
|
+
VCR.use_cassette("v1/put_storage_object_02") do
|
106
|
+
valid_ucloud.authorize
|
107
|
+
destination_with_slash = "/" + destination
|
108
|
+
valid_ucloud.upload(file_path, box, destination_with_slash).should be_true
|
109
|
+
uploaded_url = "https://ssproxy.ucloudbiz.olleh.com/v1/AUTH_f46e842e-c688-460e-a70b-e6a4d30e9885/dev_box/cropped_images/sample_file.txt"
|
110
|
+
HTTParty.get(uploaded_url).header.code_type.should == Net::HTTPOK
|
111
|
+
valid_ucloud.delete(box, destination)
|
83
112
|
end
|
84
113
|
end
|
85
114
|
|
86
115
|
it "should fail to upload with invalid file path" do
|
87
116
|
VCR.use_cassette('storage/v1/auth') do
|
88
117
|
valid_ucloud.authorize
|
118
|
+
invalid_file_path = File.join(File.dirname(__FILE__), "/fixtures/no_sample_file.txt")
|
119
|
+
expect {
|
120
|
+
valid_ucloud.upload(invalid_file_path, box, destination)
|
121
|
+
}.to raise_error(Errno::ENOENT)
|
89
122
|
end
|
90
|
-
|
91
|
-
file_path = File.join(File.dirname(__FILE__), "/fixtures/no_sample_file.txt")
|
92
|
-
box = 'dev'
|
93
|
-
destination = 'cropped_images/'+Pathname(file_path).basename.to_s
|
94
|
-
|
95
|
-
expect {
|
96
|
-
valid_ucloud.upload(file_path, box, destination)
|
97
|
-
}.to raise_error(Errno::ENOENT)
|
98
123
|
end
|
99
124
|
|
100
125
|
it "should fail to upload without authorization" do
|
101
|
-
file_path = File.join(File.dirname(__FILE__), "/fixtures/sample_file.txt")
|
102
|
-
box = 'dev'
|
103
|
-
destination = 'cropped_images/'+Pathname(file_path).basename.to_s
|
104
|
-
|
105
126
|
expect {
|
106
127
|
valid_ucloud.upload(file_path, box, destination).should be_true
|
107
128
|
}.to raise_error(UcloudStorage::NotAuthorized)
|
108
129
|
end
|
109
130
|
|
110
131
|
it "should retry to upload if authorization failure response" do
|
111
|
-
VCR.use_cassette(
|
132
|
+
VCR.use_cassette("v1/put_storage_object_with_auth_fail") do
|
112
133
|
valid_ucloud.authorize
|
113
|
-
|
114
|
-
|
115
|
-
file_path = File.join(File.dirname(__FILE__), "/fixtures/sample_file.txt")
|
116
|
-
box = 'dev'
|
117
|
-
destination = 'cropped_images/'+Pathname(file_path).basename.to_s
|
118
|
-
valid_ucloud.auth_token += "a"
|
134
|
+
valid_ucloud.auth_token += "a"
|
119
135
|
|
120
|
-
VCR.use_cassette("v1/put_storage_object_with_auth_fail") do
|
121
136
|
valid_ucloud.upload(file_path, box, destination) do |response|
|
122
137
|
response.code.should == 201
|
123
138
|
end
|
139
|
+
valid_ucloud.delete(box, destination)
|
124
140
|
end
|
125
141
|
end
|
126
142
|
|
127
143
|
it 'yields response' do
|
128
|
-
VCR.use_cassette('storage/v1/auth') do
|
129
|
-
valid_ucloud.authorize
|
130
|
-
end
|
131
|
-
|
132
|
-
file_path = File.join(File.dirname(__FILE__), "/fixtures/sample_file.txt")
|
133
|
-
box = 'dev'
|
134
|
-
destination = 'cropped_images/'+Pathname(file_path).basename.to_s
|
135
|
-
|
136
144
|
VCR.use_cassette("v1/put_storage_object") do
|
145
|
+
valid_ucloud.authorize
|
137
146
|
valid_ucloud.upload(file_path, box, destination) do |response|
|
138
147
|
response.code.should == 201
|
139
148
|
end
|
149
|
+
valid_ucloud.delete(box, destination)
|
140
150
|
end
|
141
151
|
end
|
142
152
|
end
|
143
153
|
|
144
154
|
describe "#delete" do
|
145
155
|
it 'should delete updated object' do
|
146
|
-
|
147
|
-
|
148
|
-
box = 'dev_box'
|
149
|
-
destination = 'cropped_images/'+Pathname(file_path).basename.to_s
|
150
|
-
|
151
|
-
VCR.use_cassette("v1/put_storage_object_02") do
|
156
|
+
VCR.use_cassette("v1/delete_storage_object_02") do
|
157
|
+
valid_ucloud.authorize
|
152
158
|
valid_ucloud.upload(file_path, box, destination)
|
153
|
-
end
|
154
159
|
|
155
|
-
VCR.use_cassette("v1/delete_storage_object_02") do
|
156
160
|
valid_ucloud.delete(box, destination) do |response|
|
157
161
|
response.code.should == 204
|
158
162
|
end.should == true
|
163
|
+
uploaded_url = "https://ssproxy.ucloudbiz.olleh.com/v1/AUTH_f46e842e-c688-460e-a70b-e6a4d30e9885/dev_box/cropped_images/sample_file.txt"
|
164
|
+
HTTParty.get(uploaded_url).header.code_type.should == Net::HTTPNotFound
|
159
165
|
end
|
160
166
|
end
|
161
167
|
end
|
162
168
|
|
163
169
|
describe "#exist" do
|
164
170
|
it "should retry to upload if authorization failure response" do
|
165
|
-
VCR.use_cassette(
|
171
|
+
VCR.use_cassette("v1/object_exists") do
|
166
172
|
valid_ucloud.authorize
|
167
|
-
end
|
168
|
-
|
169
|
-
file_path = File.join(File.dirname(__FILE__), "/fixtures/sample_file.txt")
|
170
|
-
box = 'dev'
|
171
|
-
destination = 'cropped_images/'+Pathname(file_path).basename.to_s
|
172
|
-
|
173
|
-
VCR.use_cassette("v1/put_storage_object_04") do
|
174
173
|
valid_ucloud.upload(file_path, box, destination) do |response|
|
175
174
|
response.code.should == 201
|
176
175
|
end
|
177
|
-
end
|
178
176
|
|
179
|
-
|
177
|
+
valid_ucloud.auth_token += "a"
|
180
178
|
|
181
|
-
VCR.use_cassette("v1/get_storage_object)_04") do
|
182
179
|
valid_ucloud.get(box, destination) do |response|
|
183
180
|
[200, 304].should include(response.code)
|
184
181
|
end.should == true
|
@@ -186,39 +183,17 @@ describe UcloudStorage do
|
|
186
183
|
end
|
187
184
|
|
188
185
|
it 'should get updated object' do
|
189
|
-
|
190
|
-
|
191
|
-
box = 'dev_box'
|
192
|
-
destination = 'cropped_images/'+Pathname(file_path).basename.to_s
|
193
|
-
|
194
|
-
VCR.use_cassette("v1/put_storage_object_03") do
|
186
|
+
VCR.use_cassette("v1/get_updated_object") do
|
187
|
+
valid_ucloud.authorize
|
195
188
|
valid_ucloud.upload(file_path, box, destination)
|
196
|
-
end
|
197
189
|
|
198
|
-
VCR.use_cassette("v1/get_storage_object") do
|
199
190
|
result = false
|
200
191
|
valid_ucloud.get(box, destination) do |response|
|
201
192
|
result = true
|
202
|
-
|
193
|
+
[200, 304].should include(response.code)
|
203
194
|
end
|
204
195
|
result.should == true
|
205
|
-
|
206
|
-
end
|
207
|
-
|
208
|
-
it 'should get updated object' do
|
209
|
-
valid_ucloud.authorize
|
210
|
-
file_path = File.join(File.dirname(__FILE__), "/fixtures/sample_file.txt")
|
211
|
-
box = 'dev_box'
|
212
|
-
destination = 'cropped_images/'+Pathname(file_path).basename.to_s
|
213
|
-
|
214
|
-
VCR.use_cassette("v1/put_storage_object_03") do
|
215
|
-
valid_ucloud.upload(file_path, box, destination)
|
216
|
-
end
|
217
|
-
|
218
|
-
VCR.use_cassette("v1/get_storage_object") do
|
219
|
-
valid_ucloud.get(box, destination) do |response|
|
220
|
-
[200, 304].should include(response.code)
|
221
|
-
end.should == true
|
196
|
+
valid_ucloud.delete(box, destination)
|
222
197
|
end
|
223
198
|
end
|
224
199
|
end
|
data/ucloud_storage.gemspec
CHANGED
metadata
CHANGED
@@ -1,8 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ucloud_storage
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
5
|
-
prerelease:
|
4
|
+
version: 0.0.15
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- Sooyong Wang
|
@@ -10,52 +9,64 @@ authors:
|
|
10
9
|
autorequire:
|
11
10
|
bindir: bin
|
12
11
|
cert_chain: []
|
13
|
-
date:
|
12
|
+
date: 2014-01-09 00:00:00.000000000 Z
|
14
13
|
dependencies:
|
15
14
|
- !ruby/object:Gem::Dependency
|
16
15
|
name: rspec
|
17
|
-
requirement:
|
18
|
-
none: false
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
19
17
|
requirements:
|
20
|
-
- -
|
18
|
+
- - '>='
|
21
19
|
- !ruby/object:Gem::Version
|
22
20
|
version: '0'
|
23
21
|
type: :development
|
24
22
|
prerelease: false
|
25
|
-
version_requirements:
|
23
|
+
version_requirements: !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - '>='
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
version: '0'
|
26
28
|
- !ruby/object:Gem::Dependency
|
27
29
|
name: vcr
|
28
|
-
requirement:
|
29
|
-
none: false
|
30
|
+
requirement: !ruby/object:Gem::Requirement
|
30
31
|
requirements:
|
31
|
-
- -
|
32
|
+
- - '>='
|
32
33
|
- !ruby/object:Gem::Version
|
33
34
|
version: '0'
|
34
35
|
type: :development
|
35
36
|
prerelease: false
|
36
|
-
version_requirements:
|
37
|
+
version_requirements: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - '>='
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '0'
|
37
42
|
- !ruby/object:Gem::Dependency
|
38
43
|
name: webmock
|
39
|
-
requirement:
|
40
|
-
none: false
|
44
|
+
requirement: !ruby/object:Gem::Requirement
|
41
45
|
requirements:
|
42
|
-
- -
|
46
|
+
- - '>='
|
43
47
|
- !ruby/object:Gem::Version
|
44
48
|
version: '0'
|
45
49
|
type: :development
|
46
50
|
prerelease: false
|
47
|
-
version_requirements:
|
51
|
+
version_requirements: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - '>='
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '0'
|
48
56
|
- !ruby/object:Gem::Dependency
|
49
57
|
name: httparty
|
50
|
-
requirement:
|
51
|
-
none: false
|
58
|
+
requirement: !ruby/object:Gem::Requirement
|
52
59
|
requirements:
|
53
|
-
- -
|
60
|
+
- - '>='
|
54
61
|
- !ruby/object:Gem::Version
|
55
62
|
version: '0'
|
56
63
|
type: :runtime
|
57
64
|
prerelease: false
|
58
|
-
version_requirements:
|
65
|
+
version_requirements: !ruby/object:Gem::Requirement
|
66
|
+
requirements:
|
67
|
+
- - '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
59
70
|
description: ucloud storage API
|
60
71
|
email:
|
61
72
|
- wangsy@wangsy.com
|
@@ -80,28 +91,28 @@ files:
|
|
80
91
|
- spec/ucloud_storage_spec.rb
|
81
92
|
- ucloud_storage.gemspec
|
82
93
|
homepage: https://github.com/wangsy/ucloud-storage
|
83
|
-
licenses:
|
94
|
+
licenses:
|
95
|
+
- MIT
|
96
|
+
metadata: {}
|
84
97
|
post_install_message:
|
85
98
|
rdoc_options: []
|
86
99
|
require_paths:
|
87
100
|
- lib
|
88
101
|
required_ruby_version: !ruby/object:Gem::Requirement
|
89
|
-
none: false
|
90
102
|
requirements:
|
91
|
-
- -
|
103
|
+
- - '>='
|
92
104
|
- !ruby/object:Gem::Version
|
93
105
|
version: '0'
|
94
106
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
95
|
-
none: false
|
96
107
|
requirements:
|
97
|
-
- -
|
108
|
+
- - '>='
|
98
109
|
- !ruby/object:Gem::Version
|
99
110
|
version: '0'
|
100
111
|
requirements: []
|
101
112
|
rubyforge_project: ucloudstorage
|
102
|
-
rubygems_version:
|
113
|
+
rubygems_version: 2.0.6
|
103
114
|
signing_key:
|
104
|
-
specification_version:
|
115
|
+
specification_version: 4
|
105
116
|
summary: simple API for authorize, upload/delete files
|
106
117
|
test_files:
|
107
118
|
- spec/fixtures/sample_file.txt
|