lib/carrierwave/storage/aliyun.rb in carrierwave-aliyun-0.1.5 vs lib/carrierwave/storage/aliyun.rb in carrierwave-aliyun-0.2.0
- old
+ new
@@ -1,75 +1,87 @@
# encoding: utf-8
require 'carrierwave'
require 'digest/hmac'
require 'digest/md5'
require "rest-client"
+require 'uri'
module CarrierWave
module Storage
class Aliyun < Abstract
class Connection
def initialize(options={})
@aliyun_access_id = options[:aliyun_access_id]
@aliyun_access_key = options[:aliyun_access_key]
@aliyun_bucket = options[:aliyun_bucket]
- @aliyun_upload_host = "oss.aliyuncs.com"
- @aliyun_host = options[:aliyun_host] || @aliyun_upload_host
+ # Host for upload
+ @aliyun_upload_host = "#{@aliyun_bucket}.oss.aliyuncs.com"
if options[:aliyun_internal] == true
- @aliyun_upload_host = "oss-internal.aliyuncs.com"
+ @aliyun_upload_host = "#{@aliyun_bucket}.oss-internal.aliyuncs.com"
end
+ # Host for get request
+ @aliyun_host = options[:aliyun_host] || "#{@aliyun_bucket}.oss.aliyuncs.com"
end
def put(path, file_data, options={})
path = format_path(path)
+ bucket_path = get_bucket_path(path)
content_md5 = Digest::MD5.hexdigest(file_data)
content_type = options[:content_type] || "image/jpg"
date = gmtdate
url = path_to_url(path)
- auth_sign = sign("PUT", path, content_md5, content_type,date)
+ auth_sign = sign("PUT", bucket_path, content_md5, content_type,date)
headers = {
"Authorization" => auth_sign,
"Content-Type" => content_type,
"Content-Length" => file_data.length,
"Date" => date,
"Host" => @aliyun_upload_host,
"Expect" => "100-Continue"
}
- response = RestClient.put(url, file_data, headers)
+ RestClient.put(URI.encode(url), file_data, headers)
return path_to_url(path, :get => true)
end
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", path, "", "" ,date)
+ "Authorization" => sign("DELETE", bucket_path, "", "" ,date)
}
url = path_to_url(path)
- RestClient.delete(url, headers)
+ RestClient.delete(URI.encode(url), headers)
return path_to_url(path, :get => true)
end
def gmtdate
Time.now.gmtime.strftime("%a, %d %b %Y %H:%M:%S GMT")
end
def format_path(path)
return "" if path.blank?
path.gsub!(/^\//,"")
- [@aliyun_bucket, path].join("/")
+
+ path
end
+
+ def get_bucket_path(path)
+ [@aliyun_bucket,path].join("/")
+ end
def path_to_url(path, opts = {})
- host = @aliyun_upload_host
- host = @aliyun_host if opts[:get]
- "http://#{host}/#{path}"
+ if opts[:get]
+ "http://#{@aliyun_host}/#{path}"
+ else
+ "http://#{@aliyun_upload_host}/#{path}"
+ end
end
-
+
private
def sign(verb, path, content_md5 = '', content_type = '', date)
canonicalized_oss_headers = ''
canonicalized_resource = "/#{path}"
string_to_sign = "#{verb}\n\n#{content_type}\n#{date}\n#{canonicalized_oss_headers}#{canonicalized_resource}"
@@ -123,11 +135,11 @@
nil
end
end
def url
- "http://#{@uploader.aliyun_host}/#{@uploader.aliyun_bucket}/#{@path}"
+ oss_connection.path_to_url(@path, :get => true)
end
def store(data, opts = {})
oss_connection.put(@path, data, opts)
end
@@ -142,14 +154,16 @@
@base.connection
end
def oss_connection
return @oss_connection if @oss_connection
-
+
config = {
:aliyun_access_id => @uploader.aliyun_access_id,
:aliyun_access_key => @uploader.aliyun_access_key,
- :aliyun_bucket => @uploader.aliyun_bucket
+ :aliyun_bucket => @uploader.aliyun_bucket,
+ :aliyun_internal => @uploader.aliyun_internal,
+ :aliyun_host => @uploader.aliyun_host
}
@oss_connection ||= CarrierWave::Storage::Aliyun::Connection.new(config)
end
end