cos 0.1.0 → 0.1.1

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.
Files changed (51) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +12 -0
  3. data/.rspec +2 -0
  4. data/.travis.yml +13 -2
  5. data/Gemfile +4 -1
  6. data/LICENSE +191 -0
  7. data/README.md +2014 -17
  8. data/Rakefile +23 -6
  9. data/bin/cos +325 -0
  10. data/bin/setup +1 -3
  11. data/cos.gemspec +24 -13
  12. data/lib/cos.rb +41 -4
  13. data/lib/cos/api.rb +289 -0
  14. data/lib/cos/bucket.rb +731 -0
  15. data/lib/cos/checkpoint.rb +62 -0
  16. data/lib/cos/client.rb +58 -0
  17. data/lib/cos/config.rb +102 -0
  18. data/lib/cos/dir.rb +301 -0
  19. data/lib/cos/download.rb +252 -0
  20. data/lib/cos/exception.rb +62 -0
  21. data/lib/cos/file.rb +152 -0
  22. data/lib/cos/http.rb +95 -0
  23. data/lib/cos/logging.rb +47 -0
  24. data/lib/cos/resource.rb +201 -0
  25. data/lib/cos/signature.rb +119 -0
  26. data/lib/cos/slice.rb +292 -0
  27. data/lib/cos/struct.rb +49 -0
  28. data/lib/cos/tree.rb +165 -0
  29. data/lib/cos/util.rb +82 -0
  30. data/lib/cos/version.rb +2 -2
  31. data/spec/cos/bucket_spec.rb +562 -0
  32. data/spec/cos/client_spec.rb +77 -0
  33. data/spec/cos/dir_spec.rb +195 -0
  34. data/spec/cos/download_spec.rb +105 -0
  35. data/spec/cos/http_spec.rb +70 -0
  36. data/spec/cos/signature_spec.rb +83 -0
  37. data/spec/cos/slice_spec.rb +302 -0
  38. data/spec/cos/struct_spec.rb +38 -0
  39. data/spec/cos/tree_spec.rb +322 -0
  40. data/spec/cos/util_spec.rb +106 -0
  41. data/test/download_test.rb +44 -0
  42. data/test/list_test.rb +43 -0
  43. data/test/upload_test.rb +48 -0
  44. metadata +132 -21
  45. data/.idea/.name +0 -1
  46. data/.idea/cos.iml +0 -49
  47. data/.idea/encodings.xml +0 -6
  48. data/.idea/misc.xml +0 -14
  49. data/.idea/modules.xml +0 -8
  50. data/.idea/workspace.xml +0 -465
  51. data/bin/console +0 -14
@@ -0,0 +1,82 @@
1
+ # coding: utf-8
2
+
3
+ require 'digest'
4
+ require 'fileutils'
5
+
6
+ module COS
7
+ module Util
8
+
9
+ class << self
10
+
11
+ # 文件sha1
12
+ def file_sha1(file)
13
+ Digest::SHA1.file(file).hexdigest
14
+ end
15
+
16
+ # 字符串sha1
17
+ def string_sha1(string)
18
+ Digest::SHA1.hexdigest(string)
19
+ end
20
+
21
+ # 获取本地目录路径, 不存在会创建
22
+ def get_local_path(path, disable_mkdir = false)
23
+ local = File.expand_path(path)
24
+ unless File.exist?(local) and File.directory?(local)
25
+ # 创建目录
26
+ if disable_mkdir
27
+ raise LocalPathNotExist, "Local path #{local} not exist!"
28
+ else
29
+ FileUtils::mkdir_p(local)
30
+ end
31
+ end
32
+
33
+ local
34
+ end
35
+
36
+ # 解析list时的path
37
+ def get_list_path(path, name = '', is_file = false)
38
+ # 目录必须带"/"
39
+ path = "/#{path}" unless path.start_with?('/')
40
+
41
+ if is_file
42
+ # 文件
43
+ if path.end_with?('/')
44
+ "#{path}#{name}"
45
+ else
46
+ "#{path}/#{name}"
47
+ end
48
+ else
49
+ # 目录
50
+ if path.end_with?('/')
51
+ "#{path}#{name}/"
52
+ else
53
+ "#{path}/#{name}/"
54
+ end
55
+ end
56
+ end
57
+
58
+ # 获取resource_path
59
+ def get_resource_path(app_id, bucket, path, file = nil)
60
+ # file_name检测
61
+ if file and (file.end_with?('/') or file.start_with?('/'))
62
+ raise ClientError, "File name can't start or end with '/'"
63
+ end
64
+
65
+ # 目录必须带"/"
66
+ path = "/#{path}" unless path.start_with?('/')
67
+ path = "#{path}/" unless path.end_with?('/')
68
+
69
+ "/#{app_id}/#{bucket}#{path}#{file}"
70
+ end
71
+
72
+ # 获取resource_path, 不自动添加'/'
73
+ def get_resource_path_or_file(app_id, bucket, path)
74
+ # 目录必须带"/"
75
+ path = "/#{path}" unless path.start_with?('/')
76
+ "/#{app_id}/#{bucket}#{path}"
77
+ end
78
+
79
+ end
80
+
81
+ end
82
+ end
@@ -1,3 +1,3 @@
1
- module Cos
2
- VERSION = "0.1.0"
1
+ module COS
2
+ VERSION = '0.1.1'
3
3
  end
@@ -0,0 +1,562 @@
1
+ # coding: utf-8
2
+
3
+ require 'spec_helper'
4
+ require 'fileutils'
5
+
6
+ module COS
7
+
8
+ describe Bucket do
9
+
10
+ before :all do
11
+ stub_request(:get, "http://web.file.myqcloud.com/files/v1/100000/bucket_name/?op=stat").
12
+ to_return(:status => 200, :body => { code: 0, message: 'ok', data: {authority:'eWRPrivate'}}.to_json)
13
+
14
+ @config = {
15
+ app_id: '100000',
16
+ secret_id: 'secret_id',
17
+ secret_key: 'secret_key',
18
+ protocol: 'http',
19
+ default_bucket: 'bucket_name'
20
+ }
21
+ @bucket = Client.new(@config).bucket('bucket_name')
22
+ end
23
+
24
+ it 'should delete empty path success' do
25
+ stub_request(:get, "http://web.file.myqcloud.com/files/v1/100000/bucket_name/empty_path/?op=stat")
26
+ .to_return(:status => 200, :body => { code: 0, message: 'ok', data: {ctime: @time, mtime: @time}}.to_json)
27
+
28
+ stub_request(:post, "http://web.file.myqcloud.com/files/v1/100000/bucket_name/empty_path/")
29
+ .to_return(:status => 200, :body => { code: 0, message: 'ok', data: {}}.to_json)
30
+
31
+ path = @bucket.stat('empty_path/')
32
+ expect(path.delete!).to eq(true)
33
+ end
34
+
35
+ it 'should list folder' do
36
+ stub_request(:get, 'http://web.file.myqcloud.com/files/v1/100000/bucket_name/path/?context=&num=20&op=list&order=0&pattern=eListDirOnly').
37
+ to_return(:status => 200, :body => {
38
+ code: 0,
39
+ message: 'ok',
40
+ data: {
41
+ has_more: false,
42
+ context: '',
43
+ infos: [
44
+ {name: 'f1', ctime: Time.now.to_i.to_s, mtime: Time.now.to_i.to_s, biz_attr: ''},
45
+ {name: 'f2', ctime: Time.now.to_i.to_s, mtime: Time.now.to_i.to_s, biz_attr: ''}
46
+ ]
47
+ }
48
+ }.to_json, :headers => {})
49
+
50
+ size = 0
51
+ @bucket.list('/path/', {pattern: :dir_only}).each do |f|
52
+ size += 1
53
+ end
54
+
55
+ expect(WebMock).to have_requested(:get, 'http://web.file.myqcloud.com/files/v1/100000/bucket_name/path/?context=&num=20&op=list&order=0&pattern=eListDirOnly')
56
+
57
+ expect(size).to eq(2)
58
+ end
59
+
60
+ it 'should resource operators' do
61
+
62
+ @time = Time.now.to_i.to_s
63
+
64
+ stub_request(:get, 'http://web.file.myqcloud.com/files/v1/100000/bucket_name/path/?context=&num=20&op=list&order=0&pattern=eListBoth').
65
+ to_return(:status => 200, :body => {
66
+ code: 0,
67
+ message: 'ok',
68
+ data: {
69
+ has_more: false,
70
+ context: '',
71
+ infos: [
72
+ {name: 'd1', ctime: @time, mtime: @time, biz_attr: ''},
73
+ {name: 'd2', ctime: @time, mtime: @time, biz_attr: ''},
74
+ {name: 'f1', ctime: @time, mtime: @time, biz_attr: '', filesize: 100, filelen:100, access_url: 'url'},
75
+ ]
76
+ }
77
+ }.to_json, :headers => {})
78
+
79
+ stub_request(:post, "http://web.file.myqcloud.com/files/v1/100000/bucket_name/path/d1/").to_return(:status => 200, :body => { code: 0, message: 'ok', data: {ctime: @time, mtime: @time}}.to_json)
80
+
81
+ stub_request(:post, "http://web.file.myqcloud.com/files/v1/100000/bucket_name/path/d1/path2/").to_return(:status => 200, :body => { code: 0, message: 'ok', data: {ctime: @time, mtime: @time}}.to_json)
82
+
83
+ stub_request(:post, "http://web.file.myqcloud.com/files/v1/100000/bucket_name/path/d2/").to_return(:status => 200, :body => { code: 0, message: 'ok', data: {ctime: @time, mtime: @time}}.to_json)
84
+
85
+ stub_request(:post, "http://web.file.myqcloud.com/files/v1/100000/bucket_name/path/d2/path2/").to_return(:status => 200, :body => { code: 0, message: 'ok', data: {ctime: @time, mtime: @time}}.to_json)
86
+
87
+ stub_request(:post, "http://web.file.myqcloud.com/files/v1/100000/bucket_name/path/f1").to_return(:status => 200, :body => { code: 0, message: 'ok', data: {ctime: @time, mtime: @time}}.to_json)
88
+
89
+ stub_request(:get, "http://web.file.myqcloud.com/files/v1/100000/bucket_name/path/d1/?op=stat").to_return(:status => 200, :body => { code: 0, message: 'ok', data: {name: 'd1', ctime: @time, mtime: @time, biz_attr: ''}}.to_json)
90
+
91
+ stub_request(:get, "http://web.file.myqcloud.com/files/v1/100000/bucket_name/path/d2/?op=stat").to_return(:status => 200, :body => { code: 0, message: 'ok', data: {name: 'd2', ctime: @time, mtime: @time, biz_attr: ''}}.to_json)
92
+
93
+ stub_request(:get, "http://web.file.myqcloud.com/files/v1/100000/bucket_name/path/f1?op=stat").to_return(:status => 200, :body => { code: 0, message: 'ok', data: {name: 'f1', filesize: 100, ctime: @time, mtime: @time, biz_attr: '', access_url: 'url'}}.to_json)
94
+
95
+ stub_request(:get, "http://web.file.myqcloud.com/files/v1/100000/bucket_name/path/f1?op=stat").to_return(:status => 400, :body => { code: -166, message: '索引不存在'}.to_json)
96
+
97
+ stub_request(:get, "http://web.file.myqcloud.com/files/v1/100000/bucket_name/path/d1/?context=&num=1&op=list&order=0&pattern=eListBoth").
98
+ to_return(:status => 200, :body => {
99
+ code: 0,
100
+ message: 'ok',
101
+ data: {
102
+ has_more: false,
103
+ context: '',
104
+ filecount: 5,
105
+ dircount: 10,
106
+ infos: []
107
+ }
108
+ }.to_json, :headers => {})
109
+
110
+ stub_request(:get, "http://web.file.myqcloud.com/files/v1/100000/bucket_name/path/d2/?context=&num=1&op=list&order=0&pattern=eListBoth").
111
+ to_return(:status => 200, :body => {
112
+ code: 0,
113
+ message: 'ok',
114
+ data: {
115
+ has_more: false,
116
+ context: '',
117
+ filecount: 5,
118
+ dircount: 10,
119
+ infos: []
120
+ }
121
+ }.to_json, :headers => {})
122
+
123
+ @bucket.list('/path/').each do |f|
124
+
125
+ if f.is_a?(COS::COSDir) and f.type == 'dir'
126
+ expect(f.created_at).to eq(Time.at(@time.to_i))
127
+ expect(f.updated_at).to eq(Time.at(@time.to_i))
128
+ expect(f.stat.name).to eq(f.name)
129
+
130
+ expect(f.list_count[:files]).to eq(5)
131
+ expect(f.count).to eq(15)
132
+ expect(f.count_dirs).to eq(10)
133
+
134
+ expect(f.update('111biz_attr').biz_attr).to eq('111biz_attr')
135
+
136
+ f.create_folder('path2')
137
+
138
+ else
139
+ expect(f.to_hash[:filesize]).to eq(100)
140
+ expect(f.format_size).to eq('100B')
141
+ expect(f.complete?).to eq(true)
142
+ f.delete
143
+ expect(f.exist?).to eq(false)
144
+ end
145
+
146
+ end
147
+
148
+ expect(WebMock).to have_requested(:get, 'http://web.file.myqcloud.com/files/v1/100000/bucket_name/path/?context=&num=20&op=list&order=0&pattern=eListBoth')
149
+
150
+ expect(WebMock).to have_requested(:post, 'http://web.file.myqcloud.com/files/v1/100000/bucket_name/path/f1')
151
+ expect(WebMock).to have_requested(:post, 'http://web.file.myqcloud.com/files/v1/100000/bucket_name/path/d1/')
152
+ expect(WebMock).to have_requested(:post, 'http://web.file.myqcloud.com/files/v1/100000/bucket_name/path/d2/')
153
+
154
+ end
155
+
156
+ it 'should get stat , raise when exception, exist?' do
157
+
158
+ @time = Time.now.to_i.to_s
159
+
160
+ stub_request(:get, 'http://web.file.myqcloud.com/files/v1/100000/bucket_name/path/?context=&num=20&op=list&order=0&pattern=eListBoth').
161
+ to_return(:status => 200, :body => {
162
+ code: 0,
163
+ message: 'ok',
164
+ data: {
165
+ has_more: false,
166
+ context: '',
167
+ infos: [
168
+ {name: 'd1', ctime: @time, mtime: @time, biz_attr: ''},
169
+ {name: 'f1', ctime: @time, mtime: @time, biz_attr: '', filesize: 33333333333, filelen:33333333333},
170
+ ]
171
+ }
172
+ }.to_json, :headers => {})
173
+
174
+ stub_request(:get, 'http://web.file.myqcloud.com/files/v1/100000/bucket_name/?context=&num=1&op=list&order=0&pattern=eListBoth').
175
+ to_return(:status => 200, :body => {
176
+ code: 0,
177
+ message: 'ok',
178
+ data: {
179
+ has_more: false,
180
+ dircount: 1,
181
+ filecount: 1,
182
+ context: '',
183
+ infos: [
184
+ {name: 'd1', ctime: @time, mtime: @time, biz_attr: ''},
185
+ ]
186
+ }
187
+ }.to_json, :headers => {})
188
+
189
+ stub_request(:get, "http://web.file.myqcloud.com/files/v1/100000/bucket_name/path/f1?op=stat").to_return(:status => 200, :body => { code: 0, message: 'ok', data: {name: 'f1', filesize: 33333333333, filelen:33333333333, ctime: @time, mtime: @time, biz_attr: ''}}.to_json).times(2)
190
+
191
+ stub_request(:get, "http://web.file.myqcloud.com/files/v1/100000/bucket_name/path/d1/?op=stat").to_return(:status => 400, :body => { code: -111, message: '未知错误'}.to_json)
192
+
193
+ stub_request(:post, "http://web.file.myqcloud.com/files/v1/100000/bucket_name/path/f1")
194
+ .to_return(:status => 400, :body => { code: -111, message: '未知错误'}.to_json)
195
+
196
+ expect(@bucket.count).to eq(2)
197
+
198
+ @bucket.list('/path/', {}).each do |f|
199
+ if f.is_a?(COS::COSFile) and f.type == 'file'
200
+ expect(f.exist?).to eq(true)
201
+ expect(f.stat.size).to eq(33333333333)
202
+ expect(f.format_size).to eq('31.04GB')
203
+ expect(f.delete!).to eq(false)
204
+ else
205
+ expect do
206
+ f.exist?
207
+ end.to raise_error(ServerError)
208
+ end
209
+ end
210
+ end
211
+
212
+ it 'upload file slice to a path' do
213
+ @time = Time.now.to_i.to_s
214
+
215
+ stub_request(:get, "http://web.file.myqcloud.com/files/v1/100000/bucket_name/path/d1/?op=stat").
216
+ to_return(:status => 200, :body => {data:{name: 'd1', ctime: @time, mtime: @time, biz_attr: ''}}.to_json)
217
+
218
+ stub_request(:get, 'http://web.file.myqcloud.com/files/v1/100000/bucket_name/path/?context=&num=20&op=list&order=0&pattern=eListFileOnly').
219
+ to_return(:status => 200, :body => {
220
+ code: 0,
221
+ message: 'ok',
222
+ data: {
223
+ has_more: false,
224
+ context: '',
225
+ infos: [
226
+ {name: 'd1', ctime: @time, mtime: @time, biz_attr: ''},
227
+ {name: 'f1', ctime: @time, mtime: @time, biz_attr: '', filesize: 100, filelen:100},
228
+ ]
229
+ }
230
+ }.to_json, :headers => {})
231
+
232
+ stub_request(:post, 'http://web.file.myqcloud.com/files/v1/100000/bucket_name/path/d1/test_file').
233
+ to_return(:status => 200, :body => {
234
+ code: 0, message: 'ok', data: {session: 'session', slice_size: 10000, offset: 0, access_url: 'access_url'}
235
+ }.to_json)
236
+
237
+ stub_request(:get, "http://web.file.myqcloud.com/files/v1/100000/bucket_name/path/d1/test_file?op=stat").to_return(:status => 200, :body => {
238
+ code: 0, message: 'ok', data: {name: 'f1', filesize: 100, filelen:100, ctime: @time, mtime: @time, biz_attr: ''}
239
+ }.to_json)
240
+
241
+ @file = './file_to_upload.log'
242
+ @file_name = 'test_file'
243
+
244
+ File.delete("#{@file}.cpt") if File.exist?("#{@file}.cpt")
245
+ File.delete("#{@file}") if File.exist?("#{@file}")
246
+
247
+ File.open(@file, 'w') do |f|
248
+ (1..100).each do |i|
249
+ f.puts i.to_s.rjust(100, '0')
250
+ end
251
+ end
252
+
253
+ @bucket.list('/path/', {pattern: :file_only}).each do |f|
254
+ if f.is_a?(COS::COSDir) and f.type == 'dir'
255
+ file = f.upload(@file_name, @file, min_slice_size: 1)
256
+
257
+ expect(file.name).to eq('f1')
258
+ end
259
+ end
260
+
261
+ File.delete("#{@file}.cpt") if File.exist?("#{@file}.cpt")
262
+ File.delete("#{@file}") if File.exist?("#{@file}")
263
+ end
264
+
265
+ it 'upload file entire to a path, and errors' do
266
+ @time = Time.now.to_i.to_s
267
+
268
+ stub_request(:get, "http://web.file.myqcloud.com/files/v1/100000/bucket_name/path/d1/?op=stat").
269
+ to_return(:status => 200, :body => {data:{name: 'd1', ctime: @time, mtime: @time, biz_attr: ''}}.to_json)
270
+
271
+ stub_request(:get, 'http://web.file.myqcloud.com/files/v1/100000/bucket_name/path/?context=&num=20&op=list&order=0&pattern=eListBoth').
272
+ to_return(:status => 200, :body => {
273
+ code: 0,
274
+ message: 'ok',
275
+ data: {
276
+ has_more: false,
277
+ context: '',
278
+ infos: [
279
+ {name: 'd1', ctime: @time, mtime: @time, biz_attr: ''},
280
+ {name: 'f1', ctime: @time, mtime: @time, biz_attr: '', filesize: 100, filelen:100},
281
+ ]
282
+ }
283
+ }.to_json, :headers => {})
284
+
285
+ stub_request(:post, 'http://web.file.myqcloud.com/files/v1/100000/bucket_name/path/d1/test_file')
286
+ .to_return(:status => 400, :body => {
287
+ code: -999, message: 'server error' }.to_json).then
288
+ .to_return(:status => 200, :body => {
289
+ code: 0, message: 'ok', data: {session: 'session', slice_size: 10000, offset: 0, access_url: 'access_url'}
290
+ }.to_json)
291
+
292
+ stub_request(:get, "http://web.file.myqcloud.com/files/v1/100000/bucket_name/path/d1/test_file?op=stat").to_return(:status => 200, :body => {
293
+ code: 0, message: 'ok', data: {name: 'f1', filesize: 100, filelen:100, ctime: @time, mtime: @time, biz_attr: ''}
294
+ }.to_json)
295
+
296
+ @file = './file_to_upload.log'
297
+ @file_name = 'test_file'
298
+
299
+ File.delete("#{@file}.cpt") if File.exist?("#{@file}.cpt")
300
+ File.delete("#{@file}") if File.exist?("#{@file}")
301
+
302
+ File.open(@file, 'w') do |f|
303
+ (1..100).each do |i|
304
+ f.puts i.to_s.rjust(100, '0')
305
+ end
306
+ end
307
+
308
+ @bucket.list('/path/', {}).each do |f|
309
+ if f.is_a?(COS::COSDir) and f.type == 'dir'
310
+ f.upload(@file_name, @file, min_slice_size: 1000000)
311
+ end
312
+ end
313
+
314
+ File.delete("#{@file}.cpt") if File.exist?("#{@file}.cpt")
315
+ File.delete("#{@file}") if File.exist?("#{@file}")
316
+ end
317
+
318
+ it 'upload file entire to a path, raise error' do
319
+ @time = Time.now.to_i.to_s
320
+
321
+ stub_request(:get, "http://web.file.myqcloud.com/files/v1/100000/bucket_name/path/d1/?op=stat").
322
+ to_return(:status => 200, :body => {data:{name: 'd1', ctime: @time, mtime: @time, biz_attr: ''}}.to_json)
323
+
324
+ stub_request(:get, 'http://web.file.myqcloud.com/files/v1/100000/bucket_name/path/?context=&num=20&op=list&order=0&pattern=eListBoth').
325
+ to_return(:status => 200, :body => {
326
+ code: 0,
327
+ message: 'ok',
328
+ data: {
329
+ has_more: false,
330
+ context: '',
331
+ infos: [
332
+ {name: 'd1', ctime: @time, mtime: @time, biz_attr: ''},
333
+ {name: 'f1', ctime: @time, mtime: @time, biz_attr: '', filesize: 100, filelen:100},
334
+ ]
335
+ }
336
+ }.to_json, :headers => {})
337
+
338
+ stub_request(:post, 'http://web.file.myqcloud.com/files/v1/100000/bucket_name/path/d1/test_file')
339
+ .to_return(:status => 400, :body => {
340
+ code: -999, message: 'server error' }.to_json).times(4)
341
+
342
+ @file = './file_to_upload.log'
343
+ @file_name = 'test_file'
344
+
345
+ File.delete("#{@file}.cpt") if File.exist?("#{@file}.cpt")
346
+ File.delete("#{@file}") if File.exist?("#{@file}")
347
+
348
+ File.open(@file, 'w') do |f|
349
+ (1..100).each do |i|
350
+ f.puts i.to_s.rjust(100, '0')
351
+ end
352
+ end
353
+
354
+ @bucket.list('/path/', {}).each do |f|
355
+ if f.is_a?(COS::COSDir) and f.type == 'dir'
356
+ expect do
357
+ f.upload(@file_name, @file, min_slice_size: 1000000, upload_retry: 3)
358
+ end.to raise_error(ServerError)
359
+ end
360
+ end
361
+
362
+ expect(WebMock).to have_requested(:post, 'http://web.file.myqcloud.com/files/v1/100000/bucket_name/path/d1/test_file').times(4)
363
+
364
+ File.delete("#{@file}.cpt") if File.exist?("#{@file}.cpt")
365
+ File.delete("#{@file}") if File.exist?("#{@file}")
366
+ end
367
+
368
+ it 'test upload_all' do
369
+ @time = Time.now.to_i.to_s
370
+
371
+ local_path = "/tmp/cos_test/#{Time.now.to_i}"
372
+ FileUtils::mkdir_p(local_path)
373
+
374
+ File.open("#{local_path}/file1.txt", 'w') do |f|
375
+ (1..10).each do |i|
376
+ f.puts i.to_s.rjust(10, '0')
377
+ end
378
+ end
379
+
380
+ stub_request(:get, "http://web.file.myqcloud.com/files/v1/100000/bucket_name/upload_path/?op=stat").
381
+ to_return(:status => 200, :body => {data:{name: 'd1', ctime: @time, mtime: @time, biz_attr: ''}}.to_json)
382
+
383
+ stub_request(:post, "http://web.file.myqcloud.com/files/v1/100000/bucket_name/upload_path/file1.txt")
384
+ .to_return(:status => 200, :body => {
385
+ code: 0, message: 'ok', data: {session: 'session', slice_size: 10000, offset: 0, access_url: 'access_url'}
386
+ }.to_json)
387
+
388
+ stub_request(:get, "http://web.file.myqcloud.com/files/v1/100000/bucket_name/upload_path/file1.txt?op=stat").
389
+ to_return(:status => 200, :body => {data:{name: 'file1.txt', ctime: @time, mtime: @time, biz_attr: '', filesize: 100, filelen: 100, access_url: 'url'}}.to_json)
390
+
391
+ uploads = @bucket.upload_all('/upload_path', local_path, skip_error: true)
392
+
393
+ expect(uploads[0].path).to eq('/upload_path/file1.txt')
394
+ end
395
+
396
+ it 'test upload_all raise errors' do
397
+ @time = Time.now.to_i.to_s
398
+
399
+ local_path = "/tmp/cos_test/2_#{Time.now.to_i}"
400
+ FileUtils::mkdir_p(local_path)
401
+
402
+ File.open("#{local_path}/file1.txt", 'w') do |f|
403
+ (1..10).each do |i|
404
+ f.puts i.to_s.rjust(10, '0')
405
+ end
406
+ end
407
+
408
+ File.open("#{local_path}/file2.txt", 'w') do |f|
409
+ (1..10).each do |i|
410
+ f.puts i.to_s.rjust(10, '0')
411
+ end
412
+ end
413
+
414
+ stub_request(:get, "http://web.file.myqcloud.com/files/v1/100000/bucket_name/upload_path/?op=stat").
415
+ to_return(:status => 200, :body => {data:{name: 'd1', ctime: @time, mtime: @time, biz_attr: ''}}.to_json)
416
+
417
+ stub_request(:post, "http://web.file.myqcloud.com/files/v1/100000/bucket_name/upload_path/file1.txt")
418
+ .to_return(:status => 400, :body => {
419
+ code: -999, message: 'server error' }.to_json)
420
+
421
+ stub_request(:post, "http://web.file.myqcloud.com/files/v1/100000/bucket_name/upload_path/file2.txt")
422
+ .to_return(:status => 200, :body => {data:{name: 'file2.txt', ctime: @time, mtime: @time, biz_attr: '', filesize: 100, filelen: 100, access_url: 'url'}}.to_json)
423
+
424
+ stub_request(:get, "http://web.file.myqcloud.com/files/v1/100000/bucket_name/upload_path/file2.txt?op=stat").
425
+ to_return(:status => 200, :body => {data:{name: 'file2.txt', ctime: @time, mtime: @time, biz_attr: '', filesize: 100, filelen: 100, access_url: 'url'}}.to_json)
426
+
427
+ expect do
428
+ @uploads = @bucket.upload_all('/upload_path', local_path)
429
+ end.to raise_error(ServerError)
430
+ end
431
+
432
+ it 'test upload_all skip errors' do
433
+ @time = Time.now.to_i.to_s
434
+
435
+ local_path = "/tmp/cos_test/2_#{Time.now.to_i}"
436
+ FileUtils::mkdir_p(local_path)
437
+
438
+ File.open("#{local_path}/file1.txt", 'w') do |f|
439
+ (1..10).each do |i|
440
+ f.puts i.to_s.rjust(10, '0')
441
+ end
442
+ end
443
+
444
+ File.open("#{local_path}/file2.txt", 'w') do |f|
445
+ (1..10).each do |i|
446
+ f.puts i.to_s.rjust(10, '0')
447
+ end
448
+ end
449
+
450
+ stub_request(:get, "http://web.file.myqcloud.com/files/v1/100000/bucket_name/upload_path/?op=stat").
451
+ to_return(:status => 200, :body => {data:{name: 'd1', ctime: @time, mtime: @time, biz_attr: ''}}.to_json)
452
+
453
+ stub_request(:post, "http://web.file.myqcloud.com/files/v1/100000/bucket_name/upload_path/file1.txt")
454
+ .to_return(:status => 400, :body => {
455
+ code: -999, message: 'server error' }.to_json)
456
+
457
+ stub_request(:post, "http://web.file.myqcloud.com/files/v1/100000/bucket_name/upload_path/file2.txt")
458
+ .to_return(:status => 200, :body => {data:{name: 'file2.txt', ctime: @time, mtime: @time, biz_attr: '', filesize: 100, filelen: 100, access_url: 'url'}}.to_json)
459
+
460
+ stub_request(:get, "http://web.file.myqcloud.com/files/v1/100000/bucket_name/upload_path/file2.txt?op=stat").
461
+ to_return(:status => 200, :body => {data:{name: 'file2.txt', ctime: @time, mtime: @time, biz_attr: '', filesize: 100, filelen: 100, access_url: 'url'}}.to_json)
462
+
463
+ uploads = @bucket.upload_all('/upload_path', local_path, skip_error: true)
464
+
465
+ expect(uploads[0].path).to eq('/upload_path/file2.txt')
466
+
467
+ cp = @bucket.complete?('/upload_path/file2.txt')
468
+ expect(cp).to eq(true)
469
+ end
470
+
471
+ it 'test download all' do
472
+ @time = Time.now.to_i.to_s
473
+
474
+ local_path = "/tmp/cos_test/d_#{Time.now.to_i}"
475
+ FileUtils::mkdir_p(local_path)
476
+
477
+ stub_request(:get, "http://web.file.myqcloud.com/files/v1/100000/bucket_name/upload_path/?op=stat").
478
+ to_return(:status => 200, :body => {data:{name: 'd1', ctime: @time, mtime: @time, biz_attr: ''}}.to_json)
479
+
480
+ stub_request(:get, "http://web.file.myqcloud.com/files/v1/100000/bucket_name/upload_path/?context=&num=20&op=list&order=0&pattern=eListFileOnly").
481
+ to_return(:status => 200, :body => {
482
+ code: 0,
483
+ message: 'ok',
484
+ data: {
485
+ has_more: false,
486
+ dircount: 1,
487
+ filecount: 1,
488
+ context: '',
489
+ infos: [
490
+ {name: 'file1.txt', ctime: @time, mtime: @time, biz_attr: '', filesize: 100, filelen:100, access_url: 'url'},
491
+ ]
492
+ }
493
+ }.to_json, :headers => {})
494
+
495
+ stub_request(:get, %r{^(http?:\/\/)url\/\?sign=[^\s]+}).to_return(:status => 200, :body => "11111111111", :headers => {})
496
+
497
+ d = @bucket.download_all('/upload_path', local_path)
498
+ expect(d).to eq(["#{local_path}/file1.txt"])
499
+ end
500
+
501
+ it 'upload all auto_create_folder false' do
502
+ @time = Time.now.to_i.to_s
503
+
504
+ stub_request(:get, "http://web.file.myqcloud.com/files/v1/100000/bucket_name/upload_path1/?op=stat").
505
+ to_return(:status => 400, :body => {}.to_json)
506
+
507
+ expect do
508
+ @bucket.upload_all('/upload_path1', '/tmp', auto_create_folder: false)
509
+ end.to raise_error(ServerError)
510
+ end
511
+
512
+ it 'upload all auto_create_folder' do
513
+ @time = Time.now.to_i.to_s
514
+
515
+ stub_request(:get, "http://web.file.myqcloud.com/files/v1/100000/bucket_name/upload_path2/?op=stat")
516
+ .to_return(:status => 400, :body => {code:-166, message: '索引不存在'}.to_json).then.to_return(:status => 200, :body => {data:{name: 'upload_path2', ctime: @time, mtime: @time, biz_attr: ''}}.to_json)
517
+
518
+ stub_request(:post, "http://web.file.myqcloud.com/files/v1/100000/bucket_name/upload_path2/").
519
+ to_return(:status => 200, :body => {data:{name: 'upload_path2', ctime: @time, mtime: @time, biz_attr: ''}}.to_json)
520
+
521
+ stub_request(:post, "http://web.file.myqcloud.com/files/v1/100000/bucket_name/upload_path2/file1.txt").
522
+ to_return(:status => 200, :body => {data:{name: 'file1.txt', ctime: @time, mtime: @time, biz_attr: '', filesize: 100, filelen: 100, access_url: 'http://www.qq.com/url'}}.to_json)
523
+
524
+ stub_request(:get, "http://web.file.myqcloud.com/files/v1/100000/bucket_name/upload_path2/file1.txt?op=stat").
525
+ to_return(:status => 200, :body => {data:{name: 'file1.txt', ctime: @time, mtime: @time, biz_attr: '', filesize: 100, filelen: 100, access_url: 'http://www.qq.com/url'}}.to_json)
526
+
527
+ stub_request(:get, "http://web.file.myqcloud.com/files/v1/100000/bucket_name/upload_path2/?context=&num=1&op=list&order=0&pattern=eListBoth").
528
+ to_return(:status => 200, :body => {
529
+ code: 0,
530
+ message: 'ok',
531
+ data: {
532
+ has_more: false,
533
+ dircount: 0,
534
+ filecount: 1,
535
+ context: '',
536
+ infos: [
537
+ {name: 'file1.txt', ctime: @time, mtime: @time, biz_attr: '', filesize: 100, filelen:100, access_url: 'url'},
538
+ ]
539
+ }
540
+ }.to_json, :headers => {})
541
+
542
+ local_path = "/tmp/cos_test/5_#{Time.now.to_i}"
543
+ FileUtils::mkdir_p(local_path)
544
+
545
+ File.open("#{local_path}/file1.txt", 'w') do |f|
546
+ (1..10).each do |i|
547
+ f.puts i.to_s.rjust(10, '0')
548
+ end
549
+ end
550
+
551
+ uploads = @bucket.upload_all('/upload_path2', local_path, auto_create_folder: true)
552
+ expect(uploads.count).to eq(1)
553
+
554
+ expect(uploads[0].url(:cname => 'www.domain.com', :https => true).start_with?('https://www.domain.com/url?sign=')).to eq(true)
555
+
556
+ expect(@bucket.stat('upload_path2/').empty?).to eq(false)
557
+
558
+ end
559
+
560
+ end
561
+
562
+ end