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 的文件.
- #format_path(path) ⇒ Object
- #get_bucket_path(path) ⇒ Object
-
#gmtdate ⇒ Object
阿里云需要的 GMT 时间格式.
-
#initialize(options = {}) ⇒ Connection
constructor
A new instance of Connection.
-
#path_to_url(path, opts = {}) ⇒ Object
根据配置返回完整的上传文件的访问地址.
-
#put(path, file, options = {}) ⇒ Object
上传文件.
Constructor Details
#initialize(options = {}) ⇒ Connection
Returns a new instance of Connection.
13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/carrierwave/storage/aliyun.rb', line 13 def initialize(={}) @aliyun_access_id = [:aliyun_access_id] @aliyun_access_key = [:aliyun_access_key] @aliyun_bucket = [:aliyun_bucket] @aliyun_area = [:aliyun_area] || 'cn-hangzhou' # Host for upload @aliyun_upload_host = "#{@aliyun_bucket}.oss-#{@aliyun_area}.aliyuncs.com" if [:aliyun_internal] == true @aliyun_upload_host = "#{@aliyun_bucket}.oss-#{@aliyun_area}-internal.aliyuncs.com" end # Host for get request @aliyun_host = [:aliyun_host] || "#{@aliyun_bucket}.oss-#{@aliyun_area}.aliyuncs.com" end |
Instance Method Details
#delete(path) ⇒ Object
删除 Remote 的文件
参数:
-
path - remote 存储路径
返回值:
图片的下载地址
68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/carrierwave/storage/aliyun.rb', line 68 def delete(path) path = format_path(path) bucket_path = get_bucket_path(path) date = gmtdate headers = { "Host" => @aliyun_upload_host, "Date" => date, "Authorization" => sign("DELETE", bucket_path, "", "" ,date) } url = path_to_url(path) RestClient.delete(URI.encode(url), headers) return path_to_url(path, :get => true) end |
#format_path(path) ⇒ Object
88 89 90 91 92 93 |
# File 'lib/carrierwave/storage/aliyun.rb', line 88 def format_path(path) return "" if path.blank? path.gsub!(/^\//,"") path end |
#get_bucket_path(path) ⇒ Object
95 96 97 |
# File 'lib/carrierwave/storage/aliyun.rb', line 95 def get_bucket_path(path) [@aliyun_bucket,path].join("/") end |
#gmtdate ⇒ Object
阿里云需要的 GMT 时间格式
84 85 86 |
# File 'lib/carrierwave/storage/aliyun.rb', line 84 def gmtdate Time.now.gmtime.strftime("%a, %d %b %Y %H:%M:%S GMT") end |
#path_to_url(path, opts = {}) ⇒ Object
根据配置返回完整的上传文件的访问地址
101 102 103 104 105 106 107 |
# File 'lib/carrierwave/storage/aliyun.rb', line 101 def path_to_url(path, opts = {}) if opts[:get] "http://#{@aliyun_host}/#{path}" else "http://#{@aliyun_upload_host}/#{path}" end end |
#put(path, file, options = {}) ⇒ Object
上传文件
参数:
-
path - remote 存储路径
-
file - 需要上传文件的 File 对象
-
options:
-
content_type - 上传文件的 MimeType,默认 ‘image/jpg`
-
返回值:
图片的下载地址
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/carrierwave/storage/aliyun.rb', line 39 def put(path, file, ={}) path = format_path(path) bucket_path = get_bucket_path(path) content_md5 = Digest::MD5.file(file) content_type = [:content_type] || "image/jpg" date = gmtdate url = path_to_url(path) auth_sign = sign("PUT", bucket_path, content_md5, content_type,date) headers = { "Authorization" => auth_sign, "Content-Type" => content_type, "Content-Length" => file.size, "Date" => date, "Host" => @aliyun_upload_host, "Expect" => "100-Continue" } RestClient.put(URI.encode(url), file, headers) return path_to_url(path, :get => true) end |