lib/carrierwave/storage/aliyun.rb in carrierwave-aliyun-0.3.5 vs lib/carrierwave/storage/aliyun.rb in carrierwave-aliyun-0.3.6

- old
+ new

@@ -47,11 +47,11 @@ bucket_path = get_bucket_path(path) content_md5 = Digest::MD5.file(file) content_type = options[:content_type] || "image/jpg" date = gmtdate url = path_to_url(path) - + host = URI.parse(url).host auth_sign = sign("PUT", bucket_path, content_md5, content_type,date) headers = { "Authorization" => auth_sign, @@ -60,23 +60,34 @@ "Date" => date, "Host" => host, "Expect" => "100-Continue" } - RestClient.put(URI.encode(url).gsub("+", "%2B"), file, headers) + RestClient.put(url, file, headers) return path_to_url(path, :get => true) end # 读取文件 # params: # - path - remote 存储路径 # returns: # file data def get(path) - path = format_path(path) - url = path_to_url(path) - RestClient.get(URI.encode(url)) + path = format_path(path) + bucket_path = get_bucket_path(path) + date = gmtdate + url = path_to_url(path) + host = URI.parse(url).host + headers = { + "Host" => host, + "Date" => date, + "Authorization" => sign("GET", bucket_path, "", "" ,date) + } + + # path = format_path(path) + # url = path_to_url(path) + RestClient.get(url, headers) end # 删除 Remote 的文件 # # params: @@ -93,12 +104,12 @@ headers = { "Host" => host, "Date" => date, "Authorization" => sign("DELETE", bucket_path, "", "" ,date) } - - RestClient.delete(URI.encode(url).gsub("+", "%2B"), headers) + + RestClient.delete(url, headers) return path_to_url(path, :get => true) end # # 阿里云需要的 GMT 时间格式 @@ -128,11 +139,11 @@ end private def sign(verb, path, content_md5 = '', content_type = '', date) canonicalized_oss_headers = '' - canonicalized_resource = "/#{path}" + canonicalized_resource = "/#{URI.decode(path)}" string_to_sign = "#{verb}\n\n#{content_type}\n#{date}\n#{canonicalized_oss_headers}#{canonicalized_resource}" digest = OpenSSL::Digest.new('sha1') h = OpenSSL::HMAC.digest(digest, @aliyun_access_key, string_to_sign) h = Base64.encode64(h) "OSS #{@aliyun_access_id}:#{h}" @@ -140,10 +151,10 @@ end class File < CarrierWave::SanitizedFile def initialize(uploader, base, path) @uploader = uploader - @path = path + @path = URI.encode(path) @base = base end ## # Returns the current path/filename of the file on Cloud Files.