aliyun-oss-ex 0.7.0.1402831795
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/COPYING +19 -0
- data/INSTALL +35 -0
- data/README +443 -0
- data/Rakefile +334 -0
- data/bin/oss +6 -0
- data/bin/setup.rb +11 -0
- data/lib/aliyun/oss.rb +55 -0
- data/lib/aliyun/oss/acl.rb +132 -0
- data/lib/aliyun/oss/authentication.rb +222 -0
- data/lib/aliyun/oss/base.rb +241 -0
- data/lib/aliyun/oss/bucket.rb +320 -0
- data/lib/aliyun/oss/connection.rb +279 -0
- data/lib/aliyun/oss/error.rb +70 -0
- data/lib/aliyun/oss/exceptions.rb +134 -0
- data/lib/aliyun/oss/extensions.rb +405 -0
- data/lib/aliyun/oss/logging.rb +304 -0
- data/lib/aliyun/oss/object.rb +612 -0
- data/lib/aliyun/oss/owner.rb +45 -0
- data/lib/aliyun/oss/parsing.rb +100 -0
- data/lib/aliyun/oss/response.rb +181 -0
- data/lib/aliyun/oss/service.rb +52 -0
- data/lib/aliyun/oss/version.rb +14 -0
- data/support/faster-xml-simple/lib/faster_xml_simple.rb +188 -0
- data/support/faster-xml-simple/test/regression_test.rb +48 -0
- data/support/faster-xml-simple/test/test_helper.rb +18 -0
- data/support/faster-xml-simple/test/xml_simple_comparison_test.rb +47 -0
- data/support/rdoc/code_info.rb +212 -0
- data/test/acl_test.rb +70 -0
- data/test/authentication_test.rb +114 -0
- data/test/base_test.rb +137 -0
- data/test/bucket_test.rb +75 -0
- data/test/connection_test.rb +218 -0
- data/test/error_test.rb +71 -0
- data/test/extensions_test.rb +346 -0
- data/test/fixtures.rb +90 -0
- data/test/fixtures/buckets.yml +133 -0
- data/test/fixtures/errors.yml +34 -0
- data/test/fixtures/headers.yml +3 -0
- data/test/fixtures/logging.yml +15 -0
- data/test/fixtures/loglines.yml +5 -0
- data/test/fixtures/logs.yml +7 -0
- data/test/fixtures/policies.yml +16 -0
- data/test/logging_test.rb +90 -0
- data/test/mocks/fake_response.rb +27 -0
- data/test/object_test.rb +221 -0
- data/test/parsing_test.rb +67 -0
- data/test/remote/acl_test.rb +28 -0
- data/test/remote/bucket_test.rb +147 -0
- data/test/remote/logging_test.rb +86 -0
- data/test/remote/object_test.rb +350 -0
- data/test/remote/test_file.data +0 -0
- data/test/remote/test_helper.rb +34 -0
- data/test/response_test.rb +69 -0
- data/test/service_test.rb +24 -0
- data/test/test_helper.rb +110 -0
- metadata +177 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 461c8b3f81b80081ecd2c774624fa928415e3a28
|
4
|
+
data.tar.gz: 61e8f65693ba1dbb85cb9fd8c879b807d51dccf6
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: ea543a1430e644b24f321662f9fb8e552919240c18ad9dc2e42cab6e64c8a77d3843bcc734c9d9258ceaa79ff68900b1366a19ff629efa059d9f54c5943535ef
|
7
|
+
data.tar.gz: 86677b96c9a21a0d852d0623b4721612316b0ff229e4f724bbd77ac090cab20354f75ee8fb7b2c27421779d4119653b18a23a1afbc5772a8e78cf96f21fc0365
|
data/COPYING
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
#
|
2
|
+
# Copyright (c) 2006-2009 Marcel Molina Jr. <marcel@vernix.org>
|
3
|
+
#
|
4
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy of
|
5
|
+
# this software and associated documentation files (the "Software"), to deal in the
|
6
|
+
# Software without restriction, including without limitation the rights to use,
|
7
|
+
# copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
|
8
|
+
# Software, and to permit persons to whom the Software is furnished to do so,
|
9
|
+
# subject to the following conditions:
|
10
|
+
#
|
11
|
+
# The above copyright notice and this permission notice shall be included in all
|
12
|
+
# copies or substantial portions of the Software.
|
13
|
+
#
|
14
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
15
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
16
|
+
# FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
17
|
+
# COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
|
18
|
+
# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
19
|
+
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/INSTALL
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
== Rubygems
|
2
|
+
|
3
|
+
The easiest way to install aliyun/oss is with Rubygems:
|
4
|
+
|
5
|
+
% gem i aliyun-oss -ry
|
6
|
+
|
7
|
+
== Directly from git
|
8
|
+
|
9
|
+
% git clone git://github.com/mangege/aliyun-oss-sdk-for-ruby.git
|
10
|
+
|
11
|
+
== Dependencies
|
12
|
+
|
13
|
+
Aliyun::OSS requires Ruby 1.8.4 or greater.
|
14
|
+
|
15
|
+
It also has the following dependencies:
|
16
|
+
|
17
|
+
gem i xml-simple -ry
|
18
|
+
gem i builder -ry
|
19
|
+
gem i mime-types -ry
|
20
|
+
|
21
|
+
=== XML parsing (xml-simple)
|
22
|
+
|
23
|
+
Aliyun::OSS depends on XmlSimple (http://xml-simple.rubyforge.org/). When installing aliyun/oss with
|
24
|
+
Rubygems, this dependency will be taken care of for you. Otherwise, installation instructions are listed on the xml-simple
|
25
|
+
site.
|
26
|
+
|
27
|
+
If your system has the Ruby libxml bindings installed (http://libxml.rubyforge.org/) they will be used instead of REXML (which is what XmlSimple uses). For those concerned with speed and efficiency, it would behoove you to install libxml (instructions here: http://libxml.rubyforge.org/install.html) as it is considerably faster and less expensive than REXML.
|
28
|
+
|
29
|
+
=== XML generation (builder)
|
30
|
+
|
31
|
+
Aliyun::OSS also depends on the Builder library (http://builder.rubyforge.org/ and http://rubyforge.org/projects/builder/). This will also automatically be installed for you when using Rubygems.
|
32
|
+
|
33
|
+
=== Content type inference (mime-types)
|
34
|
+
|
35
|
+
Aliyun::OSS depends on the MIME::Types library (http://mime-types.rubyforge.org/) to infer the content type of an object that does not explicitly specify it. This library will automatically be installed for you when using Rubygems.
|
data/README
ADDED
@@ -0,0 +1,443 @@
|
|
1
|
+
= Aliyun::OSS
|
2
|
+
|
3
|
+
Aliyun::OSS is a Ruby library for Aliyun's Simple Storage Service's REST API (http://www.aliyun.com/product/oss).
|
4
|
+
Full documentation of the currently supported API can be found at http://www.aliyun.com/product/oss#resources.
|
5
|
+
|
6
|
+
== Getting started
|
7
|
+
|
8
|
+
To get started you need to require 'aliyun/oss':
|
9
|
+
|
10
|
+
% irb -rubygems
|
11
|
+
irb(main):001:0> require 'aliyun/oss'
|
12
|
+
# => true
|
13
|
+
|
14
|
+
The Aliyun::OSS library ships with an interactive shell called <tt>oss</tt>. From within it, you have access to all the operations the library exposes from the command line.
|
15
|
+
|
16
|
+
% oss
|
17
|
+
>> Version
|
18
|
+
|
19
|
+
Before you can do anything, you must establish a connection using Base.establish_connection!. A basic connection would look something like this:
|
20
|
+
|
21
|
+
Aliyun::OSS::Base.establish_connection!(
|
22
|
+
:access_key_id => 'abc',
|
23
|
+
:secret_access_key => '123'
|
24
|
+
)
|
25
|
+
|
26
|
+
The minimum connection options that you must specify are your access key id and your secret access key.
|
27
|
+
|
28
|
+
(If you don't already have your access keys, all you need to sign up for the OSS service is an account at Aliyun. You can sign up for OSS and get access keys by visiting http://aliyun.aliyun.com/oss.)
|
29
|
+
|
30
|
+
For convenience, if you set two special environment variables with the value of your access keys, the console will automatically create a default connection for you. For example:
|
31
|
+
|
32
|
+
% cat .oss_keys
|
33
|
+
export OSS_ACCESS_KEY_ID='abcdefghijklmnop'
|
34
|
+
export OSS_SECRET_ACCESS_KEY='1234567891012345'
|
35
|
+
|
36
|
+
Then load it in your shell's rc file.
|
37
|
+
|
38
|
+
% cat .zshrc
|
39
|
+
if [[ -f "$HOME/.oss_keys" ]]; then
|
40
|
+
source "$HOME/.oss_keys";
|
41
|
+
fi
|
42
|
+
|
43
|
+
See more connection details at Aliyun::OSS::Connection::Management::ClassMethods.
|
44
|
+
|
45
|
+
|
46
|
+
== Aliyun::OSS Basics
|
47
|
+
=== The service, buckets and objects
|
48
|
+
|
49
|
+
The three main concepts of OSS are the service, buckets and objects.
|
50
|
+
|
51
|
+
==== The service
|
52
|
+
|
53
|
+
The service lets you find out general information about your account, like what buckets you have.
|
54
|
+
|
55
|
+
Service.buckets
|
56
|
+
# => []
|
57
|
+
|
58
|
+
|
59
|
+
==== Buckets
|
60
|
+
|
61
|
+
Buckets are containers for objects (the files you store on OSS). To create a new bucket you just specify its name.
|
62
|
+
|
63
|
+
# Pick a unique name, or else you'll get an error
|
64
|
+
# if the name is already taken.
|
65
|
+
Bucket.create('jukebox')
|
66
|
+
|
67
|
+
Bucket names must be unique across the entire OSS system, sort of like domain names across the internet. If you try
|
68
|
+
to create a bucket with a name that is already taken, you will get an error.
|
69
|
+
|
70
|
+
Assuming the name you chose isn't already taken, your new bucket will now appear in the bucket list:
|
71
|
+
|
72
|
+
Service.buckets
|
73
|
+
# => [#<Aliyun::OSS::Bucket @attributes={"name"=>"jukebox"}>]
|
74
|
+
|
75
|
+
Once you have succesfully created a bucket you can fetch it by name using Bucket.find.
|
76
|
+
|
77
|
+
music_bucket = Bucket.find('jukebox')
|
78
|
+
|
79
|
+
The bucket that is returned will contain a listing of all the objects in the bucket.
|
80
|
+
|
81
|
+
music_bucket.objects.size
|
82
|
+
# => 0
|
83
|
+
|
84
|
+
If all you are interested in is the objects of the bucket, you can get to them directly using Bucket.objects.
|
85
|
+
|
86
|
+
Bucket.objects('jukebox').size
|
87
|
+
# => 0
|
88
|
+
|
89
|
+
By default all objects will be returned, though there are several options you can use to limit what is returned, such as
|
90
|
+
specifying that only objects whose name is after a certain place in the alphabet be returned, and etc. Details about these options can
|
91
|
+
be found in the documentation for Bucket.find.
|
92
|
+
|
93
|
+
To add an object to a bucket you specify the name of the object, its value, and the bucket to put it in.
|
94
|
+
|
95
|
+
file = 'black-flowers.mp3'
|
96
|
+
OSSObject.store(file, open(file), 'jukebox')
|
97
|
+
|
98
|
+
You'll see your file has been added to it:
|
99
|
+
|
100
|
+
music_bucket.objects
|
101
|
+
# => [#<Aliyun::OSS::OSSObject '/jukebox/black-flowers.mp3'>]
|
102
|
+
|
103
|
+
You can treat your bucket like a hash and access objects by name:
|
104
|
+
|
105
|
+
jukebox['black-flowers.mp3']
|
106
|
+
# => #<Aliyun::OSS::OSSObject '/jukebox/black-flowers.mp3'>
|
107
|
+
|
108
|
+
In the event that you want to delete a bucket, you can use Bucket.delete.
|
109
|
+
|
110
|
+
Bucket.delete('jukebox')
|
111
|
+
|
112
|
+
Keep in mind, like unix directories, you can not delete a bucket unless it is empty. Trying to delete a bucket
|
113
|
+
that contains objects will raise a BucketNotEmpty exception.
|
114
|
+
|
115
|
+
Passing the :force => true option to delete will take care of deleting all the bucket's objects for you.
|
116
|
+
|
117
|
+
Bucket.delete('photos', :force => true)
|
118
|
+
# => true
|
119
|
+
|
120
|
+
|
121
|
+
==== Objects
|
122
|
+
|
123
|
+
OSSObjects represent the data you store on OSS. They have a key (their name) and a value (their data). All objects belong to a
|
124
|
+
bucket.
|
125
|
+
|
126
|
+
You can store an object on OSS by specifying a key, its data and the name of the bucket you want to put it in:
|
127
|
+
|
128
|
+
OSSObject.store('me.jpg', open('headshot.jpg'), 'photos')
|
129
|
+
|
130
|
+
The content type of the object will be inferred by its extension. If the appropriate content type can not be inferred, OSS defaults
|
131
|
+
to <tt>binary/octet-stream</tt>.
|
132
|
+
|
133
|
+
If you want to override this, you can explicitly indicate what content type the object should have with the <tt>:content_type</tt> option:
|
134
|
+
|
135
|
+
file = 'black-flowers.m4a'
|
136
|
+
OSSObject.store(
|
137
|
+
file,
|
138
|
+
open(file),
|
139
|
+
'jukebox',
|
140
|
+
:content_type => 'audio/mp4a-latm'
|
141
|
+
)
|
142
|
+
|
143
|
+
You can read more about storing files on OSS in the documentation for OSSObject.store.
|
144
|
+
|
145
|
+
If you just want to fetch an object you've stored on OSS, you just specify its name and its bucket:
|
146
|
+
|
147
|
+
picture = OSSObject.find 'headshot.jpg', 'photos'
|
148
|
+
|
149
|
+
N.B. The actual data for the file is not downloaded in both the example where the file appeared in the bucket and when fetched directly.
|
150
|
+
You get the data for the file like this:
|
151
|
+
|
152
|
+
picture.value
|
153
|
+
|
154
|
+
You can fetch just the object's data directly:
|
155
|
+
|
156
|
+
OSSObject.value 'headshot.jpg', 'photos'
|
157
|
+
|
158
|
+
Or stream it by passing a block to <tt>stream</tt>:
|
159
|
+
|
160
|
+
open('song.mp3', 'w') do |file|
|
161
|
+
OSSObject.stream('song.mp3', 'jukebox') do |chunk|
|
162
|
+
file.write chunk
|
163
|
+
end
|
164
|
+
end
|
165
|
+
|
166
|
+
The data of the file, once download, is cached, so subsequent calls to <tt>value</tt> won't redownload the file unless you
|
167
|
+
tell the object to reload its <tt>value</tt>:
|
168
|
+
|
169
|
+
# Redownloads the file's data
|
170
|
+
song.value(:reload)
|
171
|
+
|
172
|
+
Other functionality includes:
|
173
|
+
|
174
|
+
# Check if an object exists?
|
175
|
+
OSSObject.exists? 'headshot.jpg', 'photos'
|
176
|
+
|
177
|
+
# Copying an object
|
178
|
+
OSSObject.copy 'headshot.jpg', 'headshot2.jpg', 'photos'
|
179
|
+
|
180
|
+
# Renaming an object
|
181
|
+
OSSObject.rename 'headshot.jpg', 'portrait.jpg', 'photos'
|
182
|
+
|
183
|
+
# Deleting an object
|
184
|
+
OSSObject.delete 'headshot.jpg', 'photos'
|
185
|
+
|
186
|
+
==== More about objects and their metadata
|
187
|
+
|
188
|
+
You can find out the content type of your object with the <tt>content_type</tt> method:
|
189
|
+
|
190
|
+
song.content_type
|
191
|
+
# => "audio/mpeg"
|
192
|
+
|
193
|
+
You can change the content type as well if you like:
|
194
|
+
|
195
|
+
song.content_type = 'application/pdf'
|
196
|
+
song.store
|
197
|
+
|
198
|
+
(Keep in mind that due to limitiations in OSS's exposed API, the only way to change things like the content_type
|
199
|
+
is to PUT the object onto OSS again. In the case of large files, this will result in fully re-uploading the file.)
|
200
|
+
|
201
|
+
A bevie of information about an object can be had using the <tt>about</tt> method:
|
202
|
+
|
203
|
+
pp song.about
|
204
|
+
{"last-modified" => "Sat, 28 Oct 2006 21:29:26 GMT",
|
205
|
+
"content-type" => "binary/octet-stream",
|
206
|
+
"etag" => "\"dc629038ffc674bee6f62eb64ff3a\"",
|
207
|
+
"date" => "Sat, 28 Oct 2006 21:30:41 GMT",
|
208
|
+
"x-amz-request-id" => "B7BC68F55495B1C8",
|
209
|
+
"server" => "AliyunOSS",
|
210
|
+
"content-length" => "3418766"}
|
211
|
+
|
212
|
+
You can get and set metadata for an object:
|
213
|
+
|
214
|
+
song.metadata
|
215
|
+
# => {}
|
216
|
+
song.metadata[:album] = "A River Ain't Too Much To Love"
|
217
|
+
# => "A River Ain't Too Much To Love"
|
218
|
+
song.metadata[:released] = 2005
|
219
|
+
pp song.metadata
|
220
|
+
{"x-amz-meta-released" => 2005,
|
221
|
+
"x-amz-meta-album" => "A River Ain't Too Much To Love"}
|
222
|
+
song.store
|
223
|
+
|
224
|
+
That metadata will be saved in OSS and is hence forth available from that object:
|
225
|
+
|
226
|
+
song = OSSObject.find('black-flowers.mp3', 'jukebox')
|
227
|
+
pp song.metadata
|
228
|
+
{"x-amz-meta-released" => "2005",
|
229
|
+
"x-amz-meta-album" => "A River Ain't Too Much To Love"}
|
230
|
+
song.metadata[:released]
|
231
|
+
# => "2005"
|
232
|
+
song.metadata[:released] = 2006
|
233
|
+
pp song.metadata
|
234
|
+
{"x-amz-meta-released" => 2006,
|
235
|
+
"x-amz-meta-album" => "A River Ain't Too Much To Love"}
|
236
|
+
|
237
|
+
|
238
|
+
==== Streaming uploads
|
239
|
+
|
240
|
+
When storing an object on the OSS servers using OSSObject.store, the <tt>data</tt> argument can be a string or an I/O stream.
|
241
|
+
If <tt>data</tt> is an I/O stream it will be read in segments and written to the socket incrementally. This approach
|
242
|
+
may be desirable for very large files so they are not read into memory all at once.
|
243
|
+
|
244
|
+
# Non streamed upload
|
245
|
+
OSSObject.store('greeting.txt', 'hello world!', 'marcel')
|
246
|
+
|
247
|
+
# Streamed upload
|
248
|
+
OSSObject.store('roots.mpeg', open('roots.mpeg'), 'marcel')
|
249
|
+
|
250
|
+
|
251
|
+
== Setting the current bucket
|
252
|
+
==== Scoping operations to a specific bucket
|
253
|
+
|
254
|
+
If you plan on always using a specific bucket for certain files, you can skip always having to specify the bucket by creating
|
255
|
+
a subclass of Bucket or OSSObject and telling it what bucket to use:
|
256
|
+
|
257
|
+
class JukeBoxSong < Aliyun::OSS::OSSObject
|
258
|
+
set_current_bucket_to 'jukebox'
|
259
|
+
end
|
260
|
+
|
261
|
+
For all methods that take a bucket name as an argument, the current bucket will be used if the bucket name argument is omitted.
|
262
|
+
|
263
|
+
other_song = 'baby-please-come-home.mp3'
|
264
|
+
JukeBoxSong.store(other_song, open(other_song))
|
265
|
+
|
266
|
+
This time we didn't have to explicitly pass in the bucket name, as the JukeBoxSong class knows that it will
|
267
|
+
always use the 'jukebox' bucket.
|
268
|
+
|
269
|
+
"Astute readers", as they say, may have noticed that we used the third parameter to pass in the content type,
|
270
|
+
rather than the fourth parameter as we had the last time we created an object. If the bucket can be inferred, or
|
271
|
+
is explicitly set, as we've done in the JukeBoxSong class, then the third argument can be used to pass in
|
272
|
+
options.
|
273
|
+
|
274
|
+
Now all operations that would have required a bucket name no longer do.
|
275
|
+
|
276
|
+
other_song = JukeBoxSong.find('baby-please-come-home.mp3')
|
277
|
+
|
278
|
+
|
279
|
+
== Access control
|
280
|
+
==== Using canned access control policies
|
281
|
+
|
282
|
+
By default buckets are private. This means that only the owner has access rights to the bucket and its objects.
|
283
|
+
Objects in that bucket inherit the permission of the bucket unless otherwise specified. When an object is private, the owner can
|
284
|
+
generate a signed url that exposes the object to anyone who has that url. Alternatively, buckets and objects can be given other
|
285
|
+
access levels. Several canned access levels are defined:
|
286
|
+
|
287
|
+
* <tt>:private</tt> - Owner gets FULL_CONTROL. No one else has any access rights. This is the default.
|
288
|
+
* <tt>:public_read</tt> - Owner gets FULL_CONTROL and the anonymous principal is granted READ access. If this policy is used on an object, it can be read from a browser with no authentication.
|
289
|
+
* <tt>:public_read_write</tt> - Owner gets FULL_CONTROL, the anonymous principal is granted READ and WRITE access. This is a useful policy to apply to a bucket, if you intend for any anonymous user to PUT objects into the bucket.
|
290
|
+
* <tt>:authenticated_read</tt> - Owner gets FULL_CONTROL, and any principal authenticated as a registered Aliyun OSS user is granted READ access.
|
291
|
+
|
292
|
+
You can set a canned access level when you create a bucket or an object by using the <tt>:access</tt> option:
|
293
|
+
|
294
|
+
OSSObject.store(
|
295
|
+
'kiss.jpg',
|
296
|
+
data,
|
297
|
+
'marcel',
|
298
|
+
:access => :public_read
|
299
|
+
)
|
300
|
+
|
301
|
+
Since the image we created is publicly readable, we can access it directly from a browser by going to the corresponding bucket name
|
302
|
+
and specifying the object's key without a special authenticated url:
|
303
|
+
|
304
|
+
http://oss.aliyuncs.com/marcel/kiss.jpg
|
305
|
+
|
306
|
+
|
307
|
+
==== Accessing private objects from a browser
|
308
|
+
|
309
|
+
All private objects are accessible via an authenticated GET request to the OSS servers. You can generate an
|
310
|
+
authenticated url for an object like this:
|
311
|
+
|
312
|
+
OSSObject.url_for('beluga_baby.jpg', 'marcel_molina')
|
313
|
+
|
314
|
+
By default authenticated urls expire 5 minutes after they were generated.
|
315
|
+
|
316
|
+
Expiration options can be specified either with an absolute time since the epoch with the <tt>:expires</tt> options,
|
317
|
+
or with a number of seconds relative to now with the <tt>:expires_in</tt> options:
|
318
|
+
|
319
|
+
# Absolute expiration date
|
320
|
+
# (Expires January 18th, 2038)
|
321
|
+
doomsday = Time.mktime(2038, 1, 18).to_i
|
322
|
+
OSSObject.url_for('beluga_baby.jpg',
|
323
|
+
'marcel',
|
324
|
+
:expires => doomsday)
|
325
|
+
|
326
|
+
# Expiration relative to now specified in seconds
|
327
|
+
# (Expires in 3 hours)
|
328
|
+
OSSObject.url_for('beluga_baby.jpg',
|
329
|
+
'marcel',
|
330
|
+
:expires_in => 60 * 60 * 3)
|
331
|
+
|
332
|
+
You can specify whether the url should go over SSL with the <tt>:use_ssl</tt> option:
|
333
|
+
|
334
|
+
# Url will use https protocol
|
335
|
+
OSSObject.url_for('beluga_baby.jpg',
|
336
|
+
'marcel',
|
337
|
+
:use_ssl => true)
|
338
|
+
|
339
|
+
By default, the ssl settings for the current connection will be used.
|
340
|
+
|
341
|
+
If you have an object handy, you can use its <tt>url</tt> method with the same objects:
|
342
|
+
|
343
|
+
song.url(:expires_in => 30)
|
344
|
+
|
345
|
+
To get an unauthenticated url for the object, such as in the case
|
346
|
+
when the object is publicly readable, pass the
|
347
|
+
<tt>:authenticated</tt> option with a value of <tt>false</tt>.
|
348
|
+
|
349
|
+
OSSObject.url_for('beluga_baby.jpg',
|
350
|
+
'marcel',
|
351
|
+
:authenticated => false)
|
352
|
+
# => http://oss.aliyuncs.com/marcel/beluga_baby.jpg
|
353
|
+
|
354
|
+
|
355
|
+
== Logging
|
356
|
+
==== Tracking requests made on a bucket
|
357
|
+
|
358
|
+
A bucket can be set to log the requests made on it. By default logging is turned off. You can check if a bucket has logging enabled:
|
359
|
+
|
360
|
+
Bucket.logging_enabled_for? 'jukebox'
|
361
|
+
# => false
|
362
|
+
|
363
|
+
Enabling it is easy:
|
364
|
+
|
365
|
+
Bucket.enable_logging_for('jukebox')
|
366
|
+
|
367
|
+
Unless you specify otherwise, logs will be written to the bucket you want to log. The logs are just like any other object. By default they will start with the prefix 'log-'. You can customize what bucket you want the logs to be delivered to, as well as customize what the log objects' key is prefixed with by setting the <tt>target_bucket</tt> and <tt>target_prefix</tt> option:
|
368
|
+
|
369
|
+
Bucket.enable_logging_for(
|
370
|
+
'jukebox', 'target_bucket' => 'jukebox-logs'
|
371
|
+
)
|
372
|
+
|
373
|
+
Now instead of logging right into the jukebox bucket, the logs will go into the bucket called jukebox-logs.
|
374
|
+
|
375
|
+
Once logs have accumulated, you can access them using the <tt>logs</tt> method:
|
376
|
+
|
377
|
+
pp Bucket.logs('jukebox')
|
378
|
+
[#<Aliyun::OSS::Logging::Log '/jukebox-logs/log-2006-11-14-07-15-24-2061C35880A310A1'>,
|
379
|
+
#<Aliyun::OSS::Logging::Log '/jukebox-logs/log-2006-11-14-08-15-27-D8EEF536EC09E6B3'>,
|
380
|
+
#<Aliyun::OSS::Logging::Log '/jukebox-logs/log-2006-11-14-08-15-29-355812B2B15BD789'>]
|
381
|
+
|
382
|
+
Each log has a <tt>lines</tt> method that gives you information about each request in that log. All the fields are available
|
383
|
+
as named methods. More information is available in Logging::Log::Line.
|
384
|
+
|
385
|
+
logs = Bucket.logs('jukebox')
|
386
|
+
log = logs.first
|
387
|
+
line = log.lines.first
|
388
|
+
line.operation
|
389
|
+
# => 'REST.GET.LOGGING_STATUS'
|
390
|
+
line.request_uri
|
391
|
+
# => 'GET /jukebox?logging HTTP/1.1'
|
392
|
+
line.remote_ip
|
393
|
+
# => "67.165.183.125"
|
394
|
+
|
395
|
+
Disabling logging is just as simple as enabling it:
|
396
|
+
|
397
|
+
Bucket.disable_logging_for('jukebox')
|
398
|
+
|
399
|
+
|
400
|
+
== Errors
|
401
|
+
==== When things go wrong
|
402
|
+
|
403
|
+
Anything you do that makes a request to OSS could result in an error. If it does, the Aliyun::OSS library will raise an exception
|
404
|
+
specific to the error. All exception that are raised as a result of a request returning an error response inherit from the
|
405
|
+
ResponseError exception. So should you choose to rescue any such exception, you can simple rescue ResponseError.
|
406
|
+
|
407
|
+
Say you go to delete a bucket, but the bucket turns out to not be empty. This results in a BucketNotEmpty error (one of the many
|
408
|
+
errors listed at http://docs.aliyunwebservices.com/AliyunOSS/2006-03-01/ErrorCodeList.html):
|
409
|
+
|
410
|
+
begin
|
411
|
+
Bucket.delete('jukebox')
|
412
|
+
rescue ResponseError => error
|
413
|
+
# ...
|
414
|
+
end
|
415
|
+
|
416
|
+
Once you've captured the exception, you can extract the error message from OSS, as well as the full error response, which includes
|
417
|
+
things like the HTTP response code:
|
418
|
+
|
419
|
+
error
|
420
|
+
# => #<Aliyun::OSS::BucketNotEmpty The bucket you tried to delete is not empty>
|
421
|
+
error.message
|
422
|
+
# => "The bucket you tried to delete is not empty"
|
423
|
+
error.response.code
|
424
|
+
# => 409
|
425
|
+
|
426
|
+
You could use this information to redisplay the error in a way you see fit, or just to log the error and continue on.
|
427
|
+
|
428
|
+
|
429
|
+
==== Accessing the last request's response
|
430
|
+
|
431
|
+
Sometimes methods that make requests to the OSS servers return some object, like a Bucket or an OSSObject.
|
432
|
+
Othertimes they return just <tt>true</tt>. Other times they raise an exception that you may want to rescue. Despite all these
|
433
|
+
possible outcomes, every method that makes a request stores its response object for you in Service.response. You can always
|
434
|
+
get to the last request's response via Service.response.
|
435
|
+
|
436
|
+
objects = Bucket.objects('jukebox')
|
437
|
+
Service.response.success?
|
438
|
+
# => true
|
439
|
+
|
440
|
+
This is also useful when an error exception is raised in the console which you weren't expecting. You can
|
441
|
+
root around in the response to get more details of what might have gone wrong.
|
442
|
+
|
443
|
+
|