aliyun-oss-rails4 0.7.0.1448446959

Sign up to get free protection for your applications and to get access to all the features.
Files changed (57) hide show
  1. checksums.yaml +7 -0
  2. data/COPYING +19 -0
  3. data/INSTALL +35 -0
  4. data/README +443 -0
  5. data/Rakefile +334 -0
  6. data/bin/oss +6 -0
  7. data/bin/setup.rb +11 -0
  8. data/lib/aliyun/oss.rb +55 -0
  9. data/lib/aliyun/oss/acl.rb +132 -0
  10. data/lib/aliyun/oss/authentication.rb +222 -0
  11. data/lib/aliyun/oss/base.rb +241 -0
  12. data/lib/aliyun/oss/bucket.rb +320 -0
  13. data/lib/aliyun/oss/connection.rb +279 -0
  14. data/lib/aliyun/oss/error.rb +70 -0
  15. data/lib/aliyun/oss/exceptions.rb +134 -0
  16. data/lib/aliyun/oss/extensions.rb +364 -0
  17. data/lib/aliyun/oss/logging.rb +304 -0
  18. data/lib/aliyun/oss/object.rb +612 -0
  19. data/lib/aliyun/oss/owner.rb +45 -0
  20. data/lib/aliyun/oss/parsing.rb +100 -0
  21. data/lib/aliyun/oss/response.rb +181 -0
  22. data/lib/aliyun/oss/service.rb +52 -0
  23. data/lib/aliyun/oss/version.rb +14 -0
  24. data/support/faster-xml-simple/lib/faster_xml_simple.rb +188 -0
  25. data/support/faster-xml-simple/test/regression_test.rb +48 -0
  26. data/support/faster-xml-simple/test/test_helper.rb +18 -0
  27. data/support/faster-xml-simple/test/xml_simple_comparison_test.rb +47 -0
  28. data/support/rdoc/code_info.rb +212 -0
  29. data/test/acl_test.rb +70 -0
  30. data/test/authentication_test.rb +114 -0
  31. data/test/base_test.rb +137 -0
  32. data/test/bucket_test.rb +75 -0
  33. data/test/connection_test.rb +218 -0
  34. data/test/error_test.rb +71 -0
  35. data/test/extensions_test.rb +346 -0
  36. data/test/fixtures.rb +90 -0
  37. data/test/fixtures/buckets.yml +133 -0
  38. data/test/fixtures/errors.yml +34 -0
  39. data/test/fixtures/headers.yml +3 -0
  40. data/test/fixtures/logging.yml +15 -0
  41. data/test/fixtures/loglines.yml +5 -0
  42. data/test/fixtures/logs.yml +7 -0
  43. data/test/fixtures/policies.yml +16 -0
  44. data/test/logging_test.rb +90 -0
  45. data/test/mocks/fake_response.rb +27 -0
  46. data/test/object_test.rb +221 -0
  47. data/test/parsing_test.rb +67 -0
  48. data/test/remote/acl_test.rb +28 -0
  49. data/test/remote/bucket_test.rb +147 -0
  50. data/test/remote/logging_test.rb +86 -0
  51. data/test/remote/object_test.rb +350 -0
  52. data/test/remote/test_file.data +0 -0
  53. data/test/remote/test_helper.rb +34 -0
  54. data/test/response_test.rb +69 -0
  55. data/test/service_test.rb +24 -0
  56. data/test/test_helper.rb +110 -0
  57. metadata +177 -0
@@ -0,0 +1,28 @@
1
+ # -*- encoding : utf-8 -*-
2
+ require File.dirname(__FILE__) + '/test_helper'
3
+
4
+ class RemoteACLTest < Test::Unit::TestCase
5
+
6
+ def setup
7
+ establish_real_connection
8
+ end
9
+
10
+ def teardown
11
+ disconnect!
12
+ end
13
+
14
+ def test_acl
15
+ Bucket.create(TEST_BUCKET)
16
+ Bucket.acl(TEST_BUCKET, :private) # Wipe out the existing bucket's ACL
17
+
18
+ bucket_policy = Bucket.acl(TEST_BUCKET)
19
+ assert_equal 'private', bucket_policy
20
+
21
+ assert_nothing_raised do
22
+ Bucket.acl(TEST_BUCKET, :public_read)
23
+ end
24
+
25
+ bucket = Bucket.find(TEST_BUCKET)
26
+ assert_equal 'public-read', bucket.acl
27
+ end
28
+ end
@@ -0,0 +1,147 @@
1
+ # -*- encoding : utf-8 -*-
2
+ require File.dirname(__FILE__) + '/test_helper'
3
+
4
+ class RemoteBucketTest < Test::Unit::TestCase
5
+
6
+ def setup
7
+ establish_real_connection
8
+ assert Bucket.find(TEST_BUCKET).delete_all
9
+ end
10
+
11
+ def teardown
12
+ disconnect!
13
+ end
14
+
15
+ def test_bucket
16
+ # Fetch the testing bucket
17
+
18
+ bucket = nil
19
+ assert_nothing_raised do
20
+ bucket = Bucket.find(TEST_BUCKET)
21
+ end
22
+
23
+ assert bucket
24
+
25
+ # Confirm we can fetch the bucket implicitly
26
+
27
+ bucket = nil
28
+ assert_nothing_raised do
29
+ bucket = TestBucket.find
30
+ end
31
+
32
+ assert bucket
33
+
34
+ # Confirm the bucket has the right name
35
+
36
+ assert_equal TEST_BUCKET, bucket.name
37
+
38
+ assert bucket.empty?
39
+ assert_equal 0, bucket.size
40
+
41
+ # Add some files to the bucket
42
+
43
+ assert_nothing_raised do
44
+ %w(a m z).each do |file_name|
45
+ OSSObject.create(file_name, file_name, bucket.name, :content_type => 'text/plain')
46
+ end
47
+ end
48
+
49
+ # Confirm that we can reload the objects
50
+
51
+ assert_nothing_raised do
52
+ bucket.objects(:reload)
53
+ end
54
+
55
+ assert !bucket.empty?
56
+ assert_equal 3, bucket.size
57
+
58
+ assert_nothing_raised do
59
+ bucket.objects(:marker => 'm')
60
+ end
61
+
62
+ assert_equal 1, bucket.size
63
+ assert bucket['z']
64
+
65
+ assert_equal 1, Bucket.find(TEST_BUCKET, :max_keys => 1).size
66
+
67
+ assert_nothing_raised do
68
+ bucket.objects(:reload)
69
+ end
70
+
71
+ assert_equal 3, bucket.size
72
+
73
+ # Ensure the reloaded buckets have been repatriated
74
+
75
+ assert_equal bucket, bucket.objects.first.bucket
76
+
77
+ # Confirm that we can delete one of the objects and it will be removed
78
+
79
+ object_to_be_deleted = bucket.objects.last
80
+ assert_nothing_raised do
81
+ object_to_be_deleted.delete
82
+ end
83
+
84
+ assert !bucket.objects.include?(object_to_be_deleted)
85
+
86
+ # Confirm that we can add an object
87
+
88
+ object = bucket.new_object(:value => 'hello')
89
+
90
+ assert_raises(NoKeySpecified) do
91
+ object.store
92
+ end
93
+
94
+ object.key = 'abc'
95
+ assert_nothing_raised do
96
+ object.store
97
+ end
98
+
99
+ assert bucket.objects.include?(object)
100
+
101
+ # Confirm that the object is still there after reloading its objects
102
+
103
+ assert_nothing_raised do
104
+ bucket.objects(:reload)
105
+ end
106
+ assert bucket.objects.include?(object)
107
+
108
+ # Check that TestBucket has the same objects fetched implicitly
109
+
110
+ assert_equal bucket.objects, TestBucket.objects
111
+
112
+ # Empty out bucket
113
+
114
+ assert_nothing_raised do
115
+ bucket.delete_all
116
+ end
117
+
118
+ assert bucket.empty?
119
+
120
+ bucket = nil
121
+ assert_nothing_raised do
122
+ bucket = Bucket.find(TEST_BUCKET)
123
+ end
124
+
125
+ assert bucket.empty?
126
+ end
127
+
128
+ def test_bucket_name_is_switched_with_options_when_bucket_is_implicit_and_options_are_passed
129
+ Object.const_set(:ImplicitlyNamedBucket, Class.new(Bucket))
130
+ ImplicitlyNamedBucket.current_bucket = TEST_BUCKET
131
+ assert ImplicitlyNamedBucket.objects.empty?
132
+
133
+ %w(a b c).each {|key| OSSObject.store(key, 'value does not matter', TEST_BUCKET)}
134
+
135
+ assert_equal 3, ImplicitlyNamedBucket.objects.size
136
+
137
+ objects = nil
138
+ assert_nothing_raised do
139
+ objects = ImplicitlyNamedBucket.objects(:max_keys => 1)
140
+ end
141
+
142
+ assert objects
143
+ assert_equal 1, objects.size
144
+ ensure
145
+ %w(a b c).each {|key| OSSObject.delete(key, TEST_BUCKET)}
146
+ end
147
+ end
@@ -0,0 +1,86 @@
1
+ # -*- encoding : utf-8 -*-
2
+ require File.dirname(__FILE__) + '/test_helper'
3
+
4
+ class RemoteLoggingTest < Test::Unit::TestCase
5
+ def setup
6
+ establish_real_connection
7
+ end
8
+
9
+ def teardown
10
+ disconnect!
11
+ end
12
+
13
+ #TODO 日记功能API不确定
14
+ =begin
15
+ def test_logging
16
+ Bucket.create(TEST_BUCKET) # Clear out any custom grants
17
+
18
+ # Confirm that logging is not enabled on the test bucket
19
+
20
+ assert !Bucket.logging_enabled_for?(TEST_BUCKET)
21
+ assert !Bucket.find(TEST_BUCKET).logging_enabled?
22
+
23
+ assert_equal [], Bucket.logs_for(TEST_BUCKET)
24
+
25
+ # Confirm the current bucket doesn't have logging grants
26
+
27
+ policy = Bucket.acl(TEST_BUCKET)
28
+ assert !policy.grants.include?(:logging_read_acp)
29
+ assert !policy.grants.include?(:logging_write)
30
+
31
+ # Confirm that we can enable logging
32
+
33
+ assert_nothing_raised do
34
+ Bucket.enable_logging_for TEST_BUCKET
35
+ end
36
+
37
+ # Confirm enabling logging worked
38
+
39
+ assert Service.response.success?
40
+
41
+ assert Bucket.logging_enabled_for?(TEST_BUCKET)
42
+ assert Bucket.find(TEST_BUCKET).logging_enabled?
43
+
44
+ # Confirm the appropriate grants were added
45
+
46
+ policy = Bucket.acl(TEST_BUCKET)
47
+ assert policy.grants.include?(:logging_read_acp)
48
+ assert policy.grants.include?(:logging_write)
49
+
50
+ # Confirm logging status used defaults
51
+
52
+ logging_status = Bucket.logging_status_for TEST_BUCKET
53
+ assert_equal TEST_BUCKET, logging_status.target_bucket
54
+ assert_equal 'log-', logging_status.target_prefix
55
+
56
+ # Confirm we can update the logging status
57
+
58
+ logging_status.target_prefix = 'access-log-'
59
+
60
+ assert_nothing_raised do
61
+ Bucket.logging_status_for TEST_BUCKET, logging_status
62
+ end
63
+
64
+ assert Service.response.success?
65
+
66
+ logging_status = Bucket.logging_status_for TEST_BUCKET
67
+ assert_equal 'access-log-', logging_status.target_prefix
68
+
69
+ # Confirm we can make a request for the bucket's logs
70
+
71
+ assert_nothing_raised do
72
+ Bucket.logs_for TEST_BUCKET
73
+ end
74
+
75
+ # Confirm we can disable logging
76
+
77
+ assert_nothing_raised do
78
+ Bucket.disable_logging_for(TEST_BUCKET)
79
+ end
80
+
81
+ assert Service.response.success?
82
+
83
+ assert !Bucket.logging_enabled_for?(TEST_BUCKET)
84
+ end
85
+ =end
86
+ end
@@ -0,0 +1,350 @@
1
+ # -*- encoding : utf-8 -*-
2
+ require File.dirname(__FILE__) + '/test_helper'
3
+
4
+ class RemoteOSSObjectTest < Test::Unit::TestCase
5
+ def setup
6
+ establish_real_connection
7
+ end
8
+
9
+ def teardown
10
+ disconnect!
11
+ end
12
+
13
+ def test_object
14
+ key = 'testing_ossobjects'
15
+ value = 'testing'
16
+ content_type = 'text/plain'
17
+ unauthenticated_url = ['http:/', Base.connection.http.address, TEST_BUCKET, key].join('/')
18
+
19
+ # Create an object
20
+
21
+ response = nil
22
+ assert_nothing_raised do
23
+ response = OSSObject.create(key, value, TEST_BUCKET, :content_type => content_type)
24
+ end
25
+
26
+ # Check response
27
+
28
+ assert response.success?
29
+
30
+ # Extract the object's etag
31
+
32
+ etag = nil
33
+ assert_nothing_raised do
34
+ etag = response.etag
35
+ end
36
+
37
+ assert etag
38
+
39
+ # Confirm we can't create an object unless the bucket is set
40
+
41
+ assert_raises(NoBucketSpecified) do
42
+ object = OSSObject.new
43
+ object.key = 'hello'
44
+ object.store
45
+ end
46
+
47
+ # Fetch newly created object to show it was actually created
48
+
49
+ object = nil
50
+ assert_nothing_raised do
51
+ object = OSSObject.find(key, TEST_BUCKET)
52
+ end
53
+
54
+ assert object
55
+
56
+ # Confirm it has the right etag
57
+
58
+ assert_equal etag, object.etag
59
+
60
+ # Check if its owner is properly set
61
+
62
+ assert_nothing_raised do
63
+ object.owner.display_name
64
+ end
65
+
66
+ # Confirm we can get the object's key
67
+
68
+ assert_equal key, object.key
69
+
70
+ # Confirm its value was properly set
71
+
72
+ assert_equal value, object.value
73
+ assert_equal value, OSSObject.value(key, TEST_BUCKET)
74
+ streamed_value = ''
75
+ assert_nothing_raised do
76
+ OSSObject.stream(key, TEST_BUCKET) do |segment|
77
+ streamed_value << segment
78
+ end
79
+ end
80
+
81
+ assert_equal value, streamed_value
82
+
83
+ # Change its value
84
+
85
+ new_value = "<script>alert('foo');</script>"
86
+ assert_nothing_raised do
87
+ object.value = new_value
88
+ end
89
+ assert_equal new_value, object.value
90
+
91
+ # Confirm content type was properly set
92
+
93
+ assert_equal content_type, object.content_type
94
+
95
+ # Change its content type
96
+
97
+ new_content_type = 'text/javascript'
98
+ assert_nothing_raised do
99
+ object.content_type = new_content_type
100
+ end
101
+
102
+ assert_equal new_content_type, object.content_type
103
+
104
+ # Test that it is publicly readable
105
+
106
+ Bucket.acl(TEST_BUCKET, :public_read)
107
+ response = fetch_object_at(unauthenticated_url)
108
+ assert (200..299).include?(response.code.to_i)
109
+
110
+ # Confirm that it has no meta data
111
+
112
+ assert object.metadata.empty?
113
+
114
+ # Set some meta data
115
+
116
+ metadata_key = :secret_sauce
117
+ metadata_value = "it's a secret"
118
+ object.metadata[metadata_key] = metadata_value
119
+
120
+ # Persist all changes
121
+
122
+ assert_nothing_raised do
123
+ object.store
124
+ end
125
+
126
+ # Refetch the object
127
+
128
+ key = object.key
129
+ object = nil
130
+ assert_nothing_raised do
131
+ object = OSSObject.find(key, TEST_BUCKET)
132
+ end
133
+
134
+ # Confirm all changes were persisted
135
+
136
+ assert object
137
+ assert_equal key, object.key
138
+
139
+ assert_equal new_content_type, object.content_type
140
+
141
+ assert_equal new_value, object.value
142
+ assert_equal new_value, object.value(:reload)
143
+
144
+ assert !object.metadata.empty?
145
+ assert_equal metadata_value, object.metadata[metadata_key]
146
+
147
+ # Change acl
148
+
149
+ Bucket.acl(TEST_BUCKET, :private)
150
+
151
+ # Confirm object is no longer publicly readable
152
+
153
+ response = fetch_object_at(unauthenticated_url)
154
+ assert (400..499).include?(response.code.to_i)
155
+
156
+ # Confirm object is accessible from its authenticated url
157
+
158
+ response = fetch_object_at(object.url)
159
+ assert (200..299).include?(response.code.to_i)
160
+
161
+ # Copy the object
162
+
163
+ assert_nothing_raised do
164
+ object.copy('testing_ossobjects-copy')
165
+ end
166
+
167
+ # Confirm the object is identical
168
+
169
+ copy = nil
170
+ assert_nothing_raised do
171
+ copy = OSSObject.find('testing_ossobjects-copy', TEST_BUCKET)
172
+ end
173
+
174
+ assert copy
175
+
176
+ assert_equal object.value, copy.value
177
+ assert_equal object.content_type, copy.content_type
178
+
179
+ # Delete object
180
+
181
+ assert_nothing_raised do
182
+ object.delete
183
+ end
184
+
185
+ # Confirm we can rename objects
186
+
187
+ renamed_to = copy.key + '-renamed'
188
+ renamed_value = copy.value
189
+ assert_nothing_raised do
190
+ OSSObject.rename(copy.key, renamed_to, TEST_BUCKET)
191
+ end
192
+
193
+ # Confirm renamed copy exists
194
+
195
+ renamed = nil
196
+ assert_nothing_raised do
197
+ renamed = OSSObject.find(renamed_to, TEST_BUCKET)
198
+ end
199
+
200
+ assert renamed
201
+ assert_equal renamed_value, renamed.value
202
+
203
+ # Confirm copy is deleted
204
+
205
+ assert_raises(NoSuchKey) do
206
+ OSSObject.find(copy.key, TEST_BUCKET)
207
+ end
208
+
209
+ # Confirm that you can not store an object once it is deleted
210
+
211
+ assert_raises(DeletedObject) do
212
+ object.store
213
+ end
214
+
215
+ assert_raises(NoSuchKey) do
216
+ OSSObject.find(key, TEST_BUCKET)
217
+ end
218
+
219
+ # Confirm we can pass in an IO stream and have the uploading sent in chunks
220
+
221
+ response = nil
222
+ test_file_key = File.basename(TEST_FILE)
223
+ assert_nothing_raised do
224
+ response = OSSObject.store(test_file_key, open(TEST_FILE), TEST_BUCKET)
225
+ end
226
+ assert response.success?
227
+
228
+ assert_equal File.size(TEST_FILE), Integer(OSSObject.about(test_file_key, TEST_BUCKET)['content-length'])
229
+
230
+ result = nil
231
+ assert_nothing_raised do
232
+ result = OSSObject.delete(test_file_key, TEST_BUCKET)
233
+ end
234
+
235
+ assert result
236
+ end
237
+
238
+ def test_content_type_inference
239
+ # Confirm appropriate content type is inferred when not specified
240
+
241
+ content_type_objects = {'foo.jpg' => 'image/jpeg', 'no-extension-specified' => 'binary/octet-stream', 'foo.txt' => 'text/plain'}
242
+ content_type_objects.each_key do |key|
243
+ OSSObject.store(key, 'fake data', TEST_BUCKET) # No content type explicitly set
244
+ end
245
+
246
+ content_type_objects.each do |key, content_type|
247
+ assert_equal content_type, OSSObject.about(key, TEST_BUCKET)['content-type']
248
+ end
249
+
250
+ # Confirm we can update the content type
251
+
252
+ assert_nothing_raised do
253
+ object = OSSObject.find('no-extension-specified', TEST_BUCKET)
254
+ object.content_type = 'application/pdf'
255
+ object.store
256
+ end
257
+
258
+ assert_equal 'application/pdf', OSSObject.about('no-extension-specified', TEST_BUCKET)['content-type']
259
+
260
+ ensure
261
+ # Get rid of objects we just created
262
+ content_type_objects.each_key {|key| OSSObject.delete(key, TEST_BUCKET) }
263
+ end
264
+
265
+ def test_body_can_be_more_than_just_string_or_io
266
+ require 'stringio'
267
+ key = 'testing-body-as-string-io'
268
+ io = StringIO.new('hello there')
269
+ OSSObject.store(key, io, TEST_BUCKET)
270
+ assert_equal 'hello there', OSSObject.value(key, TEST_BUCKET)
271
+ ensure
272
+ OSSObject.delete(key, TEST_BUCKET)
273
+ end
274
+
275
+ def test_fetching_information_about_an_object_that_does_not_exist_raises_no_such_key
276
+ assert_raises(NoSuchKey) do
277
+ OSSObject.about('asdfasdfasdfas-this-does-not-exist', TEST_BUCKET)
278
+ end
279
+ end
280
+
281
+ # Regression test for http://developer.aliyunwebservices.com/connect/thread.jspa?messageID=49152&tstart=0#49152
282
+ def test_finding_an_object_with_slashes_in_its_name_does_not_escape_the_slash
283
+ OSSObject.store('rails/1', 'value does not matter', TEST_BUCKET)
284
+ OSSObject.store('rails/1.html', 'value does not matter', TEST_BUCKET)
285
+
286
+ object = nil
287
+ assert_nothing_raised do
288
+ object = OSSObject.find('rails/1.html', TEST_BUCKET)
289
+ end
290
+
291
+ assert_equal 'rails/1.html', object.key
292
+ ensure
293
+ %w(rails/1 rails/1.html).each {|key| OSSObject.delete(key, TEST_BUCKET)}
294
+ end
295
+
296
+ def test_finding_an_object_with_spaces_in_its_name
297
+ assert_nothing_raised do
298
+ OSSObject.store('name with spaces', 'value does not matter', TEST_BUCKET)
299
+ end
300
+
301
+ object = nil
302
+ assert_nothing_raised do
303
+ object = OSSObject.find('name with spaces', TEST_BUCKET)
304
+ end
305
+
306
+ assert object
307
+ assert_equal 'name with spaces', object.key
308
+
309
+ # Confirm authenticated url is generated correctly despite space in file name
310
+
311
+ response = fetch_object_at(object.url)
312
+ assert (200..299).include?(response.code.to_i)
313
+
314
+ ensure
315
+ OSSObject.delete('name with spaces', TEST_BUCKET)
316
+ end
317
+
318
+ def test_handling_a_path_that_is_not_valid_utf8
319
+ key = "318597/620065/GTL_75\24300_A600_A610.zip"
320
+ assert_nothing_raised do
321
+ OSSObject.store(key, 'value does not matter', TEST_BUCKET)
322
+ end
323
+
324
+ object = nil
325
+ assert_nothing_raised do
326
+ object = OSSObject.find(key, TEST_BUCKET)
327
+ end
328
+
329
+ assert object
330
+
331
+ url = nil
332
+ assert_nothing_raised do
333
+ url = OSSObject.url_for(key, TEST_BUCKET)
334
+ end
335
+
336
+ assert url
337
+
338
+ assert_equal object.value, fetch_object_at(url).body
339
+ ensure
340
+ assert_nothing_raised do
341
+ OSSObject.delete(key, TEST_BUCKET)
342
+ end
343
+ end
344
+
345
+ private
346
+ def fetch_object_at(url)
347
+ Net::HTTP.get_response(URI.parse(url))
348
+ end
349
+
350
+ end