Class: COS::COSFile
- Inherits:
-
ResourceOperator
- Object
- Struct::Base
- ResourceOperator
- COS::COSFile
- Defined in:
- lib/cos/file.rb
Overview
COS文件资源
Constant Summary collapse
- STORAGE_UNITS =
%w[B KB MB GB]- STORAGE_BASE =
1024
Instance Attribute Summary
Attributes inherited from ResourceOperator
Instance Method Summary collapse
-
#complete? ⇒ Boolean
文件是否完整, 是否上传完了.
-
#download(file_store, options = {}) {|Float| ... } ⇒ String
下载当前文件, 支持断点续传, 支持多线程.
-
#filelen ⇒ Integer
获取文件大小并转为数值型.
-
#filesize ⇒ Integer
获取文件存储大小并转为数值型.
-
#format_size ⇒ String
获取格式化的文件大小.
-
#initialize(attrs = {}) ⇒ COS::COSFile
constructor
初始化.
-
#sha1_match?(file) ⇒ Boolean
判断文件sha1是否一致.
-
#size ⇒ Object
文件大小.
-
#url(options = {}) ⇒ String
获取文件的URL, 支持cname, https.
Methods inherited from ResourceOperator
#created_at, #delete, #delete!, #exist?, #stat, #to_hash, #update, #updated_at
Methods included from Struct::Base::AttrHelper
#optional_attrs, #required_attrs
Constructor Details
#initialize(attrs = {}) ⇒ COS::COSFile
初始化
28 29 30 31 |
# File 'lib/cos/file.rb', line 28 def initialize(attrs = {}) super(attrs) @type = 'file' end |
Instance Method Details
#complete? ⇒ Boolean
文件是否完整, 是否上传完了
83 84 85 |
# File 'lib/cos/file.rb', line 83 def complete? access_url != nil and filelen == filesize end |
#download(file_store, options = {}) {|Float| ... } ⇒ String
下载当前文件, 支持断点续传, 支持多线程
130 131 132 |
# File 'lib/cos/file.rb', line 130 def download(file_store, = {}, &block) bucket.download(self, file_store, , &block) end |
#filelen ⇒ Integer
获取文件大小并转为数值型
43 44 45 |
# File 'lib/cos/file.rb', line 43 def filelen @filelen.to_i end |
#filesize ⇒ Integer
获取文件存储大小并转为数值型
36 37 38 |
# File 'lib/cos/file.rb', line 36 def filesize @filesize.to_i end |
#format_size ⇒ String
获取格式化的文件大小
68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/cos/file.rb', line 68 def format_size if filesize.to_i < STORAGE_BASE size_str = filesize.to_s + STORAGE_UNITS[0] else c_size = human_rep(filesize.to_i) size_str = "%.2f" % c_size[:size].round(2) size_str = "#{size_str}#{c_size[:unit]}" end size_str end |
#sha1_match?(file) ⇒ Boolean
判断文件sha1是否一致
50 51 52 53 |
# File 'lib/cos/file.rb', line 50 def sha1_match?(file) file = File.(file) File.exist?(file) and sha.upcase == Util.file_sha1(file).upcase end |
#size ⇒ Object
文件大小
58 59 60 |
# File 'lib/cos/file.rb', line 58 def size filesize.to_i end |
#url(options = {}) ⇒ String
Note:
私有读取的bucket会自动生成带签名的URL
获取文件的URL, 支持cname, https
99 100 101 |
# File 'lib/cos/file.rb', line 99 def url( = {}) bucket.url(self, ) end |