Class: Aliyun::OSS::Bucket
- Inherits:
-
Common::Struct::Base
- Object
- Common::Struct::Base
- Aliyun::OSS::Bucket
- Defined in:
- lib/aliyun/oss/bucket.rb
Overview
Bucket是用户的Object相关的操作的client,主要包括三部分功能:
-
bucket相关:获取/设置bucket的属性(acl, logging, referer, website, lifecycle, cors)
-
object相关:上传、下载、追加、拷贝object等
-
multipart相关:断点续传、断点续载
Instance Method Summary collapse
-
#abort_upload(upload_id, key) ⇒ Object
取消一个multipart上传请求,一般用于清除Bucket下因断点上传而产 生的文件碎片。成功取消后属于这个上传请求的分片都会被清除。.
-
#access_key_id ⇒ String
获取用户所设置的ACCESS_KEY_ID.
-
#acl ⇒ String
获取Bucket的ACL.
-
#acl=(acl) ⇒ Object
设置Bucket的ACL.
-
#append_object(key, pos, opts = {}) {|HTTP::StreamWriter| ... } ⇒ Integer
向Bucket中的object追加内容。如果object不存在,则创建一个 Appendable Object。.
-
#batch_delete_objects(keys, opts = {}) ⇒ Array<String>
批量删除object.
-
#bucket_url ⇒ String
获取Bucket的URL.
-
#copy_object(source, dest, opts = {}) ⇒ Hash
将Bucket中的一个object拷贝成另外一个object.
-
#cors ⇒ Array<OSS::CORSRule>
获取Bucket的跨域资源共享(CORS)的规则.
-
#cors=(rules) ⇒ Object
设置Bucket的跨域资源共享(CORS)的规则.
-
#delete_object(key) ⇒ Object
删除一个object.
-
#get_object(key, opts = {}) {|String| ... } ⇒ OSS::Object
从Bucket中下载一个object.
-
#get_object_acl(key) ⇒ String
获取object的ACL.
-
#get_object_cors(key) ⇒ OSS::CORSRule
获取object的CORS规则.
-
#initialize(opts = {}, protocol = nil) ⇒ Bucket
constructor
A new instance of Bucket.
-
#lifecycle ⇒ Array<OSS::LifeCycleRule>
获取Bucket的生命周期配置.
-
#lifecycle=(rules) ⇒ Object
设置Bucket的生命周期配置.
-
#list_objects(opts = {}) ⇒ Enumerator<Object>
列出bucket中的object.
-
#list_uploads(opts = {}) ⇒ Enumerator<Multipart::Transaction>
列出此Bucket中正在进行的multipart上传请求,不包括已经完成或者 被取消的。.
-
#logging ⇒ BucketLogging
获取Bucket的logging配置.
-
#logging=(logging) ⇒ Object
设置Bucket的logging配置.
-
#object_exists?(key) ⇒ Boolean
(also: #object_exist?)
判断一个object是否存在.
-
#object_url(key, sign = true, expiry = 60) ⇒ String
获取Object的URL.
-
#put_object(key, opts = {}) {|HTTP::StreamWriter| ... } ⇒ Object
向Bucket中上传一个object.
-
#referer ⇒ BucketReferer
获取Bucket的Referer配置.
-
#referer=(referer) ⇒ Object
设置Bucket的Referer配置.
-
#resumable_download(key, file, opts = {}) {|Float| ... } ⇒ Object
下载bucket中的一个object到本地文件,支持断点续传。指定的object 会被分成多个分片进行下载,只有所有的分片都下载成功整个object才 下载成功。对于每个下载的分片,会在file所在目录建立一个临时文件 file.part.N,下载成功后这些part文件会被合并成最后的file然后删 除。.
-
#resumable_upload(key, file, opts = {}) {|Float| ... } ⇒ Object
上传一个本地文件到bucket中的一个object,支持断点续传。指定的文 件会被分成多个分片进行上传,只有所有分片都上传成功整个文件才 上传成功。.
-
#set_object_acl(key, acl) ⇒ Object
设置object的ACL.
-
#sign(string_to_sign) ⇒ String
用ACCESS_KEY_SECRET对内容进行签名.
-
#update_object_metas(key, metas, conditions = {}) ⇒ Hash
更新Object的metas.
-
#website ⇒ BucketWebsite
获取Bucket的website配置.
-
#website=(website) ⇒ Object
设置Bucket的website配置.
Methods inherited from Common::Struct::Base
Methods included from Common::Struct::Base::AttrHelper
Constructor Details
#initialize(opts = {}, protocol = nil) ⇒ Bucket
Returns a new instance of Bucket.
15 16 17 18 |
# File 'lib/aliyun/oss/bucket.rb', line 15 def initialize(opts = {}, protocol = nil) super(opts) @protocol = protocol end |
Instance Method Details
#abort_upload(upload_id, key) ⇒ Object
取消一个multipart上传请求,一般用于清除Bucket下因断点上传而产生的文件碎片。成功取消后属于这个上传请求的分片都会被清除。
578 579 580 |
# File 'lib/aliyun/oss/bucket.rb', line 578 def abort_upload(upload_id, key) @protocol.abort_multipart_upload(name, key, upload_id) end |
#access_key_id ⇒ String
获取用户所设置的ACCESS_KEY_ID
631 632 633 |
# File 'lib/aliyun/oss/bucket.rb', line 631 def access_key_id @protocol.get_access_key_id end |
#acl ⇒ String
获取Bucket的ACL
24 25 26 |
# File 'lib/aliyun/oss/bucket.rb', line 24 def acl @protocol.get_bucket_acl(name) end |
#acl=(acl) ⇒ Object
设置Bucket的ACL
30 31 32 |
# File 'lib/aliyun/oss/bucket.rb', line 30 def acl=(acl) @protocol.put_bucket_acl(name, acl) end |
#append_object(key, pos, opts = {}) {|HTTP::StreamWriter| ... } ⇒ Integer
向Bucket中的object追加内容。如果object不存在,则创建一个Appendable Object。
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 |
# File 'lib/aliyun/oss/bucket.rb', line 334 def append_object(key, pos, opts = {}, &block) args = opts.dup file = args[:file] args[:content_type] ||= get_content_type(file) if file args[:content_type] ||= get_content_type(key) if file next_pos = @protocol.append_object(name, key, pos, args) do |sw| File.open(File.(file), 'rb') do |f| sw << f.read(Protocol::STREAM_CHUNK_SIZE) until f.eof? end end else next_pos = @protocol.append_object(name, key, pos, args, &block) end next_pos end |
#batch_delete_objects(keys, opts = {}) ⇒ Array<String>
批量删除object
394 395 396 397 |
# File 'lib/aliyun/oss/bucket.rb', line 394 def batch_delete_objects(keys, opts = {}) @protocol.batch_delete_objects( name, keys, opts.merge(encoding: KeyEncoding::URL)) end |
#bucket_url ⇒ String
获取Bucket的URL
584 585 586 |
# File 'lib/aliyun/oss/bucket.rb', line 584 def bucket_url @protocol.get_request_url(name) end |
#copy_object(source, dest, opts = {}) ⇒ Hash
将Bucket中的一个object拷贝成另外一个object
374 375 376 377 378 379 |
# File 'lib/aliyun/oss/bucket.rb', line 374 def copy_object(source, dest, opts = {}) args = opts.dup args[:content_type] ||= get_content_type(dest) @protocol.copy_object(name, source, dest, args) end |
#cors ⇒ Array<OSS::CORSRule>
获取Bucket的跨域资源共享(CORS)的规则
113 114 115 116 117 118 119 120 121 |
# File 'lib/aliyun/oss/bucket.rb', line 113 def cors begin r = @protocol.get_bucket_cors(name) rescue ServerError => e raise unless e.http_code == 404 end r || [] end |
#cors=(rules) ⇒ Object
如果rules为空,则会删除这个bucket上的CORS配置
设置Bucket的跨域资源共享(CORS)的规则
126 127 128 129 130 131 132 |
# File 'lib/aliyun/oss/bucket.rb', line 126 def cors=(rules) if rules.empty? @protocol.delete_bucket_cors(name) else @protocol.set_bucket_cors(name, rules) end end |
#delete_object(key) ⇒ Object
删除一个object
383 384 385 |
# File 'lib/aliyun/oss/bucket.rb', line 383 def delete_object(key) @protocol.delete_object(name, key) end |
#get_object(key, opts = {}) {|String| ... } ⇒ OSS::Object
如果opts中指定了‘:file`,则block会被忽略
如果既没有指定‘:file`也没有指定block,则只获取Object meta而不下载Object内容
从Bucket中下载一个object
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 |
# File 'lib/aliyun/oss/bucket.rb', line 261 def get_object(key, opts = {}, &block) obj = nil file = opts[:file] if file File.open(File.(file), 'wb') do |f| obj = @protocol.get_object(name, key, opts) do |chunk| f.write(chunk) end end elsif block obj = @protocol.get_object(name, key, opts, &block) else obj = @protocol.(name, key, opts) end obj end |
#get_object_acl(key) ⇒ String
获取object的ACL
409 410 411 |
# File 'lib/aliyun/oss/bucket.rb', line 409 def get_object_acl(key) @protocol.get_object_acl(name, key) end |
#get_object_cors(key) ⇒ OSS::CORSRule
获取object的CORS规则
416 417 418 |
# File 'lib/aliyun/oss/bucket.rb', line 416 def get_object_cors(key) @protocol.get_object_cors(name, key) end |
#lifecycle ⇒ Array<OSS::LifeCycleRule>
获取Bucket的生命周期配置
87 88 89 90 91 92 93 94 95 |
# File 'lib/aliyun/oss/bucket.rb', line 87 def lifecycle begin r = @protocol.get_bucket_lifecycle(name) rescue ServerError => e raise unless e.http_code == 404 end r || [] end |
#lifecycle=(rules) ⇒ Object
如果rules为空,则会删除这个bucket上的lifecycle配置
设置Bucket的生命周期配置
102 103 104 105 106 107 108 |
# File 'lib/aliyun/oss/bucket.rb', line 102 def lifecycle=(rules) if rules.empty? @protocol.delete_bucket_lifecycle(name) else @protocol.put_bucket_lifecycle(name, rules) end end |
#list_objects(opts = {}) ⇒ Enumerator<Object>
列出bucket中的object
167 168 169 170 |
# File 'lib/aliyun/oss/bucket.rb', line 167 def list_objects(opts = {}) Iterator::Objects.new( @protocol, name, opts.merge(encoding: KeyEncoding::URL)).to_enum end |
#list_uploads(opts = {}) ⇒ Enumerator<Multipart::Transaction>
列出此Bucket中正在进行的multipart上传请求,不包括已经完成或者被取消的。
568 569 570 571 |
# File 'lib/aliyun/oss/bucket.rb', line 568 def list_uploads(opts = {}) Iterator::Uploads.new( @protocol, name, opts.merge(encoding: KeyEncoding::URL)).to_enum end |
#logging ⇒ BucketLogging
获取Bucket的logging配置
36 37 38 |
# File 'lib/aliyun/oss/bucket.rb', line 36 def logging @protocol.get_bucket_logging(name) end |
#logging=(logging) ⇒ Object
设置Bucket的logging配置
42 43 44 45 46 47 48 |
# File 'lib/aliyun/oss/bucket.rb', line 42 def logging=(logging) if logging.enabled? @protocol.put_bucket_logging(name, logging) else @protocol.delete_bucket_logging(name) end end |
#object_exists?(key) ⇒ Boolean Also known as: object_exist?
判断一个object是否存在
298 299 300 301 302 303 304 305 306 307 308 |
# File 'lib/aliyun/oss/bucket.rb', line 298 def object_exists?(key) begin get_object(key) return true rescue ServerError => e return false if e.http_code == 404 raise e end false end |
#object_url(key, sign = true, expiry = 60) ⇒ String
获取Object的URL
593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 |
# File 'lib/aliyun/oss/bucket.rb', line 593 def object_url(key, sign = true, expiry = 60) url = @protocol.get_request_url(name, key) return url unless sign expires = Time.now.to_i + expiry query = { 'Expires' => expires.to_s, 'OSSAccessKeyId' => CGI.escape(access_key_id) } sub_res = [] if @protocol.get_sts_token sub_res << "security-token=#{@protocol.get_sts_token}" query['security-token'] = CGI.escape(@protocol.get_sts_token) end resource = "/#{name}/#{key}" unless sub_res.empty? resource << "?#{sub_res.join('&')}" end string_to_sign = "" << "GET\n" << # method "\n" << # Content-MD5 "\n" << # Content-Type "#{expires}\n" << "#{resource}" signature = sign(string_to_sign) query_string = query.merge('Signature' => CGI.escape(signature)) .map { |k, v| "#{k}=#{v}" }.join('&') [url, query_string].join('?') end |
#put_object(key, opts = {}) {|HTTP::StreamWriter| ... } ⇒ Object
如果opts中指定了‘:file`,则block会被忽略
如果指定了‘:callback`,则可能文件上传成功,但是callback 执行失败,此时会抛出CallbackError,用户可以选择接住这个异常,以忽略Callback调用错误
向Bucket中上传一个object
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 |
# File 'lib/aliyun/oss/bucket.rb', line 210 def put_object(key, opts = {}, &block) args = opts.dup file = args[:file] args[:content_type] ||= get_content_type(file) if file args[:content_type] ||= get_content_type(key) if file @protocol.put_object(name, key, args) do |sw| File.open(File.(file), 'rb') do |f| sw << f.read(Protocol::STREAM_CHUNK_SIZE) until f.eof? end end else @protocol.put_object(name, key, args, &block) end end |
#referer ⇒ BucketReferer
获取Bucket的Referer配置
74 75 76 |
# File 'lib/aliyun/oss/bucket.rb', line 74 def referer @protocol.get_bucket_referer(name) end |
#referer=(referer) ⇒ Object
设置Bucket的Referer配置
80 81 82 |
# File 'lib/aliyun/oss/bucket.rb', line 80 def referer=(referer) @protocol.put_bucket_referer(name, referer) end |
#resumable_download(key, file, opts = {}) {|Float| ... } ⇒ Object
已经下载的部分会在file所在的目录创建.part文件,命名方式为file.part.N
下载bucket中的一个object到本地文件,支持断点续传。指定的object 会被分成多个分片进行下载,只有所有的分片都下载成功整个object才下载成功。对于每个下载的分片,会在file所在目录建立一个临时文件file.part.N,下载成功后这些part文件会被合并成最后的file然后删除。
530 531 532 533 534 535 536 537 538 539 540 541 542 543 |
# File 'lib/aliyun/oss/bucket.rb', line 530 def resumable_download(key, file, opts = {}, &block) args = opts.dup args[:content_type] ||= get_content_type(file) args[:content_type] ||= get_content_type(key) cpt_file = args[:cpt_file] || get_cpt_file(file) Multipart::Download.new( @protocol, options: args, progress: block, object: key, bucket: name, creation_time: Time.now, file: File.(file), cpt_file: cpt_file ).run end |
#resumable_upload(key, file, opts = {}) {|Float| ... } ⇒ Object
如果指定了‘:callback`,则可能文件上传成功,但是callback 执行失败,此时会抛出CallbackError,用户可以选择接住这个异常,以忽略Callback调用错误
上传一个本地文件到bucket中的一个object,支持断点续传。指定的文件会被分成多个分片进行上传,只有所有分片都上传成功整个文件才上传成功。
474 475 476 477 478 479 480 481 482 483 484 485 486 487 |
# File 'lib/aliyun/oss/bucket.rb', line 474 def resumable_upload(key, file, opts = {}, &block) args = opts.dup args[:content_type] ||= get_content_type(file) args[:content_type] ||= get_content_type(key) cpt_file = args[:cpt_file] || get_cpt_file(file) Multipart::Upload.new( @protocol, options: args, progress: block, object: key, bucket: name, creation_time: Time.now, file: File.(file), cpt_file: cpt_file ).run end |
#set_object_acl(key, acl) ⇒ Object
设置object的ACL
402 403 404 |
# File 'lib/aliyun/oss/bucket.rb', line 402 def set_object_acl(key, acl) @protocol.put_object_acl(name, key, acl) end |
#sign(string_to_sign) ⇒ String
用ACCESS_KEY_SECRET对内容进行签名
638 639 640 |
# File 'lib/aliyun/oss/bucket.rb', line 638 def sign(string_to_sign) @protocol.sign(string_to_sign) end |
#update_object_metas(key, metas, conditions = {}) ⇒ Hash
更新Object的metas
287 288 289 290 291 292 293 |
# File 'lib/aliyun/oss/bucket.rb', line 287 def (key, , conditions = {}) @protocol.copy_object( name, key, key, :meta_directive => MetaDirective::REPLACE, :metas => , :condition => conditions) end |
#website ⇒ BucketWebsite
获取Bucket的website配置
52 53 54 55 56 57 58 59 60 |
# File 'lib/aliyun/oss/bucket.rb', line 52 def website begin w = @protocol.get_bucket_website(name) rescue ServerError => e raise unless e.http_code == 404 end w || BucketWebsite.new end |
#website=(website) ⇒ Object
设置Bucket的website配置
64 65 66 67 68 69 70 |
# File 'lib/aliyun/oss/bucket.rb', line 64 def website=(website) if website.enabled? @protocol.put_bucket_website(name, website) else @protocol.delete_bucket_website(name) end end |