Class: COS::Bucket
Constant Summary collapse
- MIN_UPLOAD_SLICE_SIZE =
最小完整上传大小
10 * 1024 * 1024
- MIN_DOWNLOAD_SLICE_SIZE =
最小下载分块大小
5 * 1024 * 1024
- DEFAULT_UPLOAD_RETRY =
默认上传重试次数
10
- DEFAULT_DOWNLOAD_RETRY =
默认下载重试次数
10
Constants included from Logging
Logging::DEFAULT_LOG_FILE, Logging::MAX_NUM_LOG, Logging::ROTATE_SIZE
Instance Attribute Summary collapse
-
#authority ⇒ Object
readonly
Returns the value of attribute authority.
-
#blackrefers ⇒ Object
readonly
Returns the value of attribute blackrefers.
-
#brower_exec ⇒ Object
readonly
Returns the value of attribute brower_exec.
-
#bucket_name ⇒ Object
readonly
Returns the value of attribute bucket_name.
-
#bucket_type ⇒ Object
readonly
Returns the value of attribute bucket_type.
-
#client ⇒ Object
readonly
Returns the value of attribute client.
-
#cnames ⇒ Object
readonly
Returns the value of attribute cnames.
-
#migrate_source_domain ⇒ Object
readonly
Returns the value of attribute migrate_source_domain.
-
#need_preview ⇒ Object
readonly
Returns the value of attribute need_preview.
-
#nugc_flag ⇒ Object
readonly
Returns the value of attribute nugc_flag.
-
#refers ⇒ Object
readonly
Returns the value of attribute refers.
Instance Method Summary collapse
-
#complete?(path) ⇒ Boolean
判断文件是否上传完成.
-
#count(path = '') ⇒ Integer
(also: #size)
获取文件及目录总数.
-
#count_dirs(path = '') ⇒ Integer
获取目录数.
-
#count_files(path = '') ⇒ Integer
获取文件数.
-
#create_folder(path, options = {}) ⇒ COS::COSDir
(also: #mkdir)
创建目录.
-
#delete(path) ⇒ Object
删除文件或目录.
-
#delete!(path) ⇒ Object
删除文件或目录, 不会抛出异常而是返回布尔值.
-
#download(path_or_file, file_store, options = {}) {|Float| ... } ⇒ String
下载文件, 支持断点续传, 支持多线程.
-
#download_all(path_or_dir, file_store_path, options = {}) {|Float| ... } ⇒ Array<String>
批量下载目录下的全部文件(不包含子目录).
-
#empty?(path = '') ⇒ Boolean
目录是否是空的.
-
#exist?(path) ⇒ Boolean
(also: #exists?)
文件或目录是否存在.
-
#hash_tree(path_or_dir = '', options = {}) ⇒ Hash<Object>
获取Hash格式的目录树形结构, 可用于直接to_json.
-
#initialize(client, bucket_name = nil) ⇒ COS::Bucket
constructor
初始化.
-
#list(path = '', options = {}) ⇒ Enumerator<Object>
(also: #ls)
列出目录.
-
#list_count(path = '', options = {}) ⇒ Hash
获取list中的文件及目录个数.
-
#stat(path = '') ⇒ COSFile|COSDir
获取文件或目录信息.
-
#tree(path_or_dir = '', options = {}) ⇒ Hash
获取目录树形结构.
-
#update(path, biz_attr) ⇒ Object
更新文件及目录业务属性.
-
#upload(path_or_dir, file_name, file_src, options = {}) {|Float| ... } ⇒ COS::COSFile
上传文件, 大文件自动断点续传, 多线程上传.
-
#upload_all(path_or_dir, file_src_path, options = {}) {|Float| ... } ⇒ Array<COS::COSFile>
批量上传目录下的全部文件(不包含子目录).
-
#url(path_or_file, options = {}) ⇒ String
获取文件可访问的URL.
Methods included from Logging
Constructor Details
#initialize(client, bucket_name = nil) ⇒ COS::Bucket
SDK会自动获取bucket的信息,包括读取权限等并进行缓存 如需在后台修改了bucket信息请重新初始化Client
初始化
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/cos/bucket.rb', line 40 def initialize(client, bucket_name = nil) @client = client @bucket_name = client.config.get_bucket(bucket_name) # 使用stat API 获取根目录信息可获取到bucket信息 data = client.api.stat('/', bucket: bucket_name) @authority = data[:authority] @bucket_type = data[:bucket_type] @need_preview = data[:need_preview] @refers = data[:refers] @migrate_source_domain = data[:migrate_source_domain] @blackrefers = data[:blackrefers] @brower_exec = data[:brower_exec] @cnames = data[:cnames] @nugc_flag = data[:nugc_flag] end |
Instance Attribute Details
#authority ⇒ Object (readonly)
Returns the value of attribute authority.
11 12 13 |
# File 'lib/cos/bucket.rb', line 11 def @authority end |
#blackrefers ⇒ Object (readonly)
Returns the value of attribute blackrefers.
11 12 13 |
# File 'lib/cos/bucket.rb', line 11 def blackrefers @blackrefers end |
#brower_exec ⇒ Object (readonly)
Returns the value of attribute brower_exec.
11 12 13 |
# File 'lib/cos/bucket.rb', line 11 def brower_exec @brower_exec end |
#bucket_name ⇒ Object (readonly)
Returns the value of attribute bucket_name.
11 12 13 |
# File 'lib/cos/bucket.rb', line 11 def bucket_name @bucket_name end |
#bucket_type ⇒ Object (readonly)
Returns the value of attribute bucket_type.
11 12 13 |
# File 'lib/cos/bucket.rb', line 11 def bucket_type @bucket_type end |
#client ⇒ Object (readonly)
Returns the value of attribute client.
11 12 13 |
# File 'lib/cos/bucket.rb', line 11 def client @client end |
#cnames ⇒ Object (readonly)
Returns the value of attribute cnames.
11 12 13 |
# File 'lib/cos/bucket.rb', line 11 def cnames @cnames end |
#migrate_source_domain ⇒ Object (readonly)
Returns the value of attribute migrate_source_domain.
11 12 13 |
# File 'lib/cos/bucket.rb', line 11 def migrate_source_domain @migrate_source_domain end |
#need_preview ⇒ Object (readonly)
Returns the value of attribute need_preview.
11 12 13 |
# File 'lib/cos/bucket.rb', line 11 def need_preview @need_preview end |
#nugc_flag ⇒ Object (readonly)
Returns the value of attribute nugc_flag.
11 12 13 |
# File 'lib/cos/bucket.rb', line 11 def nugc_flag @nugc_flag end |
#refers ⇒ Object (readonly)
Returns the value of attribute refers.
11 12 13 |
# File 'lib/cos/bucket.rb', line 11 def refers @refers end |
Instance Method Details
#complete?(path) ⇒ Boolean
判断文件是否上传完成
425 426 427 |
# File 'lib/cos/bucket.rb', line 425 def complete?(path) get_file(path).complete? end |
#count(path = '') ⇒ Integer Also known as: size
获取文件及目录总数
113 114 115 116 |
# File 'lib/cos/bucket.rb', line 113 def count(path = '') lc = list_count(path) lc[:total] end |
#count_dirs(path = '') ⇒ Integer
获取目录数
139 140 141 142 |
# File 'lib/cos/bucket.rb', line 139 def count_dirs(path = '') lc = list_count(path) lc[:dirs] end |
#count_files(path = '') ⇒ Integer
获取文件数
127 128 129 130 |
# File 'lib/cos/bucket.rb', line 127 def count_files(path = '') lc = list_count(path) lc[:files] end |
#create_folder(path, options = {}) ⇒ COS::COSDir Also known as: mkdir
创建目录
68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/cos/bucket.rb', line 68 def create_folder(path, = {}) data = client.api.create_folder(path, .merge({bucket: bucket_name})) dir = { mtime: data[:mtime], ctime: data[:ctime], name: data[:name], biz_attr: [:biz_attr], bucket: self, path: path } COSDir.new(dir) end |
#delete(path) ⇒ Object
非空目录及根目录不可删除,会抛出异常
删除文件或目录
359 360 361 |
# File 'lib/cos/bucket.rb', line 359 def delete(path) client.api.delete(path, bucket: bucket_name) end |
#delete!(path) ⇒ Object
非空目录及根目录不可删除, 返回false
删除文件或目录, 不会抛出异常而是返回布尔值
371 372 373 374 375 376 |
# File 'lib/cos/bucket.rb', line 371 def delete!(path) delete(path) true rescue false end |
#download(path_or_file, file_store, options = {}) {|Float| ... } ⇒ String
下载文件, 支持断点续传, 支持多线程
506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 |
# File 'lib/cos/bucket.rb', line 506 def download(path_or_file, file_store, = {}, &block) min_size = [:min_slice_size] || MIN_DOWNLOAD_SLICE_SIZE retry_times = [:download_retry] || DEFAULT_DOWNLOAD_RETRY # 如果传入的是一个路径需要先获取文件信息 file = get_file(path_or_file) # 检查文件是否上传完整才能下载 unless file.complete? raise FileUploadNotComplete, 'file upload not complete' end # 检查本地文件sha1是否一致, 如一致就已下载完成了 if file.sha1_match?(file_store) logger.info("File #{file_store} exist and sha1 match, skip download.") return file_store end retry_loop(retry_times) do if file.filesize > min_size # 分块下载 Download.new( bucket: self, cos_file: file, file_store: file_store, options: , progress: block ).download else # 直接下载 client.api.download(file.access_url, file_store, bucket: bucket_name) end end # 返回本地文件路径 file_store end |
#download_all(path_or_dir, file_store_path, options = {}) {|Float| ... } ⇒ Array<String>
sdk会自动创建本地目录
批量下载目录下的全部文件(不包含子目录)
582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 |
# File 'lib/cos/bucket.rb', line 582 def download_all(path_or_dir, file_store_path, = {}, &block) local_path = Util.get_local_path(file_store_path, [:disable_mkdir]) dir = get_dir(path_or_dir) downloaded = [] # 遍历目录下的所有文件 dir.list(pattern: :file_only).each do |file| logger.info("Begin to download file >> #{file.name}") downloaded << file.download("#{local_path}/#{file.name}", , &block) logger.info("#{file.name} download finished") end downloaded end |
#empty?(path = '') ⇒ Boolean
目录是否是空的
388 389 390 |
# File 'lib/cos/bucket.rb', line 388 def empty?(path = '') count(path) == 0 end |
#exist?(path) ⇒ Boolean Also known as: exists?
文件或目录是否存在
402 403 404 405 406 407 408 409 410 411 |
# File 'lib/cos/bucket.rb', line 402 def exist?(path) begin stat(path) rescue ServerError => e return false if e.error_code == -166 raise e end true end |
#hash_tree(path_or_dir = '', options = {}) ⇒ Hash<Object>
获取Hash格式的目录树形结构, 可用于直接to_json
:resource => {name: '', mtime: ''...,
:children => [
=> resource, :children => [...],
=> resource, :children => [...],
...
]
}
652 653 654 655 |
# File 'lib/cos/bucket.rb', line 652 def hash_tree(path_or_dir = '', = {}) dir = get_dir(path_or_dir) Tree.new(.merge({path: dir})).to_hash end |
#list(path = '', options = {}) ⇒ Enumerator<Object> Also known as: ls
列出目录
168 169 170 |
# File 'lib/cos/bucket.rb', line 168 def list(path = '', = {}) Resource.new(self, path, ).to_enum end |
#list_count(path = '', options = {}) ⇒ Hash
获取list中的文件及目录个数
98 99 100 101 102 103 104 |
# File 'lib/cos/bucket.rb', line 98 def list_count(path = '', = {}) = {} result = client.api.list(path, .merge({num: 1, bucket: bucket_name})) total = result[:filecount] + result[:dircount] {total: total, files: result[:filecount], dirs: result[:dircount]} end |
#stat(path = '') ⇒ COSFile|COSDir
如查询根目录(‘/’, ”)可以获取到bucket信息, 返回COSDir
获取文件或目录信息
321 322 323 324 325 326 327 328 329 330 331 332 333 334 |
# File 'lib/cos/bucket.rb', line 321 def stat(path = '') data = client.api.stat(path, bucket: bucket_name) # 查询'/'获取的是bucket信息, 无name参数, 需要补全 data[:name] = '' if data[:name].nil? if data[:filesize].nil? # 目录 COSDir.new(data.merge({bucket: self, path: path})) else # 文件 COSFile.new(data.merge({bucket: self, path: path})) end end |
#tree(path_or_dir = '', options = {}) ⇒ Hash
获取目录树形结构
:resource => resource,
:children => [
{:resource => resource, :children => [...],
=> resource, :children => [...],
...
]
}
625 626 627 628 |
# File 'lib/cos/bucket.rb', line 625 def tree(path_or_dir = '', = {}) dir = get_dir(path_or_dir) Tree.new(.merge({path: dir})).to_object end |
#update(path, biz_attr) ⇒ Object
更新文件及目录业务属性
345 346 347 |
# File 'lib/cos/bucket.rb', line 345 def update(path, biz_attr) client.api.update(path, biz_attr, bucket: bucket_name) end |
#upload(path_or_dir, file_name, file_src, options = {}) {|Float| ... } ⇒ COS::COSFile
上传文件, 大文件自动断点续传, 多线程上传
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 |
# File 'lib/cos/bucket.rb', line 210 def upload(path_or_dir, file_name, file_src, = {}, &block) dir = get_dir(path_or_dir, [:auto_create_folder]) min_size = [:min_slice_size] || MIN_UPLOAD_SLICE_SIZE retry_times = [:upload_retry] || DEFAULT_UPLOAD_RETRY if min_size > 20 * 1024 * 1024 raise EntireUploadTooLarge, 'entire upload file too large, file size must < 20MB' end .merge!({bucket: bucket_name}) file_src = File.(file_src) file_size = File.size(file_src) retry_loop(retry_times) do if file_size > min_size # 分块上传 client.api.upload_slice(dir.path, file_name, file_src, , &block) else # 完整上传 client.api.upload(dir.path, file_name, file_src, ) end end # 获取上传完成文件的状态, 只会返回<COSFile> stat(Util.get_list_path(dir.path, file_name, true)) end |
#upload_all(path_or_dir, file_src_path, options = {}) {|Float| ... } ⇒ Array<COS::COSFile>
已存在的文件不会再次上传, 本地目录中的隐藏文件(已“.”开头的)不会上传 “.cpt”文件不会上传, 不会上传子目录
批量上传目录下的全部文件(不包含子目录)
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 |
# File 'lib/cos/bucket.rb', line 278 def upload_all(path_or_dir, file_src_path, = {}, &block) local_path = Util.get_local_path(file_src_path, true) uploaded = [] Dir.foreach(local_path) do |file| if !file.start_with?('.') and !file.end_with?('.cpt') and !File.directory?(file) logger.info("Begin to upload file >> #{file}") begin # 逐个上传 uploaded << upload(path_or_dir, file, "#{local_path}/#{file}", , &block) rescue => error # 跳过错误 if [:skip_error] logger.info("#{file} error skipped") next else # 终止上传抛出异常 raise error end end logger.info("#{file} upload finished") end end uploaded end |
#url(path_or_file, options = {}) ⇒ String
私有读取的bucket会自动生成带签名的URL
获取文件可访问的URL
445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 |
# File 'lib/cos/bucket.rb', line 445 def url(path_or_file, = {}) file = get_file(path_or_file) url = file.access_url # 使用cname if [:cname] host = URI.parse(url).host.downcase url.gsub!(host, [:cname]) end # 使用https if [:https] url.gsub!('http://', 'https://') end if == 'eWRPrivate' # 私有读取的bucket自动生成带签名的URL sign = client.signature.multiple( bucket_name, [:expire_seconds] || client.config.multiple_sign_expire) "#{url}?sign=#{sign}" else url end end |