Class: Aliyun::OSS::Bucket
- Inherits:
-
Struct::Base
- Object
- 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
-
#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.
-
#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 Struct::Base
Methods included from 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
#access_key_id ⇒ String
获取用户所设置的ACCESS_KEY_ID
525 526 527 |
# File 'lib/aliyun/oss/bucket.rb', line 525 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。
311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 |
# File 'lib/aliyun/oss/bucket.rb', line 311 def append_object(key, pos, opts = {}, &block) next_pos = -1 file = opts[:file] if file opts[:content_type] = get_content_type(file) next_pos = @protocol.append_object(name, key, pos, opts) 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, opts, &block) end next_pos end |
#batch_delete_objects(keys, opts = {}) ⇒ Array<String>
批量删除object
366 367 368 |
# File 'lib/aliyun/oss/bucket.rb', line 366 def batch_delete_objects(keys, opts = {}) @protocol.batch_delete_objects(name, keys, opts) end |
#bucket_url ⇒ String
获取Bucket的URL
494 495 496 |
# File 'lib/aliyun/oss/bucket.rb', line 494 def bucket_url @protocol.get_request_url(name) end |
#copy_object(source, dest, opts = {}) ⇒ Hash
将Bucket中的一个object拷贝成另外一个object
347 348 349 |
# File 'lib/aliyun/oss/bucket.rb', line 347 def copy_object(source, dest, opts = {}) @protocol.copy_object(name, source, dest, opts) 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
353 354 355 |
# File 'lib/aliyun/oss/bucket.rb', line 353 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
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 |
# File 'lib/aliyun/oss/bucket.rb', line 240 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
380 381 382 |
# File 'lib/aliyun/oss/bucket.rb', line 380 def get_object_acl(key) @protocol.get_object_acl(name, key) end |
#get_object_cors(key) ⇒ OSS::CORSRule
获取object的CORS规则
387 388 389 |
# File 'lib/aliyun/oss/bucket.rb', line 387 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 |
# File 'lib/aliyun/oss/bucket.rb', line 167 def list_objects(opts = {}) Iterator::Objects.new(@protocol, name, opts).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是否存在
277 278 279 280 281 282 283 284 285 286 287 |
# File 'lib/aliyun/oss/bucket.rb', line 277 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
503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 |
# File 'lib/aliyun/oss/bucket.rb', line 503 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 string_to_sign = "GET\n" + "\n\n" + "#{expires}\n" + "/#{name}/#{key}" signature = sign(string_to_sign) query_string = { 'Expires' => expires.to_s, 'OSSAccessKeyId' => CGI.escape(access_key_id), 'Signature' => CGI.escape(signature) }.map { |k, v| "#{k}=#{v}" }.join('&') [url, query_string].join('?') end |
#put_object(key, opts = {}) {|HTTP::StreamWriter| ... } ⇒ Object
采用streaming的方式时,提供的数据必须是有结束标记的数据。 因为put_object会不断地从StreamWriter中读取数据上传到OSS,直到 它读到的数据为nil停止。
如果opts中指定了:file,则block会被忽略
向Bucket中上传一个object
194 195 196 197 198 199 200 201 202 203 204 205 206 207 |
# File 'lib/aliyun/oss/bucket.rb', line 194 def put_object(key, opts = {}, &block) file = opts[:file] if file opts[:content_type] = get_content_type(file) @protocol.put_object(name, key, opts) 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, opts, &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然后删 除。
479 480 481 482 483 484 485 486 487 488 489 490 |
# File 'lib/aliyun/oss/bucket.rb', line 479 def resumable_download(key, file, opts = {}, &block) unless cpt_file = opts[:cpt_file] cpt_file = get_cpt_file(file) end Multipart::Download.new( @protocol, options: opts, 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
上传一个本地文件到bucket中的一个object,支持断点续传。指定的文 件会被分成多个分片进行上传,只有所有分片都上传成功整个文件才 上传成功。
427 428 429 430 431 432 433 434 435 436 437 438 |
# File 'lib/aliyun/oss/bucket.rb', line 427 def resumable_upload(key, file, opts = {}, &block) unless cpt_file = opts[:cpt_file] cpt_file = get_cpt_file(file) end Multipart::Upload.new( @protocol, options: opts, 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
373 374 375 |
# File 'lib/aliyun/oss/bucket.rb', line 373 def set_object_acl(key, acl) @protocol.put_object_acl(name, key, acl) end |
#sign(string_to_sign) ⇒ String
用ACCESS_KEY_SECRET对内容进行签名
532 533 534 |
# File 'lib/aliyun/oss/bucket.rb', line 532 def sign(string_to_sign) @protocol.sign(string_to_sign) end |
#update_object_metas(key, metas, conditions = {}) ⇒ Hash
更新Object的metas
266 267 268 269 270 271 272 |
# File 'lib/aliyun/oss/bucket.rb', line 266 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 |