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,304 @@
1
+ # -*- encoding : utf-8 -*-
2
+ module Aliyun
3
+ module OSS
4
+ # 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:
5
+ #
6
+ # Bucket.logging_enabled_for? 'jukebox'
7
+ # # => false
8
+ #
9
+ # Enabling it is easy:
10
+ #
11
+ # Bucket.enable_logging_for('jukebox')
12
+ #
13
+ # 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:
14
+ #
15
+ # Bucket.enable_logging_for(
16
+ # 'jukebox', 'target_bucket' => 'jukebox-logs'
17
+ # )
18
+ #
19
+ # Now instead of logging right into the jukebox bucket, the logs will go into the bucket called jukebox-logs.
20
+ #
21
+ # Once logs have accumulated, you can access them using the <tt>logs</tt> method:
22
+ #
23
+ # pp Bucket.logs('jukebox')
24
+ # [#<Aliyun::OSS::Logging::Log '/jukebox-logs/log-2006-11-14-07-15-24-2061C35880A310A1'>,
25
+ # #<Aliyun::OSS::Logging::Log '/jukebox-logs/log-2006-11-14-08-15-27-D8EEF536EC09E6B3'>,
26
+ # #<Aliyun::OSS::Logging::Log '/jukebox-logs/log-2006-11-14-08-15-29-355812B2B15BD789'>]
27
+ #
28
+ # Each log has a <tt>lines</tt> method that gives you information about each request in that log. All the fields are available
29
+ # as named methods. More information is available in Logging::Log::Line.
30
+ #
31
+ # logs = Bucket.logs('jukebox')
32
+ # log = logs.first
33
+ # line = log.lines.first
34
+ # line.operation
35
+ # # => 'REST.GET.LOGGING_STATUS'
36
+ # line.request_uri
37
+ # # => 'GET /jukebox?logging HTTP/1.1'
38
+ # line.remote_ip
39
+ # # => "67.165.183.125"
40
+ #
41
+ # Disabling logging is just as simple as enabling it:
42
+ #
43
+ # Bucket.disable_logging_for('jukebox')
44
+ module Logging
45
+ # Logging status captures information about the calling bucket's logging settings. If logging is enabled for the bucket
46
+ # the status object will indicate what bucket the logs are written to via the <tt>target_bucket</tt> method as well as
47
+ # the logging key prefix with via <tt>target_prefix</tt>.
48
+ #
49
+ # See the documentation for Logging::Management::ClassMethods for more information on how to get the logging status of a bucket.
50
+ class Status
51
+ include SelectiveAttributeProxy
52
+ attr_reader :enabled
53
+ alias_method :logging_enabled?, :enabled
54
+
55
+ def initialize(attributes = {}) #:nodoc:
56
+ attributes = {'target_bucket' => nil, 'target_prefix' => nil}.merge(attributes)
57
+ @enabled = attributes.has_key?('logging_enabled')
58
+ @attributes = attributes.delete('logging_enabled') || attributes
59
+ end
60
+
61
+ def to_xml #:nodoc:
62
+ Builder.new(self).to_s
63
+ end
64
+
65
+ private
66
+ attr_reader :attributes
67
+
68
+ class Builder < XmlGenerator #:nodoc:
69
+ attr_reader :logging_status
70
+ def initialize(logging_status)
71
+ @logging_status = logging_status
72
+ super()
73
+ end
74
+
75
+ def build
76
+ xml.tag!('BucketLoggingStatus', 'xmlns' => 'http://oss.aliyuncs.com/doc/2006-03-01/') do
77
+ if logging_status.target_bucket && logging_status.target_prefix
78
+ xml.LoggingEnabled do
79
+ xml.TargetBucket logging_status.target_bucket
80
+ xml.TargetPrefix logging_status.target_prefix
81
+ end
82
+ end
83
+ end
84
+ end
85
+ end
86
+ end
87
+
88
+ # A bucket log exposes requests made on the given bucket. Lines of the log represent a single request. The lines of a log
89
+ # can be accessed with the lines method.
90
+ #
91
+ # log = Bucket.logs_for('marcel').first
92
+ # log.lines
93
+ #
94
+ # More information about the logged requests can be found in the documentation for Log::Line.
95
+ class Log
96
+ def initialize(log_object) #:nodoc:
97
+ @log = log_object
98
+ end
99
+
100
+ # Returns the lines for the log. Each line is wrapped in a Log::Line.
101
+ if RUBY_VERSION >= '1.8.7'
102
+ def lines
103
+ log.value.lines.map {|line| Line.new(line)}
104
+ end
105
+ else
106
+ def lines
107
+ log.value.map {|line| Line.new(line)}
108
+ end
109
+ end
110
+ memoized :lines
111
+
112
+ def path
113
+ log.path
114
+ end
115
+
116
+ def inspect #:nodoc:
117
+ "#<%s:0x%s '%s'>" % [self.class.name, object_id, path]
118
+ end
119
+
120
+ private
121
+ attr_reader :log
122
+
123
+ # Each line of a log exposes the raw line, but it also has method accessors for all the fields of the logged request.
124
+ #
125
+ # The list of supported log line fields are listed in the OSS documentation: http://docs.aliyunwebservices.com/AliyunOSS/2006-03-01/LogFormat.html
126
+ #
127
+ # line = log.lines.first
128
+ # line.remote_ip
129
+ # # => '72.21.206.5'
130
+ #
131
+ # If a certain field does not apply to a given request (for example, the <tt>key</tt> field does not apply to a bucket request),
132
+ # or if it was unknown or unavailable, it will return <tt>nil</tt>.
133
+ #
134
+ # line.operation
135
+ # # => 'REST.GET.BUCKET'
136
+ # line.key
137
+ # # => nil
138
+ class Line < String
139
+ DATE = /\[([^\]]+)\]/
140
+ QUOTED_STRING = /"([^"]+)"/
141
+ REST = /(\S+)/
142
+ LINE_SCANNER = /#{DATE}|#{QUOTED_STRING}|#{REST}/
143
+
144
+ cattr_accessor :decorators
145
+ @@decorators = Hash.new {|hash, key| hash[key] = lambda {|entry| CoercibleString.coerce(entry)}}
146
+ cattr_reader :fields
147
+ @@fields = []
148
+
149
+ class << self
150
+ def field(name, offset, type = nil, &block) #:nodoc:
151
+ decorators[name] = block if block_given?
152
+ fields << name
153
+ class_eval(<<-EVAL, __FILE__, __LINE__)
154
+ def #{name}
155
+ value = parts[#{offset} - 1]
156
+ if value == '-'
157
+ nil
158
+ else
159
+ self.class.decorators[:#{name}].call(value)
160
+ end
161
+ end
162
+ memoized :#{name}
163
+ EVAL
164
+ end
165
+
166
+ # Time.parse doesn't like %d/%B/%Y:%H:%M:%S %z so we have to transform it unfortunately
167
+ def typecast_time(datetime) #:nodoc:
168
+ datetime.sub!(%r|^(\w{2})/(\w{3})/(\w{4})|, '\2 \1 \3')
169
+ datetime.sub!(':', ' ')
170
+ Time.parse(datetime)
171
+ end
172
+ end
173
+
174
+ def initialize(line) #:nodoc:
175
+ super(line)
176
+ @parts = parse
177
+ end
178
+
179
+ field(:owner, 1) {|entry| Owner.new('id' => entry) }
180
+ field :bucket, 2
181
+ field(:time, 3) {|entry| typecast_time(entry)}
182
+ field :remote_ip, 4
183
+ field(:requestor, 5) {|entry| Owner.new('id' => entry) }
184
+ field :request_id, 6
185
+ field :operation, 7
186
+ field :key, 8
187
+ field :request_uri, 9
188
+ field :http_status, 10
189
+ field :error_code, 11
190
+ field :bytes_sent, 12
191
+ field :object_size, 13
192
+ field :total_time, 14
193
+ field :turn_around_time, 15
194
+ field :referrer, 16
195
+ field :user_agent, 17
196
+
197
+ # Returns all fields of the line in a hash of the form <tt>:field_name => :field_value</tt>.
198
+ #
199
+ # line.attributes.values_at(:bucket, :key)
200
+ # # => ['marcel', 'kiss.jpg']
201
+ def attributes
202
+ self.class.fields.inject({}) do |attribute_hash, field|
203
+ attribute_hash[field] = send(field)
204
+ attribute_hash
205
+ end
206
+ end
207
+
208
+ private
209
+ attr_reader :parts
210
+
211
+ def parse
212
+ scan(LINE_SCANNER).flatten.compact
213
+ end
214
+ end
215
+ end
216
+
217
+ module Management #:nodoc:
218
+ def self.included(klass) #:nodoc:
219
+ klass.extend(ClassMethods)
220
+ end
221
+
222
+ module ClassMethods
223
+ # Returns the logging status for the bucket named <tt>name</tt>. From the logging status you can determine the bucket logs are delivered to
224
+ # and what the bucket object's keys are prefixed with. For more information see the Logging::Status class.
225
+ #
226
+ # Bucket.logging_status_for 'marcel'
227
+ def logging_status_for(name = nil, status = nil)
228
+ if name.is_a?(Status)
229
+ status = name
230
+ name = nil
231
+ end
232
+
233
+ path = path(name) << '?logging'
234
+ status ? put(path, {}, status.to_xml) : Status.new(get(path).parsed)
235
+ end
236
+ alias_method :logging_status, :logging_status_for
237
+
238
+ # Enables logging for the bucket named <tt>name</tt>. You can specify what bucket to log to with the <tt>'target_bucket'</tt> option as well
239
+ # as what prefix to add to the log files with the <tt>'target_prefix'</tt> option. Unless you specify otherwise, logs will be delivered to
240
+ # the same bucket that is being logged and will be prefixed with <tt>log-</tt>.
241
+ def enable_logging_for(name = nil, options = {})
242
+ name = bucket_name(name)
243
+ default_options = {'target_bucket' => name, 'target_prefix' => 'log-'}
244
+ options = default_options.merge(options)
245
+ logging_status(name, Status.new(options))
246
+ end
247
+ alias_method :enable_logging, :enable_logging_for
248
+
249
+ # Disables logging for the bucket named <tt>name</tt>.
250
+ def disable_logging_for(name = nil)
251
+ logging_status(bucket_name(name), Status.new)
252
+ end
253
+ alias_method :disable_logging, :disable_logging_for
254
+
255
+ # Returns true if logging has been enabled for the bucket named <tt>name</tt>.
256
+ def logging_enabled_for?(name = nil)
257
+ logging_status(bucket_name(name)).logging_enabled?
258
+ end
259
+ alias_method :logging_enabled?, :logging_enabled_for?
260
+
261
+ # Returns the collection of logs for the bucket named <tt>name</tt>.
262
+ #
263
+ # Bucket.logs_for 'marcel'
264
+ #
265
+ # Accepts the same options as Bucket.find, such as <tt>:max_keys</tt> and <tt>:marker</tt>.
266
+ def logs_for(name = nil, options = {})
267
+ if name.is_a?(Hash)
268
+ options = name
269
+ name = nil
270
+ end
271
+
272
+ name = bucket_name(name)
273
+ logging_status = logging_status_for(name)
274
+ return [] unless logging_status.logging_enabled?
275
+ objects(logging_status.target_bucket, options.merge(:prefix => logging_status.target_prefix)).map do |log_object|
276
+ Log.new(log_object)
277
+ end
278
+ end
279
+ alias_method :logs, :logs_for
280
+ end
281
+
282
+ def logging_status
283
+ self.class.logging_status_for(name)
284
+ end
285
+
286
+ def enable_logging(*args)
287
+ self.class.enable_logging_for(name, *args)
288
+ end
289
+
290
+ def disable_logging(*args)
291
+ self.class.disable_logging_for(name, *args)
292
+ end
293
+
294
+ def logging_enabled?
295
+ self.class.logging_enabled_for?(name)
296
+ end
297
+
298
+ def logs(options = {})
299
+ self.class.logs_for(name, options)
300
+ end
301
+ end
302
+ end
303
+ end
304
+ end
@@ -0,0 +1,612 @@
1
+ # -*- encoding : utf-8 -*-
2
+ module Aliyun
3
+ module OSS
4
+ # OSSObjects represent the data you store on OSS. They have a key (their name) and a value (their data). All objects belong to a
5
+ # bucket.
6
+ #
7
+ # 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:
8
+ #
9
+ # OSSObject.store('me.jpg', open('headshot.jpg'), 'photos')
10
+ #
11
+ # The content type of the object will be inferred by its extension. If the appropriate content type can not be inferred, OSS defaults
12
+ # to <tt>binary/octet-stream</tt>.
13
+ #
14
+ # If you want to override this, you can explicitly indicate what content type the object should have with the <tt>:content_type</tt> option:
15
+ #
16
+ # file = 'black-flowers.m4a'
17
+ # OSSObject.store(
18
+ # file,
19
+ # open(file),
20
+ # 'jukebox',
21
+ # :content_type => 'audio/mp4a-latm'
22
+ # )
23
+ #
24
+ # You can read more about storing files on OSS in the documentation for OSSObject.store.
25
+ #
26
+ # If you just want to fetch an object you've stored on OSS, you just specify its name and its bucket:
27
+ #
28
+ # picture = OSSObject.find 'headshot.jpg', 'photos'
29
+ #
30
+ # 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.
31
+ # You get the data for the file like this:
32
+ #
33
+ # picture.value
34
+ #
35
+ # You can fetch just the object's data directly:
36
+ #
37
+ # OSSObject.value 'headshot.jpg', 'photos'
38
+ #
39
+ # Or stream it by passing a block to <tt>stream</tt>:
40
+ #
41
+ # open('song.mp3', 'w') do |file|
42
+ # OSSObject.stream('song.mp3', 'jukebox') do |chunk|
43
+ # file.write chunk
44
+ # end
45
+ # end
46
+ #
47
+ # The data of the file, once download, is cached, so subsequent calls to <tt>value</tt> won't redownload the file unless you
48
+ # tell the object to reload its <tt>value</tt>:
49
+ #
50
+ # # Redownloads the file's data
51
+ # song.value(:reload)
52
+ #
53
+ # Other functionality includes:
54
+ #
55
+ # # Check if an object exists?
56
+ # OSSObject.exists? 'headshot.jpg', 'photos'
57
+ #
58
+ # # Copying an object
59
+ # OSSObject.copy 'headshot.jpg', 'headshot2.jpg', 'photos'
60
+ #
61
+ # # Renaming an object
62
+ # OSSObject.rename 'headshot.jpg', 'portrait.jpg', 'photos'
63
+ #
64
+ # # Deleting an object
65
+ # OSSObject.delete 'headshot.jpg', 'photos'
66
+ #
67
+ # ==== More about objects and their metadata
68
+ #
69
+ # You can find out the content type of your object with the <tt>content_type</tt> method:
70
+ #
71
+ # song.content_type
72
+ # # => "audio/mpeg"
73
+ #
74
+ # You can change the content type as well if you like:
75
+ #
76
+ # song.content_type = 'application/pdf'
77
+ # song.store
78
+ #
79
+ # (Keep in mind that due to limitations in OSS's exposed API, the only way to change things like the content_type
80
+ # is to PUT the object onto OSS again. In the case of large files, this will result in fully re-uploading the file.)
81
+ #
82
+ # A bevy of information about an object can be had using the <tt>about</tt> method:
83
+ #
84
+ # pp song.about
85
+ # {"last-modified" => "Sat, 28 Oct 2006 21:29:26 GMT",
86
+ # "content-type" => "binary/octet-stream",
87
+ # "etag" => "\"dc629038ffc674bee6f62eb64ff3a\"",
88
+ # "date" => "Sat, 28 Oct 2006 21:30:41 GMT",
89
+ # "x-oss-request-id" => "B7BC68F55495B1C8",
90
+ # "server" => "AliyunOSS",
91
+ # "content-length" => "3418766"}
92
+ #
93
+ # You can get and set metadata for an object:
94
+ #
95
+ # song.metadata
96
+ # # => {}
97
+ # song.metadata[:album] = "A River Ain't Too Much To Love"
98
+ # # => "A River Ain't Too Much To Love"
99
+ # song.metadata[:released] = 2005
100
+ # pp song.metadata
101
+ # {"x-oss-meta-released" => 2005,
102
+ # "x-oss-meta-album" => "A River Ain't Too Much To Love"}
103
+ # song.store
104
+ #
105
+ # That metadata will be saved in OSS and is hence forth available from that object:
106
+ #
107
+ # song = OSSObject.find('black-flowers.mp3', 'jukebox')
108
+ # pp song.metadata
109
+ # {"x-oss-meta-released" => "2005",
110
+ # "x-oss-meta-album" => "A River Ain't Too Much To Love"}
111
+ # song.metadata[:released]
112
+ # # => "2005"
113
+ # song.metadata[:released] = 2006
114
+ # pp song.metadata
115
+ # {"x-oss-meta-released" => 2006,
116
+ # "x-oss-meta-album" => "A River Ain't Too Much To Love"}
117
+ class OSSObject < Base
118
+ class << self
119
+ # Returns the value of the object with <tt>key</tt> in the specified bucket.
120
+ #
121
+ # === Conditional GET options
122
+ #
123
+ # * <tt>:if_modified_since</tt> - Return the object only if it has been modified since the specified time,
124
+ # otherwise return a 304 (not modified).
125
+ # * <tt>:if_unmodified_since</tt> - Return the object only if it has not been modified since the specified time,
126
+ # otherwise raise PreconditionFailed.
127
+ # * <tt>:if_match</tt> - Return the object only if its entity tag (ETag) is the same as the one specified,
128
+ # otherwise raise PreconditionFailed.
129
+ # * <tt>:if_none_match</tt> - Return the object only if its entity tag (ETag) is different from the one specified,
130
+ # otherwise return a 304 (not modified).
131
+ #
132
+ # === Other options
133
+ # * <tt>:range</tt> - Return only the bytes of the object in the specified range.
134
+ def value(key, bucket = nil, options = {}, &block)
135
+ Value.new(get(path!(bucket, key, options), options, &block))
136
+ end
137
+
138
+ def stream(key, bucket = nil, options = {}, &block)
139
+ value(key, bucket, options) do |response|
140
+ response.read_body(&block)
141
+ end
142
+ end
143
+
144
+ # Returns the object whose key is <tt>name</tt> in the specified bucket. If the specified key does not
145
+ # exist, a NoSuchKey exception will be raised.
146
+ def find(key, bucket = nil)
147
+ # N.B. This is arguably a hack. From what the current OSS API exposes, when you retrieve a bucket, it
148
+ # provides a listing of all the files in that bucket (assuming you haven't limited the scope of what it returns).
149
+ # Each file in the listing contains information about that file. It is from this information that an OSSObject is built.
150
+ #
151
+ # If you know the specific file that you want, OSS allows you to make a get request for that specific file and it returns
152
+ # the value of that file in its response body. This response body is used to build an OSSObject::Value object.
153
+ # If you want information about that file, you can make a head request and the headers of the response will contain
154
+ # information about that file. There is no way, though, to say, give me the representation of just this given file the same
155
+ # way that it would appear in a bucket listing.
156
+ #
157
+ # When fetching a bucket, you can provide options which narrow the scope of what files should be returned in that listing.
158
+ # Of those options, one is <tt>marker</tt> which is a string and instructs the bucket to return only object's who's key comes after
159
+ # the specified marker according to alphabetic order. Another option is <tt>max-keys</tt> which defaults to 1000 but allows you
160
+ # to dictate how many objects should be returned in the listing. With a combination of <tt>marker</tt> and <tt>max-keys</tt> you can
161
+ # *almost* specify exactly which file you'd like it to return, but <tt>marker</tt> is not inclusive. In other words, if there is a bucket
162
+ # which contains three objects who's keys are respectively 'a', 'b' and 'c', then fetching a bucket listing with marker set to 'b' will only
163
+ # return 'c', not 'b'.
164
+ #
165
+ # Given all that, my hack to fetch a bucket with only one specific file, is to set the marker to the result of calling String#previous on
166
+ # the desired object's key, which functionally makes the key ordered one degree higher than the desired object key according to
167
+ # alphabetic ordering. This is a hack, but it should work around 99% of the time. I can't think of a scenario where it would return
168
+ # something incorrect.
169
+
170
+ # We need to ensure the key doesn't have extended characters but not uri escape it before doing the lookup and comparing since if the object exists,
171
+ # the key on OSS will have been normalized
172
+ key = key.remove_extended unless key.valid_utf8?
173
+ bucket = Bucket.find(bucket_name(bucket), :marker => key.previous, :max_keys => 1)
174
+ # If our heuristic failed, trigger a NoSuchKey exception
175
+ if (object = bucket.objects.first) && object.key == key
176
+ object
177
+ else
178
+ raise NoSuchKey.new("No such key `#{key}'", bucket)
179
+ end
180
+ end
181
+
182
+ # Makes a copy of the object with <tt>key</tt> to <tt>copy_key</tt>, preserving the ACL of the existing object if the <tt>:copy_acl</tt> option is true (default false).
183
+ def copy(key, copy_key, bucket = nil, options = {})
184
+ bucket = bucket_name(bucket)
185
+ source_key = path!(bucket, key)
186
+ default_options = {'x-oss-copy-source' => source_key}
187
+ target_key = path!(bucket, copy_key)
188
+ put(target_key, default_options.merge(options))
189
+ end
190
+
191
+ # Rename the object with key <tt>from</tt> to have key in <tt>to</tt>.
192
+ def rename(from, to, bucket = nil, options = {})
193
+ copy(from, to, bucket, options)
194
+ delete(from, bucket)
195
+ end
196
+
197
+ # Fetch information about the object with <tt>key</tt> from <tt>bucket</tt>. Information includes content type, content length,
198
+ # last modified time, and others.
199
+ #
200
+ # If the specified key does not exist, NoSuchKey is raised.
201
+ def about(key, bucket = nil, options = {})
202
+ response = head(path!(bucket, key, options), options)
203
+ raise NoSuchKey.new("No such key `#{key}'", bucket) if response.code == 404
204
+ About.new(response.headers)
205
+ end
206
+
207
+ # Checks if the object with <tt>key</tt> in <tt>bucket</tt> exists.
208
+ #
209
+ # OSSObject.exists? 'kiss.jpg', 'marcel'
210
+ # # => true
211
+ def exists?(key, bucket = nil)
212
+ about(key, bucket)
213
+ true
214
+ rescue NoSuchKey
215
+ false
216
+ end
217
+
218
+ # Delete object with <tt>key</tt> from <tt>bucket</tt>.
219
+ def delete(key, bucket = nil, options = {})
220
+ # A bit confusing. Calling super actually makes an HTTP DELETE request. The delete method is
221
+ # defined in the Base class. It happens to have the same name.
222
+ super(path!(bucket, key, options), options).success?
223
+ end
224
+
225
+ # 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.
226
+ # If <tt>data</tt> is an I/O stream it will be read in segments and written to the socket incrementally. This approach
227
+ # may be desirable for very large files so they are not read into memory all at once.
228
+ #
229
+ # # Non streamed upload
230
+ # OSSObject.store('greeting.txt', 'hello world!', 'marcel')
231
+ #
232
+ # # Streamed upload
233
+ # OSSObject.store('roots.mpeg', open('roots.mpeg'), 'marcel')
234
+ def store(key, data, bucket = nil, options = {})
235
+ validate_key!(key)
236
+ # Must build path before infering content type in case bucket is being used for options
237
+ path = path!(bucket, key, options)
238
+ infer_content_type!(key, options)
239
+
240
+ put(path, options, data) # Don't call .success? on response. We want to get the etag.
241
+ end
242
+ alias_method :create, :store
243
+ alias_method :save, :store
244
+
245
+ # All private objects are accessible via an authenticated GET request to the OSS servers. You can generate an
246
+ # authenticated url for an object like this:
247
+ #
248
+ # OSSObject.url_for('beluga_baby.jpg', 'marcel_molina')
249
+ #
250
+ # By default authenticated urls expire 5 minutes after they were generated.
251
+ #
252
+ # Expiration options can be specified either with an absolute time since the epoch with the <tt>:expires</tt> options,
253
+ # or with a number of seconds relative to now with the <tt>:expires_in</tt> options:
254
+ #
255
+ # # Absolute expiration date
256
+ # # (Expires January 18th, 2038)
257
+ # doomsday = Time.mktime(2038, 1, 18).to_i
258
+ # OSSObject.url_for('beluga_baby.jpg',
259
+ # 'marcel',
260
+ # :expires => doomsday)
261
+ #
262
+ # # Expiration relative to now specified in seconds
263
+ # # (Expires in 3 hours)
264
+ # OSSObject.url_for('beluga_baby.jpg',
265
+ # 'marcel',
266
+ # :expires_in => 60 * 60 * 3)
267
+ #
268
+ # You can specify whether the url should go over SSL with the <tt>:use_ssl</tt> option:
269
+ #
270
+ # # Url will use https protocol
271
+ # OSSObject.url_for('beluga_baby.jpg',
272
+ # 'marcel',
273
+ # :use_ssl => true)
274
+ #
275
+ # By default, the ssl settings for the current connection will be used.
276
+ #
277
+ # If you have an object handy, you can use its <tt>url</tt> method with the same objects:
278
+ #
279
+ # song.url(:expires_in => 30)
280
+ #
281
+ # To get an unauthenticated url for the object, such as in the case
282
+ # when the object is publicly readable, pass the
283
+ # <tt>:authenticated</tt> option with a value of <tt>false</tt>.
284
+ #
285
+ # OSSObject.url_for('beluga_baby.jpg',
286
+ # 'marcel',
287
+ # :authenticated => false)
288
+ # # => http://oss.aliyuncs.com/marcel/beluga_baby.jpg
289
+ def url_for(name, bucket = nil, options = {})
290
+ connection.url_for(path!(bucket, name, options), options) # Do not normalize options
291
+ end
292
+
293
+ def path!(bucket, name, options = {}) #:nodoc:
294
+ # We're using the second argument for options
295
+ if bucket.is_a?(Hash)
296
+ options.replace(bucket)
297
+ bucket = nil
298
+ end
299
+ '/' << File.join(bucket_name(bucket), name)
300
+ end
301
+
302
+ private
303
+
304
+ def validate_key!(key)
305
+ raise InvalidKeyName.new(key) unless key && key.size <= 1024
306
+ end
307
+
308
+ def infer_content_type!(key, options)
309
+ return if options.has_key?(:content_type)
310
+ if mime_type = MIME::Types.type_for(key).first
311
+ options[:content_type] = mime_type.content_type
312
+ end
313
+ rescue ArgumentError #ruby1.9 非法utf-8字符dowcase报错 "318597/620065/GTL_75\24300_A600_A610.zip"
314
+ end
315
+ end
316
+
317
+ class Value < String #:nodoc:
318
+ attr_reader :response
319
+ def initialize(response)
320
+ super(response.body)
321
+ @response = response
322
+ end
323
+ end
324
+
325
+ class About < Hash #:nodoc:
326
+ def initialize(headers)
327
+ super()
328
+ replace(headers)
329
+ metadata
330
+ end
331
+
332
+ def [](header)
333
+ super(header.to_header)
334
+ end
335
+
336
+ def []=(header, value)
337
+ super(header.to_header, value)
338
+ end
339
+
340
+ def to_headers
341
+ self.merge(metadata.to_headers)
342
+ end
343
+
344
+ def metadata
345
+ Metadata.new(self)
346
+ end
347
+ memoized :metadata
348
+ end
349
+
350
+ class Metadata < Hash #:nodoc:
351
+ HEADER_PREFIX = 'x-oss-meta-'
352
+ SIZE_LIMIT = 2048 # 2 kilobytes
353
+
354
+ def initialize(headers)
355
+ @headers = headers
356
+ super()
357
+ extract_metadata!
358
+ end
359
+
360
+ def []=(header, value)
361
+ super(header_name(header.to_header), value)
362
+ end
363
+
364
+ def [](header)
365
+ super(header_name(header.to_header))
366
+ end
367
+
368
+ def to_headers
369
+ validate!
370
+ self
371
+ end
372
+
373
+ private
374
+ attr_reader :headers
375
+
376
+ def extract_metadata!
377
+ headers.keys.grep(Regexp.new(HEADER_PREFIX)).each do |metadata_header|
378
+ self[metadata_header] = headers.delete(metadata_header)
379
+ end
380
+ end
381
+
382
+ def header_name(name)
383
+ name =~ Regexp.new(HEADER_PREFIX) ? name : [HEADER_PREFIX, name].join
384
+ end
385
+
386
+ def validate!
387
+ invalid_headers = inject([]) do |invalid, (name, value)|
388
+ invalid << name unless valid?(value)
389
+ invalid
390
+ end
391
+
392
+ raise InvalidMetadataValue.new(invalid_headers) unless invalid_headers.empty?
393
+ end
394
+
395
+ def valid?(value)
396
+ value && value.size < SIZE_LIMIT
397
+ end
398
+ end
399
+
400
+ attr_writer :value #:nodoc:
401
+
402
+ # Provides readers and writers for all valid header settings listed in <tt>valid_header_settings</tt>.
403
+ # Subsequent saves to the object after setting any of the valid headers settings will be reflected in
404
+ # information about the object.
405
+ #
406
+ # some_oss_object.content_type
407
+ # => nil
408
+ # some_oss_object.content_type = 'text/plain'
409
+ # => "text/plain"
410
+ # some_oss_object.content_type
411
+ # => "text/plain"
412
+ # some_oss_object.store
413
+ # OSSObject.about(some_oss_object.key, some_oss_object.bucket.name)['content-type']
414
+ # => "text/plain"
415
+ include SelectiveAttributeProxy #:nodoc
416
+
417
+ proxy_to :about, :exclusively => false
418
+
419
+ # Initializes a new OSSObject.
420
+ def initialize(attributes = {}, &block)
421
+ super
422
+ self.value = attributes.delete(:value)
423
+ self.bucket = attributes.delete(:bucket)
424
+ yield self if block_given?
425
+ end
426
+
427
+ # The current object's bucket. If no bucket has been set, a NoBucketSpecified exception will be raised. For
428
+ # cases where you are not sure if the bucket has been set, you can use the belongs_to_bucket? method.
429
+ def bucket
430
+ @bucket or raise NoBucketSpecified
431
+ end
432
+
433
+ # Sets the bucket that the object belongs to.
434
+ def bucket=(bucket)
435
+ @bucket = bucket
436
+ self
437
+ end
438
+
439
+ # Returns true if the current object has been assigned to a bucket yet. Objects must belong to a bucket before they
440
+ # can be saved onto OSS.
441
+ def belongs_to_bucket?
442
+ !@bucket.nil?
443
+ end
444
+ alias_method :orphan?, :belongs_to_bucket?
445
+
446
+ # Returns the key of the object. If the key is not set, a NoKeySpecified exception will be raised. For cases
447
+ # where you are not sure if the key has been set, you can use the key_set? method. Objects must have a key
448
+ # set to be saved onto OSS. Objects which have already been saved onto OSS will always have their key set.
449
+ def key
450
+ attributes['key'] or raise NoKeySpecified
451
+ end
452
+
453
+ # Sets the key for the current object.
454
+ def key=(value)
455
+ attributes['key'] = value
456
+ end
457
+
458
+ # Returns true if the current object has had its key set yet. Objects which have already been saved will
459
+ # always return true. This method is useful for objects which have not been saved yet so you know if you
460
+ # need to set the object's key since you can not save an object unless its key has been set.
461
+ #
462
+ # object.store if object.key_set? && object.belongs_to_bucket?
463
+ def key_set?
464
+ !attributes['key'].nil?
465
+ end
466
+
467
+ # Lazily loads object data.
468
+ #
469
+ # Force a reload of the data by passing <tt>:reload</tt>.
470
+ #
471
+ # object.value(:reload)
472
+ #
473
+ # When loading the data for the first time you can optionally yield to a block which will
474
+ # allow you to stream the data in segments.
475
+ #
476
+ # object.value do |segment|
477
+ # send_data segment
478
+ # end
479
+ #
480
+ # The full list of options are listed in the documentation for its class method counter part, OSSObject::value.
481
+ def value(options = {}, &block)
482
+ if options.is_a?(Hash)
483
+ reload = !options.empty?
484
+ else
485
+ reload = options
486
+ options = {}
487
+ end
488
+ expirable_memoize(reload) do
489
+ self.class.stream(key, bucket.name, options, &block)
490
+ end
491
+ end
492
+
493
+ # Interface to information about the current object. Information is read only, though some of its data
494
+ # can be modified through specific methods, such as content_type and content_type=.
495
+ #
496
+ # pp some_object.about
497
+ # {"last-modified" => "Sat, 28 Oct 2006 21:29:26 GMT",
498
+ # "x-oss-id-2" => "LdcQRk5qLwxJQiZ8OH50HhoyKuqyWoJ67B6i+rOE5MxpjJTWh1kCkL+I0NQzbVQn",
499
+ # "content-type" => "binary/octet-stream",
500
+ # "etag" => "\"dc629038ffc674bee6f62eb68454ff3a\"",
501
+ # "date" => "Sat, 28 Oct 2006 21:30:41 GMT",
502
+ # "x-oss-request-id" => "B7BC68F55495B1C8",
503
+ # "server" => "AliyunOSS",
504
+ # "content-length" => "3418766"}
505
+ #
506
+ # some_object.content_type
507
+ # # => "binary/octet-stream"
508
+ # some_object.content_type = 'audio/mpeg'
509
+ # some_object.content_type
510
+ # # => 'audio/mpeg'
511
+ # some_object.store
512
+ def about
513
+ stored? ? self.class.about(key, bucket.name) : About.new
514
+ end
515
+ memoized :about
516
+
517
+ # Interface to viewing and editing metadata for the current object. To be treated like a Hash.
518
+ #
519
+ # some_object.metadata
520
+ # # => {}
521
+ # some_object.metadata[:author] = 'Dave Thomas'
522
+ # some_object.metadata
523
+ # # => {"x-oss-meta-author" => "Dave Thomas"}
524
+ # some_object.metadata[:author]
525
+ # # => "Dave Thomas"
526
+ def metadata
527
+ about.metadata
528
+ end
529
+ memoized :metadata
530
+
531
+ # Saves the current object with the specified <tt>options</tt>. Valid options are listed in the documentation for OSSObject::store.
532
+ def store(options = {})
533
+ raise DeletedObject if frozen?
534
+ options = about.to_headers.merge(options) if stored?
535
+ response = self.class.store(key, value, bucket.name, options)
536
+ bucket.update(:stored, self)
537
+ response.success?
538
+ end
539
+ alias_method :create, :store
540
+ alias_method :save, :store
541
+
542
+ # Deletes the current object. Trying to save an object after it has been deleted with
543
+ # raise a DeletedObject exception.
544
+ def delete
545
+ bucket.update(:deleted, self)
546
+ freeze
547
+ self.class.delete(key, bucket.name)
548
+ end
549
+
550
+ # Copies the current object, given it the name <tt>copy_name</tt>. Keep in mind that due to limitations in
551
+ # OSS's API, this operation requires retransmitting the entire object to OSS.
552
+ def copy(copy_name, options = {})
553
+ self.class.copy(key, copy_name, bucket.name, options)
554
+ end
555
+
556
+ # Rename the current object. Keep in mind that due to limitations in OSS's API, this operation requires
557
+ # retransmitting the entire object to OSS.
558
+ def rename(to, options = {})
559
+ self.class.rename(key, to, bucket.name, options)
560
+ end
561
+
562
+ def etag(reload = false)
563
+ return nil unless stored?
564
+ expirable_memoize(reload) do
565
+ reload ? about(reload)['etag'][1...-1] : attributes['e_tag'][1...-1]
566
+ end
567
+ end
568
+
569
+ # Returns the owner of the current object.
570
+ def owner
571
+ Owner.new(attributes['owner'])
572
+ end
573
+ memoized :owner
574
+
575
+ # Generates an authenticated url for the current object. Accepts the same options as its class method
576
+ # counter part OSSObject.url_for.
577
+ def url(options = {})
578
+ self.class.url_for(key, bucket.name, options)
579
+ end
580
+
581
+ # Returns true if the current object has been stored on OSS yet.
582
+ def stored?
583
+ !attributes['e_tag'].nil?
584
+ end
585
+
586
+ def ==(ossobject) #:nodoc:
587
+ path == ossobject.path
588
+ end
589
+
590
+ def path #:nodoc:
591
+ self.class.path!(
592
+ belongs_to_bucket? ? bucket.name : '(no bucket)',
593
+ key_set? ? key : '(no key)'
594
+ )
595
+ end
596
+
597
+ # Don't dump binary data :)
598
+ def inspect #:nodoc:
599
+ "#<%s:0x%s '%s'>" % [self.class, object_id, path]
600
+ end
601
+
602
+ private
603
+ def proxiable_attribute?(name)
604
+ valid_header_settings.include?(name)
605
+ end
606
+
607
+ def valid_header_settings
608
+ %w(cache_control content_type content_length content_md5 content_disposition content_encoding expires)
609
+ end
610
+ end
611
+ end
612
+ end