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.
-
#download_crc_enable ⇒ Object
Get the download crc status.
-
#encryption ⇒ BucketEncryption
获取Bucket的encryption配置.
-
#encryption=(encryption) ⇒ Object
设置Bucket的encryption配置.
-
#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, parameters = {}) ⇒ 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.
-
#upload_crc_enable ⇒ Object
Get the upload crc status.
-
#versioning ⇒ BucketVersioning
获取Bucket的versioning配置.
-
#versioning=(versioning) ⇒ Object
设置Bucket的versioning配置.
-
#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下因断点上传而产 生的文件碎片。成功取消后属于这个上传请求的分片都会被清除。
606 607 608 |
# File 'lib/aliyun/oss/bucket.rb', line 606 def abort_upload(upload_id, key) @protocol.abort_multipart_upload(name, key, upload_id) end |
#access_key_id ⇒ String
获取用户所设置的ACCESS_KEY_ID
656 657 658 |
# File 'lib/aliyun/oss/bucket.rb', line 656 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。
362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 |
# File 'lib/aliyun/oss/bucket.rb', line 362 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
422 423 424 425 |
# File 'lib/aliyun/oss/bucket.rb', line 422 def batch_delete_objects(keys, opts = {}) @protocol.batch_delete_objects( name, keys, opts.merge(encoding: KeyEncoding::URL)) end |
#bucket_url ⇒ String
获取Bucket的URL
612 613 614 |
# File 'lib/aliyun/oss/bucket.rb', line 612 def bucket_url @protocol.get_request_url(name) end |
#copy_object(source, dest, opts = {}) ⇒ Hash
将Bucket中的一个object拷贝成另外一个object
402 403 404 405 406 407 |
# File 'lib/aliyun/oss/bucket.rb', line 402 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)的规则
141 142 143 144 145 146 147 148 149 |
# File 'lib/aliyun/oss/bucket.rb', line 141 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)的规则
154 155 156 157 158 159 160 |
# File 'lib/aliyun/oss/bucket.rb', line 154 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
411 412 413 |
# File 'lib/aliyun/oss/bucket.rb', line 411 def delete_object(key) @protocol.delete_object(name, key) end |
#download_crc_enable ⇒ Object
Get the download crc status
669 670 671 |
# File 'lib/aliyun/oss/bucket.rb', line 669 def download_crc_enable @protocol.download_crc_enable end |
#encryption ⇒ BucketEncryption
获取Bucket的encryption配置
64 65 66 |
# File 'lib/aliyun/oss/bucket.rb', line 64 def encryption @protocol.get_bucket_encryption(name) end |
#encryption=(encryption) ⇒ Object
设置Bucket的encryption配置
70 71 72 73 74 75 76 |
# File 'lib/aliyun/oss/bucket.rb', line 70 def encryption=(encryption) if encryption.enabled? @protocol.put_bucket_encryption(name, encryption) else @protocol.delete_bucket_encryption(name) end end |
#get_object(key, opts = {}) {|String| ... } ⇒ OSS::Object
如果opts中指定了‘:file`,则block会被忽略
如果既没有指定‘:file`也没有指定block,则只获取Object meta而不下载Object内容
从Bucket中下载一个object
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 |
# File 'lib/aliyun/oss/bucket.rb', line 289 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
437 438 439 |
# File 'lib/aliyun/oss/bucket.rb', line 437 def get_object_acl(key) @protocol.get_object_acl(name, key) end |
#get_object_cors(key) ⇒ OSS::CORSRule
获取object的CORS规则
444 445 446 |
# File 'lib/aliyun/oss/bucket.rb', line 444 def get_object_cors(key) @protocol.get_object_cors(name, key) end |
#lifecycle ⇒ Array<OSS::LifeCycleRule>
获取Bucket的生命周期配置
115 116 117 118 119 120 121 122 123 |
# File 'lib/aliyun/oss/bucket.rb', line 115 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的生命周期配置
130 131 132 133 134 135 136 |
# File 'lib/aliyun/oss/bucket.rb', line 130 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
195 196 197 198 |
# File 'lib/aliyun/oss/bucket.rb', line 195 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上传请求,不包括已经完成或者 被取消的。
596 597 598 599 |
# File 'lib/aliyun/oss/bucket.rb', line 596 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是否存在
326 327 328 329 330 331 332 333 334 335 336 |
# File 'lib/aliyun/oss/bucket.rb', line 326 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, parameters = {}) ⇒ String
获取Object的URL
622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 |
# File 'lib/aliyun/oss/bucket.rb', line 622 def object_url(key, sign = true, expiry = 60, parameters = {}) url = @protocol.get_request_url(name, key).gsub('%2F', '/') query = parameters.dup if sign #header expires = Time.now.to_i + expiry headers = { 'date' => expires.to_s, } #query if @protocol.get_sts_token query['security-token'] = @protocol.get_sts_token end res = { :path => @protocol.get_resource_path(name, key), :sub_res => query, } signature = Util.get_signature(@protocol.get_access_key_secret, 'GET', headers, res) query['Expires'] = expires.to_s query['OSSAccessKeyId'] = @protocol.get_access_key_id query['Signature'] = signature end query_string = query.map { |k, v| v ? [k, CGI.escape(v)].join("=") : k }.join("&") link_char = query_string.empty? ? '' : '?' [url, query_string].join(link_char) end |
#put_object(key, opts = {}) {|HTTP::StreamWriter| ... } ⇒ Object
如果opts中指定了‘:file`,则block会被忽略
如果指定了‘:callback`,则可能文件上传成功,但是callback 执行失败,此时会抛出CallbackError,用户可以选择接住这 个异常,以忽略Callback调用错误
向Bucket中上传一个object
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 |
# File 'lib/aliyun/oss/bucket.rb', line 238 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配置
102 103 104 |
# File 'lib/aliyun/oss/bucket.rb', line 102 def referer @protocol.get_bucket_referer(name) end |
#referer=(referer) ⇒ Object
设置Bucket的Referer配置
108 109 110 |
# File 'lib/aliyun/oss/bucket.rb', line 108 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然后删 除。
558 559 560 561 562 563 564 565 566 567 568 569 570 571 |
# File 'lib/aliyun/oss/bucket.rb', line 558 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,支持断点续传。指定的文 件会被分成多个分片进行上传,只有所有分片都上传成功整个文件才 上传成功。
502 503 504 505 506 507 508 509 510 511 512 513 514 515 |
# File 'lib/aliyun/oss/bucket.rb', line 502 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
430 431 432 |
# File 'lib/aliyun/oss/bucket.rb', line 430 def set_object_acl(key, acl) @protocol.put_object_acl(name, key, acl) end |
#sign(string_to_sign) ⇒ String
用ACCESS_KEY_SECRET对内容进行签名
663 664 665 |
# File 'lib/aliyun/oss/bucket.rb', line 663 def sign(string_to_sign) @protocol.sign(string_to_sign) end |
#update_object_metas(key, metas, conditions = {}) ⇒ Hash
更新Object的metas
315 316 317 318 319 320 321 |
# File 'lib/aliyun/oss/bucket.rb', line 315 def (key, , conditions = {}) @protocol.copy_object( name, key, key, :meta_directive => MetaDirective::REPLACE, :metas => , :condition => conditions) end |
#upload_crc_enable ⇒ Object
Get the upload crc status
675 676 677 |
# File 'lib/aliyun/oss/bucket.rb', line 675 def upload_crc_enable @protocol.upload_crc_enable end |
#versioning ⇒ BucketVersioning
获取Bucket的versioning配置
52 53 54 |
# File 'lib/aliyun/oss/bucket.rb', line 52 def versioning @protocol.get_bucket_versioning(name) end |
#versioning=(versioning) ⇒ Object
设置Bucket的versioning配置
58 59 60 |
# File 'lib/aliyun/oss/bucket.rb', line 58 def versioning=(versioning) @protocol.put_bucket_versioning(name, versioning) end |
#website ⇒ BucketWebsite
获取Bucket的website配置
80 81 82 83 84 85 86 87 88 |
# File 'lib/aliyun/oss/bucket.rb', line 80 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配置
92 93 94 95 96 97 98 |
# File 'lib/aliyun/oss/bucket.rb', line 92 def website=(website) if website.enabled? @protocol.put_bucket_website(name, website) else @protocol.delete_bucket_website(name) end end |