alipan-sdk 0.1.2 → 0.1.4
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 +4 -4
- data/CHANGELOG.md +9 -1
- data/README.md +7 -1
- data/lib/alipan/adapter.rb +32 -0
- data/lib/alipan/drive.rb +20 -0
- data/lib/alipan/http.rb +15 -8
- data/lib/alipan/protocol.rb +128 -0
- data/lib/alipan/version.rb +1 -1
- data/lib/alipan.rb +1 -0
- metadata +2 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 5bcff45b623f26fc91df4adcdef634aa3fa9f663b9f596d2f6deea256483747e
|
|
4
|
+
data.tar.gz: 2da0798f5cc399130b22cf5b8d8176f21fb33fb55e6092992ff3057a07632a4f
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 8a76168fc5711230ddf63bb8f1d4cfb30a9cdf6a481e3bfcddb4150aea4e507ddd7176a5c3c3ecfb14a2dc6ea26001eac63a03320c94beb902576ad9dad61248
|
|
7
|
+
data.tar.gz: a378354ce0f356fe550b50947b1684e2330e0d5325304e70c40b4e1fb975679fd04a935a32299bdf87bb607fd8c7ecc35526377c066bdec2e2209814ebe599a9
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
|
@@ -35,9 +35,15 @@ client = Alipan::Client.new({:access_token=>"xxx"})
|
|
|
35
35
|
### 获取drive
|
|
36
36
|
drive = client.get_drive
|
|
37
37
|
|
|
38
|
-
###
|
|
38
|
+
### 获取文件列表
|
|
39
39
|
objects = drive.list_objects
|
|
40
40
|
|
|
41
|
+
### 获取文件
|
|
42
|
+
objects = drive.get_object(key, opts = {}, &block)
|
|
43
|
+
|
|
44
|
+
### 上传文件
|
|
45
|
+
objects = drive.put_object(key, opts = {}, &block)
|
|
46
|
+
|
|
41
47
|
## 更多
|
|
42
48
|
|
|
43
49
|
更多文档请查看:
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Alipan
|
|
4
|
+
class Adapter
|
|
5
|
+
|
|
6
|
+
def initialize()
|
|
7
|
+
@producer = Fiber.new do
|
|
8
|
+
yield self if block_given?
|
|
9
|
+
nil
|
|
10
|
+
end
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def read(length = nil, outbuf = nil)
|
|
14
|
+
chunk = @producer.resume
|
|
15
|
+
outbuf.replace(chunk) if outbuf && chunk
|
|
16
|
+
chunk
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def write(chunk)
|
|
20
|
+
Fiber.yield chunk.to_s.force_encoding(Encoding::ASCII_8BIT)
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
alias << write
|
|
24
|
+
|
|
25
|
+
def closed?
|
|
26
|
+
false
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def close
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
data/lib/alipan/drive.rb
CHANGED
|
@@ -31,5 +31,25 @@ module Alipan
|
|
|
31
31
|
|
|
32
32
|
obj
|
|
33
33
|
end
|
|
34
|
+
|
|
35
|
+
def put_object(key, opts = {}, &block)
|
|
36
|
+
file = opts[:file]
|
|
37
|
+
|
|
38
|
+
if file
|
|
39
|
+
@protocol.put_object(resource_drive_id, key, opts) do |sw|
|
|
40
|
+
File.open(File.expand_path(file), 'rb') do |f|
|
|
41
|
+
sw << f.read(Protocol::STREAM_CHUNK_SIZE) until f.eof?
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
else
|
|
45
|
+
@protocol.put_object(resource_drive_id, key, opts, &block)
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
def delete_object(key)
|
|
51
|
+
@protocol.delete_object(resource_drive_id, key)
|
|
52
|
+
end
|
|
53
|
+
|
|
34
54
|
end
|
|
35
55
|
end
|
data/lib/alipan/http.rb
CHANGED
|
@@ -66,14 +66,11 @@ module Alipan
|
|
|
66
66
|
:open_timeout => @config.open_timeout || OPEN_TIMEOUT,
|
|
67
67
|
:read_timeout => @config.read_timeout || READ_TIMEOUT
|
|
68
68
|
)
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
else
|
|
75
|
-
resp.return!(&blk)
|
|
76
|
-
end
|
|
69
|
+
begin
|
|
70
|
+
response = request.execute
|
|
71
|
+
rescue RestClient::ExceptionWithResponse => e
|
|
72
|
+
response = e.response
|
|
73
|
+
response = RestClient::Response.create(response.body, Net::HTTPResponse.new('1.1', 200, 'OK'), request)
|
|
77
74
|
end
|
|
78
75
|
|
|
79
76
|
unless response.is_a?(RestClient::Response)
|
|
@@ -88,3 +85,13 @@ module Alipan
|
|
|
88
85
|
end
|
|
89
86
|
end
|
|
90
87
|
end
|
|
88
|
+
|
|
89
|
+
module RestClient
|
|
90
|
+
module Payload
|
|
91
|
+
class Base
|
|
92
|
+
def headers
|
|
93
|
+
({'Content-Length' => size.to_s} if size) || {}
|
|
94
|
+
end
|
|
95
|
+
end
|
|
96
|
+
end
|
|
97
|
+
end
|
data/lib/alipan/protocol.rb
CHANGED
|
@@ -6,6 +6,8 @@ module Alipan
|
|
|
6
6
|
class Protocol
|
|
7
7
|
include Common::Logging
|
|
8
8
|
|
|
9
|
+
STREAM_CHUNK_SIZE = 16 * 1024
|
|
10
|
+
|
|
9
11
|
def initialize(config)
|
|
10
12
|
@config = config
|
|
11
13
|
@http = HTTP.new(config)
|
|
@@ -85,6 +87,10 @@ module Alipan
|
|
|
85
87
|
r = @http.post( {:sub_res => "/adrive/v1.0/openFile/get_by_path"}, {:body => payload.to_json})
|
|
86
88
|
body = JSON.parse(r.body)
|
|
87
89
|
|
|
90
|
+
if body.fetch(:code.to_s, '') == 'NotFound.File'
|
|
91
|
+
return nil
|
|
92
|
+
end
|
|
93
|
+
|
|
88
94
|
obj = Object.new(
|
|
89
95
|
:drive_id => body.fetch(:drive_id.to_s),
|
|
90
96
|
:file_id => body.fetch(:file_id.to_s),
|
|
@@ -122,5 +128,127 @@ module Alipan
|
|
|
122
128
|
|
|
123
129
|
obj
|
|
124
130
|
end
|
|
131
|
+
|
|
132
|
+
def put_object(drive_id, object_name, opts = {}, &block)
|
|
133
|
+
logger.debug("Begin put object, drive_id: #{drive_id}, object: "\
|
|
134
|
+
"#{object_name}, options: #{opts}")
|
|
135
|
+
|
|
136
|
+
obj = nil
|
|
137
|
+
need_dirs = Array.new
|
|
138
|
+
parent_file_id = 'root'
|
|
139
|
+
obj_dirname = File.dirname(object_name)
|
|
140
|
+
dirname = "#{obj_dirname}".start_with?("/") ? "#{obj_dirname}" : "/#{obj_dirname}"
|
|
141
|
+
|
|
142
|
+
until dirname == '/'
|
|
143
|
+
payload = {
|
|
144
|
+
:drive_id => drive_id,
|
|
145
|
+
:file_path => dirname
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
r = @http.post( {:sub_res => "/adrive/v1.0/openFile/get_by_path"}, {:body => payload.to_json})
|
|
149
|
+
body = JSON.parse(r.body)
|
|
150
|
+
|
|
151
|
+
if body.fetch(:code.to_s, '') == 'NotFound.File'
|
|
152
|
+
need_dirs.unshift File.basename(dirname)
|
|
153
|
+
else
|
|
154
|
+
if body.fetch(:type.to_s) != 'folder'
|
|
155
|
+
e = RuntimeError.new "File #{dirname} has already existed!"
|
|
156
|
+
logger.error(e.to_s)
|
|
157
|
+
raise e
|
|
158
|
+
else
|
|
159
|
+
parent_file_id = body.fetch(:file_id.to_s)
|
|
160
|
+
break
|
|
161
|
+
end
|
|
162
|
+
end
|
|
163
|
+
|
|
164
|
+
dirname = File.dirname(dirname)
|
|
165
|
+
end
|
|
166
|
+
|
|
167
|
+
need_dirs.each do |need_dir|
|
|
168
|
+
payload = {
|
|
169
|
+
:drive_id => drive_id,
|
|
170
|
+
:parent_file_id => parent_file_id,
|
|
171
|
+
:name => need_dir,
|
|
172
|
+
:type => 'folder',
|
|
173
|
+
:check_name_mode => 'refuse'
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
r = @http.post( {:sub_res => "/adrive/v1.0/openFile/create"}, {:body => payload.to_json})
|
|
177
|
+
body = JSON.parse(r.body)
|
|
178
|
+
|
|
179
|
+
if body.fetch(:exist.to_s) == true && body.fetch(:type.to_s) != 'folder'
|
|
180
|
+
e = RuntimeError.new "File #{dirname} has already existed!"
|
|
181
|
+
logger.error(e.to_s)
|
|
182
|
+
raise e
|
|
183
|
+
end
|
|
184
|
+
|
|
185
|
+
parent_file_id = body.fetch(:file_id.to_s)
|
|
186
|
+
end
|
|
187
|
+
|
|
188
|
+
payload = {
|
|
189
|
+
:drive_id => drive_id,
|
|
190
|
+
:parent_file_id => parent_file_id,
|
|
191
|
+
:name => File.basename(object_name),
|
|
192
|
+
:type => 'file',
|
|
193
|
+
:check_name_mode => 'refuse'
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
r = @http.post( {:sub_res => "/adrive/v1.0/openFile/create"}, {:body => payload.to_json})
|
|
197
|
+
body = JSON.parse(r.body)
|
|
198
|
+
|
|
199
|
+
file_id = body.fetch(:file_id.to_s)
|
|
200
|
+
upload_id = body.fetch(:upload_id.to_s)
|
|
201
|
+
|
|
202
|
+
body.fetch('part_info_list', Array.new).each do |part|
|
|
203
|
+
payload = Alipan::Adapter.new(&block)
|
|
204
|
+
|
|
205
|
+
@http.put( {:sub_res => part.fetch(:upload_url.to_s)}, { :headers => { 'Content-Type' => '', 'Transfer-Encoding' => 'chunked' }, :body => payload })
|
|
206
|
+
|
|
207
|
+
payload = {
|
|
208
|
+
:drive_id => drive_id,
|
|
209
|
+
:file_id => file_id,
|
|
210
|
+
:upload_id => upload_id
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
r = @http.post( {:sub_res => "/adrive/v1.0/openFile/complete"}, {:body => payload.to_json})
|
|
214
|
+
body = JSON.parse(r.body)
|
|
215
|
+
|
|
216
|
+
obj = Object.new(
|
|
217
|
+
:drive_id => body.fetch(:drive_id.to_s),
|
|
218
|
+
:file_id => body.fetch(:file_id.to_s),
|
|
219
|
+
:size => body.fetch(:size.to_s),
|
|
220
|
+
:parent_file_id => body.fetch(:parent_file_id.to_s),
|
|
221
|
+
:name => body.fetch(:name.to_s))
|
|
222
|
+
end
|
|
223
|
+
|
|
224
|
+
obj
|
|
225
|
+
end
|
|
226
|
+
|
|
227
|
+
def delete_object(drive_id, object_name)
|
|
228
|
+
logger.debug("Begin delete object, bucket: #{drive_id}, "\
|
|
229
|
+
"object: #{object_name}")
|
|
230
|
+
|
|
231
|
+
payload = {
|
|
232
|
+
:drive_id => drive_id,
|
|
233
|
+
:file_path => "#{object_name}".start_with?("/") ? "#{object_name}" : "/#{object_name}"
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
r = @http.post( {:sub_res => "/adrive/v1.0/openFile/get_by_path"}, {:body => payload.to_json})
|
|
237
|
+
body = JSON.parse(r.body)
|
|
238
|
+
|
|
239
|
+
if body.fetch(:code.to_s, '') == 'NotFound.File'
|
|
240
|
+
return
|
|
241
|
+
end
|
|
242
|
+
|
|
243
|
+
payload = {
|
|
244
|
+
:drive_id => drive_id,
|
|
245
|
+
:file_id => body.fetch(:file_id.to_s)
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
r = @http.post( {:sub_res => "/adrive/v1.0/openFile/delete"}, {:body => payload.to_json})
|
|
249
|
+
|
|
250
|
+
logger.debug("Done delete object")
|
|
251
|
+
end
|
|
252
|
+
|
|
125
253
|
end
|
|
126
254
|
end
|
data/lib/alipan/version.rb
CHANGED
data/lib/alipan.rb
CHANGED
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: alipan-sdk
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.1.
|
|
4
|
+
version: 0.1.4
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- freeshenls
|
|
@@ -36,6 +36,7 @@ files:
|
|
|
36
36
|
- README.md
|
|
37
37
|
- Rakefile
|
|
38
38
|
- lib/alipan.rb
|
|
39
|
+
- lib/alipan/adapter.rb
|
|
39
40
|
- lib/alipan/client.rb
|
|
40
41
|
- lib/alipan/common.rb
|
|
41
42
|
- lib/alipan/common/logging.rb
|