ucloud_storage 0.0.9 → 0.0.10
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/ucloud_storage/ucloud_storage.rb +61 -56
- data/lib/ucloud_storage/version.rb +1 -1
- data/spec/ucloud_storage_spec.rb +151 -134
- data/ucloud_storage.gemspec +1 -1
- metadata +11 -11
@@ -2,60 +2,65 @@
|
|
2
2
|
require 'httparty'
|
3
3
|
|
4
4
|
module UcloudStorage
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
5
|
+
class TotalyWrongException < StandardError; end
|
6
|
+
class NotAuthorized < StandardError; end
|
7
|
+
|
8
|
+
class UcloudStorage
|
9
|
+
attr_accessor :user, :pass, :storage_url, :auth_token
|
10
|
+
|
11
|
+
def initialize(options={})
|
12
|
+
@user = options.fetch(:user) { Configuration.user }
|
13
|
+
@pass = options.fetch(:pass) { Configuration.pass }
|
14
|
+
end
|
15
|
+
|
16
|
+
def authorize
|
17
|
+
response = HTTParty.get("https://api.ucloudbiz.olleh.com/storage/v1/auth/",
|
18
|
+
headers: { "X-Storage-User" => user,
|
19
|
+
"X-Storage-Pass" => pass })
|
20
|
+
|
21
|
+
yield response if block_given?
|
22
|
+
|
23
|
+
case response.code
|
24
|
+
when 200
|
25
|
+
self.storage_url = response.headers["X-Storage-Url"]
|
26
|
+
self.auth_token = response.headers["X-Auth-Token"]
|
27
|
+
true
|
28
|
+
when 401 then false
|
29
|
+
else raise TotalyWrongException
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def upload(file_path, box_name, destination, &block)
|
34
|
+
raise NotAuthorized if storage_url.nil?
|
35
|
+
|
36
|
+
file = File.new(file_path)
|
37
|
+
content_type = `file --mime-type #{file_path}`.split(": ").last
|
38
|
+
|
39
|
+
response = HTTParty.put(storage_url+ "/#{box_name}/#{destination}",
|
40
|
+
headers: {
|
41
|
+
"X-Auth-Token" => auth_token,
|
42
|
+
"Content-Type" => content_type,
|
43
|
+
"Content-Length" => file.size.to_s },
|
44
|
+
body: file.read)
|
45
|
+
|
46
|
+
if response.code == 401 and authorize
|
47
|
+
return upload(file_path,box_name,destination,&block)
|
48
|
+
end
|
49
|
+
|
50
|
+
yield response if block_given?
|
51
|
+
|
52
|
+
response.code == 201 ? true : false
|
53
|
+
end
|
54
|
+
|
55
|
+
def delete(box_name, destination)
|
56
|
+
raise NotAuthorized if storage_url.nil?
|
57
|
+
|
58
|
+
response = HTTParty.delete(storage_url+ "/#{box_name}/#{destination}",
|
59
|
+
headers: { "X-Auth-Token" => auth_token })
|
60
|
+
|
61
|
+
yield response if block_given?
|
62
|
+
|
63
|
+
response.code == 204 ? true : false
|
64
|
+
end
|
65
|
+
end
|
61
66
|
end
|
data/spec/ucloud_storage_spec.rb
CHANGED
@@ -6,138 +6,155 @@ require 'support/vcr'
|
|
6
6
|
require 'yaml'
|
7
7
|
|
8
8
|
describe UcloudStorage do
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
9
|
+
context "set user/pass info" do
|
10
|
+
before do
|
11
|
+
UcloudStorage.configure do |config|
|
12
|
+
config.user = "abc@mintshop.com"
|
13
|
+
config.pass = "my_api_key"
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
it "can set default user/pass info" do
|
18
|
+
UcloudStorage.user.should == "abc@mintshop.com"
|
19
|
+
UcloudStorage.pass.should == "my_api_key"
|
20
|
+
end
|
21
|
+
|
22
|
+
it 'uses default uesr/pass info when initialized' do
|
23
|
+
ucloud = UcloudStorage.new
|
24
|
+
ucloud.user.should == "abc@mintshop.com"
|
25
|
+
ucloud.pass.should == "my_api_key"
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
let(:valid_ucloud) do
|
30
|
+
file = File.open(File.join(File.dirname(__FILE__), "/support/auth_info.yml"))
|
31
|
+
auth_info = YAML.load(file)
|
32
|
+
ucloud = UcloudStorage.new(user: auth_info["valid_user"]["user"],
|
33
|
+
pass: auth_info["valid_user"]["pass"])
|
34
|
+
end
|
35
|
+
|
36
|
+
let(:invalid_ucloud) do
|
37
|
+
invlaid_ucloud = UcloudStorage.new
|
38
|
+
invlaid_ucloud.user = "invalid_user@mintshop.com"
|
39
|
+
invlaid_ucloud.pass = "please download mintshop"
|
40
|
+
invlaid_ucloud
|
41
|
+
end
|
42
|
+
|
43
|
+
describe '#authorize' do
|
44
|
+
it "can authorize with valid user/pass" do
|
45
|
+
VCR.use_cassette("storage/v1/auth") do
|
46
|
+
valid_ucloud.authorize.should == true
|
47
|
+
valid_ucloud.storage_url.should_not be_nil
|
48
|
+
valid_ucloud.auth_token.should_not be_nil
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
it "cannot authorize with invalid user/pass" do
|
53
|
+
VCR.use_cassette("storage/v1/auth_fail") do
|
54
|
+
invalid_ucloud.authorize.should == false
|
55
|
+
invalid_ucloud.storage_url.should be_nil
|
56
|
+
invalid_ucloud.auth_token.should be_nil
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
it 'yields response' do
|
61
|
+
VCR.use_cassette("storage/v1/auth") do
|
62
|
+
valid_ucloud.authorize do |response|
|
63
|
+
response.code.should == 200
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
describe "#upload" do
|
70
|
+
it "can upload a file" do
|
71
|
+
VCR.use_cassette('storage/v1/auth') do
|
72
|
+
valid_ucloud.authorize
|
73
|
+
end
|
74
|
+
|
75
|
+
file_path = File.join(File.dirname(__FILE__), "/fixtures/sample_file.txt")
|
76
|
+
box = 'dev'
|
77
|
+
destination = 'cropped_images/'+Pathname(file_path).basename.to_s
|
78
|
+
|
79
|
+
VCR.use_cassette("v1/put_storage_object") do
|
80
|
+
valid_ucloud.upload(file_path, box, destination).should be_true
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
it "should fail to upload with invalid file path" do
|
85
|
+
VCR.use_cassette('storage/v1/auth') do
|
86
|
+
valid_ucloud.authorize
|
87
|
+
end
|
88
|
+
|
89
|
+
file_path = File.join(File.dirname(__FILE__), "/fixtures/no_sample_file.txt")
|
90
|
+
box = 'dev'
|
91
|
+
destination = 'cropped_images/'+Pathname(file_path).basename.to_s
|
92
|
+
|
93
|
+
expect {
|
94
|
+
valid_ucloud.upload(file_path, box, destination)
|
95
|
+
}.to raise_error(Errno::ENOENT)
|
96
|
+
end
|
97
|
+
|
98
|
+
it "should fail to upload without authorization" do
|
99
|
+
file_path = File.join(File.dirname(__FILE__), "/fixtures/sample_file.txt")
|
100
|
+
box = 'dev'
|
101
|
+
destination = 'cropped_images/'+Pathname(file_path).basename.to_s
|
102
|
+
|
103
|
+
expect {
|
104
|
+
valid_ucloud.upload(file_path, box, destination).should be_true
|
105
|
+
}.to raise_error(UcloudStorage::NotAuthorized)
|
106
|
+
end
|
107
|
+
|
108
|
+
it "should retry to upload if authorization failure response" do
|
109
|
+
VCR.use_cassette('storage/v1/auth') do
|
110
|
+
valid_ucloud.authorize
|
111
|
+
end
|
112
|
+
|
113
|
+
file_path = File.join(File.dirname(__FILE__), "/fixtures/sample_file.txt")
|
114
|
+
box = 'dev'
|
115
|
+
destination = 'cropped_images/'+Pathname(file_path).basename.to_s
|
116
|
+
valid_ucloud.auth_token += "a"
|
117
|
+
|
118
|
+
VCR.use_cassette("v1/put_storage_object_with_auth_fail") do
|
119
|
+
valid_ucloud.upload(file_path, box, destination) do |response|
|
120
|
+
response.code.should == 201
|
121
|
+
end
|
122
|
+
end
|
123
|
+
end
|
124
|
+
|
125
|
+
it 'yields response' do
|
126
|
+
VCR.use_cassette('storage/v1/auth') do
|
127
|
+
valid_ucloud.authorize
|
128
|
+
end
|
129
|
+
|
130
|
+
file_path = File.join(File.dirname(__FILE__), "/fixtures/sample_file.txt")
|
131
|
+
box = 'dev'
|
132
|
+
destination = 'cropped_images/'+Pathname(file_path).basename.to_s
|
133
|
+
|
134
|
+
VCR.use_cassette("v1/put_storage_object") do
|
135
|
+
valid_ucloud.upload(file_path, box, destination) do |response|
|
136
|
+
response.code.should == 201
|
137
|
+
end
|
138
|
+
end
|
139
|
+
end
|
140
|
+
end
|
141
|
+
|
142
|
+
describe "#delete" do
|
143
|
+
it 'should delete updated object' do
|
144
|
+
valid_ucloud.authorize
|
145
|
+
file_path = File.join(File.dirname(__FILE__), "/fixtures/sample_file.txt")
|
146
|
+
box = 'dev_box'
|
147
|
+
destination = 'cropped_images/'+Pathname(file_path).basename.to_s
|
148
|
+
|
149
|
+
VCR.use_cassette("v1/put_storage_object_02") do
|
150
|
+
valid_ucloud.upload(file_path, box, destination)
|
151
|
+
end
|
152
|
+
|
153
|
+
VCR.use_cassette("v1/delete_storage_object_02") do
|
154
|
+
valid_ucloud.delete(box, destination) do |response|
|
155
|
+
response.code.should == 204
|
156
|
+
end.should == true
|
157
|
+
end
|
158
|
+
end
|
159
|
+
end
|
143
160
|
end
|
data/ucloud_storage.gemspec
CHANGED
@@ -5,7 +5,7 @@ Gem::Specification.new do |gem|
|
|
5
5
|
gem.authors = ["Sooyong Wang", "Kunha Park"]
|
6
6
|
gem.email = ["wangsy@wangsy.com", "potato9@gmail.com"]
|
7
7
|
gem.description = %q{ucloud storage API}
|
8
|
-
gem.summary = %q{simple API for authorize, upload files}
|
8
|
+
gem.summary = %q{simple API for authorize, upload/delete files}
|
9
9
|
gem.homepage = "https://github.com/wangsy/ucloud-storage"
|
10
10
|
|
11
11
|
gem.files = `git ls-files`.split($\)
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ucloud_storage
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.10
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -10,11 +10,11 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date: 2013-
|
13
|
+
date: 2013-06-12 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: rspec
|
17
|
-
requirement: &
|
17
|
+
requirement: &70267624686700 !ruby/object:Gem::Requirement
|
18
18
|
none: false
|
19
19
|
requirements:
|
20
20
|
- - ! '>='
|
@@ -22,10 +22,10 @@ dependencies:
|
|
22
22
|
version: '0'
|
23
23
|
type: :development
|
24
24
|
prerelease: false
|
25
|
-
version_requirements: *
|
25
|
+
version_requirements: *70267624686700
|
26
26
|
- !ruby/object:Gem::Dependency
|
27
27
|
name: vcr
|
28
|
-
requirement: &
|
28
|
+
requirement: &70267624686280 !ruby/object:Gem::Requirement
|
29
29
|
none: false
|
30
30
|
requirements:
|
31
31
|
- - ! '>='
|
@@ -33,10 +33,10 @@ dependencies:
|
|
33
33
|
version: '0'
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
|
-
version_requirements: *
|
36
|
+
version_requirements: *70267624686280
|
37
37
|
- !ruby/object:Gem::Dependency
|
38
38
|
name: webmock
|
39
|
-
requirement: &
|
39
|
+
requirement: &70267624685860 !ruby/object:Gem::Requirement
|
40
40
|
none: false
|
41
41
|
requirements:
|
42
42
|
- - ! '>='
|
@@ -44,10 +44,10 @@ dependencies:
|
|
44
44
|
version: '0'
|
45
45
|
type: :development
|
46
46
|
prerelease: false
|
47
|
-
version_requirements: *
|
47
|
+
version_requirements: *70267624685860
|
48
48
|
- !ruby/object:Gem::Dependency
|
49
49
|
name: httparty
|
50
|
-
requirement: &
|
50
|
+
requirement: &70267624685440 !ruby/object:Gem::Requirement
|
51
51
|
none: false
|
52
52
|
requirements:
|
53
53
|
- - ! '>='
|
@@ -55,7 +55,7 @@ dependencies:
|
|
55
55
|
version: '0'
|
56
56
|
type: :runtime
|
57
57
|
prerelease: false
|
58
|
-
version_requirements: *
|
58
|
+
version_requirements: *70267624685440
|
59
59
|
description: ucloud storage API
|
60
60
|
email:
|
61
61
|
- wangsy@wangsy.com
|
@@ -102,7 +102,7 @@ rubyforge_project: ucloudstorage
|
|
102
102
|
rubygems_version: 1.8.5
|
103
103
|
signing_key:
|
104
104
|
specification_version: 3
|
105
|
-
summary: simple API for authorize, upload files
|
105
|
+
summary: simple API for authorize, upload/delete files
|
106
106
|
test_files:
|
107
107
|
- spec/fixtures/sample_file.txt
|
108
108
|
- spec/support/auth_info.sample.yml
|