Class: Aliyun::Oss::Client
- Inherits:
-
Object
- Object
- Aliyun::Oss::Client
- Defined in:
- lib/aliyun/oss/client/clients.rb,
lib/aliyun/oss/client.rb,
lib/aliyun/oss/client/buckets.rb,
lib/aliyun/oss/client/bucket_objects.rb,
lib/aliyun/oss/client/bucket_multiparts.rb
Overview
Here is some services used to make object based API possible, they are all contains a reference to instance of client, which used to do the real job.
buckets: used to do many buckets operations eg: #list, #create, #delete
client.buckets
bucket_objects: used to do some operation on objects eg: #list, #create, :delete, #copy
client.bucket_objects
bucket_multiparts: used to do some operation for multiparts eg: #init, #list
client.bucket_multiparts
Defined Under Namespace
Modules: BucketMultiparts, BucketObjects, Buckets Classes: BucketMultipartsService, BucketObjectsService, BucketsService, ClientService
Instance Attribute Summary collapse
-
#access_key ⇒ Object
readonly
Returns the value of attribute access_key.
-
#bucket ⇒ Object
Returns the value of attribute bucket.
-
#secret_key ⇒ Object
readonly
Returns the value of attribute secret_key.
Instance Method Summary collapse
-
#bucket_abort_multipart(upload_id, key) ⇒ Response
Abort a Multipart Upload event.
-
#bucket_append_object(key, file, position = 0, headers = {}) ⇒ Response
Append data to a object, will create Appendable object.
-
#bucket_complete_multipart(upload_id, key, parts = []) ⇒ Response
Complete a Multipart Upload event.
-
#bucket_copy_object(key, source_bucket, source_key, headers = {}) ⇒ Response
Copy an existing object in OSS into another object.
-
#bucket_create(name, location = 'oss-cn-hangzhou', acl = 'private') ⇒ Response
Create bucket.
-
#bucket_create_object(key, file, headers = {}) ⇒ Response
Upload file to bucket.
-
#bucket_delete(name) ⇒ Response
Delete bucket.
-
#bucket_delete_object(key) ⇒ Response
Delete object from bucket.
-
#bucket_delete_objects(keys, quiet = false) ⇒ Response
Delete multiple objects, at max 1000 at once.
-
#bucket_disable_cors ⇒ Response
Used to disable cors and clear rules for bucket.
-
#bucket_disable_lifecycle ⇒ Object
Used to disable lifecycle for bucket.
-
#bucket_disable_logging ⇒ Response
Used to disable access logging.
-
#bucket_disable_website ⇒ Response
Used to disable website hostted mode.
-
#bucket_enable_cors(rules = []) ⇒ Response
Used to enable CORS and set rules for bucket.
-
#bucket_enable_lifecycle(rules = []) ⇒ Response
Used to enable and set lifecycle for bucket.
-
#bucket_enable_logging(target_bucket, target_prefix = nil) ⇒ Response
Used to enable access logging.
-
#bucket_enable_website(suffix, key = nil) ⇒ Response
Used to enable static website hosted mode.
-
#bucket_get_acl ⇒ Response
Get ACL for bucket.
-
#bucket_get_cors ⇒ Response
Get the CORS rules of bucket.
-
#bucket_get_lifecycle ⇒ Response
Get the lifecycle configuration of bucket.
-
#bucket_get_location ⇒ Response
Get the location information of the Bucket’s data center.
-
#bucket_get_logging ⇒ Response
Get the log configuration of Bucket.
-
#bucket_get_meta_object(key, headers = {}) ⇒ Response
Get meta information of object.
-
#bucket_get_object(key, query = {}, headers = {}) ⇒ Response
Get the object.
-
#bucket_get_object_acl(key) ⇒ Response
Get access of object.
-
#bucket_get_referer ⇒ Response
Get the referer configuration of bucket.
-
#bucket_get_website ⇒ Response
Get the bucket state of static website hosting.
-
#bucket_init_multipart(key, headers = {}) ⇒ Response
Initialize a Multipart Upload event, before using Multipart Upload mode to transmit data, we has to call the interface to notify the OSS initialize a Multipart Upload events.
-
#bucket_list_multiparts(options = {}) ⇒ Response
List existing opened Multipart Upload event.
-
#bucket_list_objects(options = {}) ⇒ Response
List objects in the bucket.
-
#bucket_list_parts(upload_id, key, options = {}) ⇒ Response
List uploaded parts for Multipart Upload event.
-
#bucket_multipart_copy_upload(upload_id, key, number, options = {}) ⇒ Response
Upload a Part from an existing Object Copy data.
-
#bucket_multipart_upload(upload_id, key, number, file) ⇒ Response
Upload object in part.
- #bucket_multiparts ⇒ Object
- #bucket_objects ⇒ Object
-
#bucket_preflight(object_key, origin, request_method, request_headers = []) ⇒ Response
OPTIONS Object.
-
#bucket_set_acl(acl) ⇒ Response
Used to modify the bucket access.
-
#bucket_set_object_acl(key, acl) ⇒ Response
Set access of object.
-
#bucket_set_referer(referers = [], allowed_empty = false) ⇒ Response
Used to set referer for bucket.
- #buckets ⇒ Object
-
#initialize(access_key, secret_key, options = {}) ⇒ Response
constructor
Initialize a object.
-
#list_buckets(options = {}) ⇒ Response
List buckets.
Constructor Details
#initialize(access_key, secret_key, options = {}) ⇒ Response
Initialize a object
20 21 22 23 24 25 26 27 |
# File 'lib/aliyun/oss/client.rb', line 20 def initialize(access_key, secret_key, = {}) @access_key = access_key @secret_key = secret_key @options = @bucket = [:bucket] @services = {} end |
Instance Attribute Details
#access_key ⇒ Object (readonly)
Returns the value of attribute access_key.
6 7 8 |
# File 'lib/aliyun/oss/client.rb', line 6 def access_key @access_key end |
#bucket ⇒ Object
Returns the value of attribute bucket.
7 8 9 |
# File 'lib/aliyun/oss/client.rb', line 7 def bucket @bucket end |
#secret_key ⇒ Object (readonly)
Returns the value of attribute secret_key.
6 7 8 |
# File 'lib/aliyun/oss/client.rb', line 6 def secret_key @secret_key end |
Instance Method Details
#bucket_abort_multipart(upload_id, key) ⇒ Response
After abort the Multipart Upload, the Uploaded data will be deleted
When abort a Multipart Upload event, if there are still part upload belonging to this event, then theree parts will not be removed. So if there is a concurrent access, in order to release the space on the OSS completely, you need to call #bucket_abort_multipart a few times.
Abort a Multipart Upload event
652 653 654 655 |
# File 'lib/aliyun/oss/client.rb', line 652 def bucket_abort_multipart(upload_id, key) query = { 'uploadId' => upload_id } http.delete("/#{key}", query: query, bucket: bucket, key: key) end |
#bucket_append_object(key, file, position = 0, headers = {}) ⇒ Response
Append data to a object, will create Appendable object
431 432 433 434 435 436 437 |
# File 'lib/aliyun/oss/client.rb', line 431 def bucket_append_object(key, file, position = 0, headers = {}) query = { 'append' => true, 'position' => position } body = Utils.to_data(file) http.post("/#{key}", query: query, headers: headers, body: body, bucket: bucket, key: key) end |
#bucket_complete_multipart(upload_id, key, parts = []) ⇒ Response
Complete a Multipart Upload event.
628 629 630 631 632 633 634 635 636 637 |
# File 'lib/aliyun/oss/client.rb', line 628 def bucket_complete_multipart(upload_id, key, parts = []) fail MultipartPartsEmptyError if parts.nil? || parts.empty? fail MultipartUploadIdEmptyError if upload_id.nil? query = { 'uploadId' => upload_id } body = XmlGenerator.generate_complete_multipart_xml(parts) http.post("/#{key}", query: query, body: body, bucket: bucket, key: key) end |
#bucket_copy_object(key, source_bucket, source_key, headers = {}) ⇒ Response
Copy an existing object in OSS into another object
410 411 412 413 414 415 416 417 |
# File 'lib/aliyun/oss/client.rb', line 410 def bucket_copy_object(key, source_bucket, source_key, headers = {}) fail('source_bucket must be not empty!') if source_bucket.nil? || source_bucket.empty? fail('source_key must be not empty!') if source_key.nil? || source_key.empty? headers.merge!('x-oss-copy-source' => "/#{source_bucket}/#{source_key}") http.put("/#{key}", headers: headers, bucket: bucket, key: key) end |
#bucket_create(name, location = 'oss-cn-hangzhou', acl = 'private') ⇒ Response
Create bucket
78 79 80 81 82 83 84 85 |
# File 'lib/aliyun/oss/client.rb', line 78 def bucket_create(name, location = 'oss-cn-hangzhou', acl = 'private') query = { 'acl' => true } headers = { 'x-oss-acl' => acl } body = XmlGenerator.generate_create_bucket_xml(location) http.put('/', query: query, headers: headers, body: body, bucket: name, location: location) end |
#bucket_create_object(key, file, headers = {}) ⇒ Response
Upload file to bucket
385 386 387 |
# File 'lib/aliyun/oss/client.rb', line 385 def bucket_create_object(key, file, headers = {}) http.put("/#{key}", headers: headers, body: Utils.to_data(file), bucket: bucket, key: key) end |
#bucket_delete(name) ⇒ Response
Delete bucket
96 97 98 |
# File 'lib/aliyun/oss/client.rb', line 96 def bucket_delete(name) http.delete('/', bucket: name) end |
#bucket_delete_object(key) ⇒ Response
Delete object from bucket
470 471 472 |
# File 'lib/aliyun/oss/client.rb', line 470 def bucket_delete_object(key) http.delete("/#{key}", bucket: bucket, key: key) end |
#bucket_delete_objects(keys, quiet = false) ⇒ Response
Delete multiple objects, at max 1000 at once
482 483 484 485 486 487 488 |
# File 'lib/aliyun/oss/client.rb', line 482 def bucket_delete_objects(keys, quiet = false) query = { 'delete' => true } body = XmlGenerator.generate_delete_objects_xml(keys, quiet) http.post('/', query: query, body: body, bucket: bucket) end |
#bucket_disable_cors ⇒ Response
Used to disable cors and clear rules for bucket
262 263 264 265 |
# File 'lib/aliyun/oss/client.rb', line 262 def bucket_disable_cors query = { 'cors' => false } http.delete('/', query: query, bucket: bucket) end |
#bucket_disable_lifecycle ⇒ Object
Used to disable lifecycle for bucket.
223 224 225 226 |
# File 'lib/aliyun/oss/client.rb', line 223 def bucket_disable_lifecycle query = { 'lifecycle' => false } http.delete('/', query: query, bucket: bucket) end |
#bucket_disable_logging ⇒ Response
Used to disable access logging.
140 141 142 143 |
# File 'lib/aliyun/oss/client.rb', line 140 def bucket_disable_logging query = { 'logging' => false } http.delete('/', query: query, bucket: bucket) end |
#bucket_disable_website ⇒ Response
Used to disable website hostted mode.
170 171 172 173 |
# File 'lib/aliyun/oss/client.rb', line 170 def bucket_disable_website query = { 'website' => false } http.delete('/', query: query, bucket: bucket) end |
#bucket_enable_cors(rules = []) ⇒ Response
Used to enable CORS and set rules for bucket
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 |
# File 'lib/aliyun/oss/client.rb', line 239 def bucket_enable_cors(rules = []) query = { 'cors' => true } rules = Utils.wrap(rules) rules.each do |rule| unless rule.valid? fail Aliyun::Oss::InvalidCorsRuleError, rule.inspect end end body = XmlGenerator.generate_cors_rules(rules) http.put('/', query: query, body: body, bucket: bucket) end |
#bucket_enable_lifecycle(rules = []) ⇒ Response
Used to enable and set lifecycle for bucket
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 |
# File 'lib/aliyun/oss/client.rb', line 204 def bucket_enable_lifecycle(rules = []) query = { 'lifecycle' => true } rules = Utils.wrap(rules) rules.each do |rule| unless rule.valid? fail Aliyun::Oss::InvalidLifeCycleRuleError, rule.inspect end end body = XmlGenerator.generate_lifecycle_rules(rules) http.put('/', query: query, body: body, bucket: bucket) end |
#bucket_enable_logging(target_bucket, target_prefix = nil) ⇒ Response
Used to enable access logging.
124 125 126 127 128 129 130 131 |
# File 'lib/aliyun/oss/client.rb', line 124 def bucket_enable_logging(target_bucket, target_prefix = nil) query = { 'logging' => true } body = XmlGenerator.generate_enable_logging_xml(target_bucket, target_prefix) http.put('/', query: query, body: body, bucket: bucket) end |
#bucket_enable_website(suffix, key = nil) ⇒ Response
Used to enable static website hosted mode.
155 156 157 158 159 160 161 |
# File 'lib/aliyun/oss/client.rb', line 155 def bucket_enable_website(suffix, key = nil) query = { 'website' => true } body = XmlGenerator.generate_enable_website_xml(suffix, key) http.put('/', query: query, body: body, bucket: bucket) end |
#bucket_get_acl ⇒ Response
Get ACL for bucket
300 301 302 303 |
# File 'lib/aliyun/oss/client.rb', line 300 def bucket_get_acl query = { 'acl' => true } http.get('/', query: query, bucket: bucket) end |
#bucket_get_cors ⇒ Response
Get the CORS rules of bucket
362 363 364 365 |
# File 'lib/aliyun/oss/client.rb', line 362 def bucket_get_cors query = { 'cors' => true } http.get('/', query: query, bucket: bucket) end |
#bucket_get_lifecycle ⇒ Response
Get the lifecycle configuration of bucket
352 353 354 355 |
# File 'lib/aliyun/oss/client.rb', line 352 def bucket_get_lifecycle query = { 'lifecycle' => true } http.get('/', query: query, bucket: bucket) end |
#bucket_get_location ⇒ Response
Get the location information of the Bucket’s data center
310 311 312 313 |
# File 'lib/aliyun/oss/client.rb', line 310 def bucket_get_location query = { 'location' => true } http.get('/', query: query, bucket: bucket) end |
#bucket_get_logging ⇒ Response
Get the log configuration of Bucket
322 323 324 325 |
# File 'lib/aliyun/oss/client.rb', line 322 def bucket_get_logging query = { 'logging' => true } http.get('/', query: query, bucket: bucket) end |
#bucket_get_meta_object(key, headers = {}) ⇒ Response
Get meta information of object
504 505 506 |
# File 'lib/aliyun/oss/client.rb', line 504 def (key, headers = {}) http.head("/#{key}", headers: headers, bucket: bucket, key: key) end |
#bucket_get_object(key, query = {}, headers = {}) ⇒ Response
Get the object
459 460 461 |
# File 'lib/aliyun/oss/client.rb', line 459 def bucket_get_object(key, query = {}, headers = {}) http.get("/#{key}", query: query, headers: headers, bucket: bucket, key: key) end |
#bucket_get_object_acl(key) ⇒ Response
Get access of object
517 518 519 520 |
# File 'lib/aliyun/oss/client.rb', line 517 def bucket_get_object_acl(key) query = { 'acl' => true } http.get("/#{key}", query: query, bucket: bucket, key: key) end |
#bucket_get_referer ⇒ Response
Get the referer configuration of bucket
342 343 344 345 |
# File 'lib/aliyun/oss/client.rb', line 342 def bucket_get_referer query = { 'referer' => true } http.get('/', query: query, bucket: bucket) end |
#bucket_get_website ⇒ Response
Get the bucket state of static website hosting.
332 333 334 335 |
# File 'lib/aliyun/oss/client.rb', line 332 def bucket_get_website query = { 'website' => true } http.get('/', query: query, bucket: bucket) end |
#bucket_init_multipart(key, headers = {}) ⇒ Response
Initialize a Multipart Upload event, before using Multipart Upload mode to transmit data, we has to call the interface to notify the OSS initialize a Multipart Upload events.
552 553 554 555 |
# File 'lib/aliyun/oss/client.rb', line 552 def bucket_init_multipart(key, headers = {}) query = { 'uploads' => true } http.post("/#{key}", query: query, headers: headers, bucket: bucket, key: key) end |
#bucket_list_multiparts(options = {}) ⇒ Response
List existing opened Multipart Upload event.
670 671 672 673 674 675 676 677 |
# File 'lib/aliyun/oss/client.rb', line 670 def bucket_list_multiparts( = {}) accepted_keys = ['prefix', 'key-marker', 'upload-id-marker', 'max-uploads', 'delimiter', 'encoding-type'] query = Utils.hash_slice(, *accepted_keys) .merge('uploads' => true) http.get('/', query: query, bucket: bucket) end |
#bucket_list_objects(options = {}) ⇒ Response
List objects in the bucket
56 57 58 59 60 |
# File 'lib/aliyun/oss/client.rb', line 56 def bucket_list_objects( = {}) accepted_keys = ['prefix', 'marker', 'max-keys', 'delimiter', 'encoding-type'] query = Utils.hash_slice(, *accepted_keys) http.get('/', query: query, bucket: bucket) end |
#bucket_list_parts(upload_id, key, options = {}) ⇒ Response
List uploaded parts for Multipart Upload event
691 692 693 694 695 696 697 |
# File 'lib/aliyun/oss/client.rb', line 691 def bucket_list_parts(upload_id, key, = {}) accepted_keys = ['max-parts', 'part-number-marker', 'encoding-type'] query = Utils.hash_slice(, *accepted_keys).merge('uploadId' => upload_id) http.get("/#{key}", query: query, bucket: bucket, key: key) end |
#bucket_multipart_copy_upload(upload_id, key, number, options = {}) ⇒ Response
Upload a Part from an existing Object Copy data.
601 602 603 604 605 606 607 608 609 610 611 612 613 |
# File 'lib/aliyun/oss/client.rb', line 601 def bucket_multipart_copy_upload(upload_id, key, number, = {}) source_bucket = .delete(:source_bucket).to_s source_key = .delete(:source_key).to_s range = .delete(:range) fail MultipartSourceBucketEmptyError if source_bucket.empty? fail MultipartSourceKeyEmptyError if source_key.empty? query = { 'partNumber' => number, 'uploadId' => upload_id } headers = copy_upload_headers(source_bucket, source_key, range, ) http.put("/#{key}", query: query, headers: headers, bucket: bucket, key: key) end |
#bucket_multipart_upload(upload_id, key, number, file) ⇒ Response
Upload object in part.
571 572 573 574 575 576 577 578 |
# File 'lib/aliyun/oss/client.rb', line 571 def bucket_multipart_upload(upload_id, key, number, file) fail MultipartPartNumberEmptyError if number.nil? fail MultipartUploadIdEmptyError if upload_id.nil? || upload_id.empty? query = { 'partNumber' => number.to_s, 'uploadId' => upload_id } http.put("/#{key}", query: query, body: Utils.to_data(file), bucket: bucket, key: key) end |
#bucket_multiparts ⇒ Object
28 29 30 31 |
# File 'lib/aliyun/oss/client/clients.rb', line 28 def bucket_multiparts @services[:bucket_multiparts] ||= \ Client::BucketMultipartsService.new(self) end |
#bucket_objects ⇒ Object
24 25 26 |
# File 'lib/aliyun/oss/client/clients.rb', line 24 def bucket_objects @services[:bucket_objects] ||= Client::BucketObjectsService.new(self) end |
#bucket_preflight(object_key, origin, request_method, request_headers = []) ⇒ Response
OPTIONS Object
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 |
# File 'lib/aliyun/oss/client.rb', line 279 def bucket_preflight(object_key, origin, request_method, request_headers = []) path = object_key ? "/#{object_key}" : '/' headers = { 'Origin' => origin, 'Access-Control-Request-Method' => request_method } unless request_headers.empty? value = request_headers.join(',') headers.merge!('Access-Control-Request-Headers' => value) end http.(path, headers: headers, bucket: bucket, key: object_key) end |
#bucket_set_acl(acl) ⇒ Response
Used to modify the bucket access.
108 109 110 111 112 |
# File 'lib/aliyun/oss/client.rb', line 108 def bucket_set_acl(acl) query = { 'acl' => true } headers = { 'x-oss-acl' => acl } http.put('/', query: query, headers: headers, bucket: bucket) end |
#bucket_set_object_acl(key, acl) ⇒ Response
Set access of object
532 533 534 535 536 |
# File 'lib/aliyun/oss/client.rb', line 532 def bucket_set_object_acl(key, acl) query = { 'acl' => true } headers = { 'x-oss-object-acl' => acl } http.put("/#{key}", query: query, headers: headers, bucket: bucket, key: key) end |
#bucket_set_referer(referers = [], allowed_empty = false) ⇒ Response
Used to set referer for bucket.
185 186 187 188 189 190 191 |
# File 'lib/aliyun/oss/client.rb', line 185 def bucket_set_referer(referers = [], allowed_empty = false) query = { 'referer' => true } body = XmlGenerator.generate_set_referer_xml(referers, allowed_empty) http.put('/', query: query, body: body, bucket: bucket) end |
#buckets ⇒ Object
20 21 22 |
# File 'lib/aliyun/oss/client/clients.rb', line 20 def buckets @services[:buckets] ||= Client::BucketsService.new(self) end |
#list_buckets(options = {}) ⇒ Response
List buckets
39 40 41 42 |
# File 'lib/aliyun/oss/client.rb', line 39 def list_buckets( = {}) query = Utils.hash_slice(, 'prefix', 'marker', 'max-keys') http.get('/', query: query) end |