Class: CarrierWave::Storage::Aliyun::Connection
- Inherits:
-
Object
- Object
- CarrierWave::Storage::Aliyun::Connection
- Defined in:
- lib/carrierwave/storage/aliyun.rb
Instance Method Summary collapse
-
#delete(path) ⇒ Object
删除 Remote 的文件.
-
#get(path) ⇒ Object
读取文件 params: - path - remote 存储路径 returns: file data.
-
#initialize(uploader) ⇒ Connection
constructor
A new instance of Connection.
-
#path_to_url(path) ⇒ Object
根据配置返回完整的上传文件的访问地址.
-
#private_get_url(path) ⇒ Object
私有空间访问地址,会带上实时算出的 token 信息 有效期 3600s.
-
#put(path, file, options = {}) ⇒ Object
上传文件 params: - path - remote 存储路径 - file - 需要上传文件的 File 对象 - options: - content_type - 上传文件的 MimeType,默认 ‘image/jpg` returns: 图片的下载地址.
Constructor Details
#initialize(uploader) ⇒ Connection
Returns a new instance of Connection.
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/carrierwave/storage/aliyun.rb', line 11 def initialize(uploader) @uploader = uploader @aliyun_access_id = uploader.aliyun_access_id @aliyun_access_key = uploader.aliyun_access_key @aliyun_bucket = uploader.aliyun_bucket @aliyun_area = uploader.aliyun_area || 'cn-hangzhou' @aliyun_private_read = uploader.aliyun_private_read # Host for get request @aliyun_host = uploader.aliyun_host || "http://#{@aliyun_bucket}.oss-#{@aliyun_area}.aliyuncs.com" if not @aliyun_host.include?("//") raise "config.aliyun_host requirement include // http:// or https://, but you give: #{@aliyun_host}" end end |
Instance Method Details
#delete(path) ⇒ Object
删除 Remote 的文件
params:
-
path - remote 存储路径
returns: 图片的下载地址
71 72 73 74 75 76 77 78 79 |
# File 'lib/carrierwave/storage/aliyun.rb', line 71 def delete(path) path.sub!(/^\//, '') res = oss_upload_client.bucket_delete_object(path) if res.success? return path_to_url(path) else raise "Delete failed" end end |
#get(path) ⇒ Object
读取文件params:
-
path - remote 存储路径
returns: file data
54 55 56 57 58 59 60 61 62 |
# File 'lib/carrierwave/storage/aliyun.rb', line 54 def get(path) path.sub!(/^\//, '') res = oss_upload_client.bucket_get_object(path) if res.success? return res.parsed_response else raise "Get content faild" end end |
#path_to_url(path) ⇒ Object
根据配置返回完整的上传文件的访问地址
83 84 85 |
# File 'lib/carrierwave/storage/aliyun.rb', line 83 def path_to_url(path) [@aliyun_host, path].join("/") end |
#private_get_url(path) ⇒ Object
私有空间访问地址,会带上实时算出的 token 信息有效期 3600s
89 90 91 92 |
# File 'lib/carrierwave/storage/aliyun.rb', line 89 def private_get_url(path) path.sub!(/^\//, '') oss_client.bucket_get_object_share_link(path, 3600) end |
#put(path, file, options = {}) ⇒ Object
上传文件params:
-
path - remote 存储路径
-
file - 需要上传文件的 File 对象
-
options:
-
content_type - 上传文件的 MimeType,默认 ‘image/jpg`
-
returns: 图片的下载地址
35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/carrierwave/storage/aliyun.rb', line 35 def put(path, file, ={}) path.sub!(/^\//, '') opts = { 'Content-Type' => [:content_type] || "image/jpg" } res = oss_upload_client.bucket_create_object(path, file, opts) if res.success? path_to_url(path) else raise "Put file failed" end end |