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
初始化
29 30 31 32 |
# File 'lib/cos/file.rb', line 29 def initialize(attrs = {}) super(attrs) @type = 'file' end |
Instance Method Details
#complete? ⇒ Boolean
文件是否完整, 是否上传完了
84 85 86 |
# File 'lib/cos/file.rb', line 84 def complete? access_url != nil and filelen == filesize end |
#download(file_store, options = {}) {|Float| ... } ⇒ String
下载当前文件, 支持断点续传, 支持多线程
131 132 133 |
# File 'lib/cos/file.rb', line 131 def download(file_store, = {}, &block) bucket.download(self, file_store, , &block) end |
#filelen ⇒ Integer
获取文件大小并转为数值型
44 45 46 |
# File 'lib/cos/file.rb', line 44 def filelen @filelen.to_i end |
#filesize ⇒ Integer
获取文件存储大小并转为数值型
37 38 39 |
# File 'lib/cos/file.rb', line 37 def filesize @filesize.to_i end |
#format_size ⇒ String
获取格式化的文件大小
69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/cos/file.rb', line 69 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是否一致
51 52 53 54 |
# File 'lib/cos/file.rb', line 51 def sha1_match?(file) file = File.(file) File.exist?(file) and sha.upcase == Util.file_sha1(file).upcase end |
#size ⇒ Object
文件大小
59 60 61 |
# File 'lib/cos/file.rb', line 59 def size filesize.to_i end |
#url(options = {}) ⇒ String
Note:
私有读取的bucket会自动生成带签名的URL
获取文件的URL, 支持cname, https
100 101 102 |
# File 'lib/cos/file.rb', line 100 def url( = {}) bucket.url(self, ) end |