Class: Fog::Aliyun::Storage::Real
- Inherits:
-
Object
- Object
- Fog::Aliyun::Storage::Real
- Defined in:
- lib/fog/aliyun/storage.rb,
lib/fog/aliyun/requests/storage/get_bucket.rb,
lib/fog/aliyun/requests/storage/get_object.rb,
lib/fog/aliyun/requests/storage/put_bucket.rb,
lib/fog/aliyun/requests/storage/put_object.rb,
lib/fog/aliyun/requests/storage/copy_object.rb,
lib/fog/aliyun/requests/storage/get_service.rb,
lib/fog/aliyun/requests/storage/head_object.rb,
lib/fog/aliyun/requests/storage/upload_part.rb,
lib/fog/aliyun/requests/storage/list_buckets.rb,
lib/fog/aliyun/requests/storage/list_objects.rb,
lib/fog/aliyun/requests/storage/delete_bucket.rb,
lib/fog/aliyun/requests/storage/delete_object.rb,
lib/fog/aliyun/requests/storage/get_object_acl.rb,
lib/fog/aliyun/requests/storage/get_bucket_location.rb,
lib/fog/aliyun/requests/storage/get_object_http_url.rb,
lib/fog/aliyun/requests/storage/get_object_https_url.rb,
lib/fog/aliyun/requests/storage/abort_multipart_upload.rb,
lib/fog/aliyun/requests/storage/delete_multiple_objects.rb,
lib/fog/aliyun/requests/storage/complete_multipart_upload.rb,
lib/fog/aliyun/requests/storage/initiate_multipart_upload.rb
Instance Attribute Summary collapse
-
#aliyun_accesskey_id ⇒ Object
readonly
Initialize connection to OSS.
-
#aliyun_accesskey_secret ⇒ Object
readonly
Returns the value of attribute aliyun_accesskey_secret.
-
#aliyun_oss_bucket ⇒ Object
readonly
Returns the value of attribute aliyun_oss_bucket.
-
#aliyun_oss_endpoint ⇒ Object
readonly
Returns the value of attribute aliyun_oss_endpoint.
-
#aliyun_oss_sdk_log_path ⇒ Object
readonly
Returns the value of attribute aliyun_oss_sdk_log_path.
-
#aliyun_region_id ⇒ Object
readonly
Returns the value of attribute aliyun_region_id.
Instance Method Summary collapse
-
#abort_multipart_upload(bucket_name, object_name, upload_id) ⇒ Object
Abort a multipart upload.
- #bucket_exists?(bucket_name) ⇒ Boolean
-
#complete_multipart_upload(bucket_name, object_name, upload_id, parts) ⇒ Object
Complete a multipart upload.
-
#copy_object(source_bucket_name, source_object_name, target_bucket_name, target_object_name, options = {}) ⇒ Object
Copy object.
-
#delete_bucket(bucket_name) ⇒ Object
Delete an existing bucket.
-
#delete_multiple_objects(bucket_name, object_names, options = {}) ⇒ Object
Delete multiple objects from OSS.
-
#delete_object(bucket_name, object_name, options = {}) ⇒ Object
Delete an existing object.
- #escape(string) ⇒ Object
- #get_bucket(bucket_name, options = {}) ⇒ Object
- #get_bucket_acl(bucket_name) ⇒ Object
- #get_bucket_CORSRules(bucket_name) ⇒ Object
- #get_bucket_lifecycle(bucket_name) ⇒ Object
-
#get_bucket_location(bucket_name) ⇒ Object
Get location constraint for an OSS bucket.
- #get_bucket_logging(bucket_name) ⇒ Object
- #get_bucket_referer(bucket_name) ⇒ Object
- #get_bucket_website(bucket_name) ⇒ Object
-
#get_object(bucket_name, object_name, options = {}, &block) ⇒ Object
Get details for object.
-
#get_object_acl(bucket_name, object_name, options = {}) ⇒ Object
Get access control list for an S3 object.
-
#get_object_http_url_public(bucket_name, object_name, expires) ⇒ Object
Get an expiring object http url.
-
#get_object_https_url_public(bucket_name, object_name, expires) ⇒ Object
Get an expiring object https url from Cloud Files.
-
#get_service ⇒ Object
List information about OSS buckets for authorized user.
-
#head_object(bucket_name, object_name, options = {}) ⇒ Object
Get headers for object.
-
#initialize(options = {}) ⇒ Real
constructor
A new instance of Real.
-
#initiate_multipart_upload(bucket_name, object_name, options = {}) ⇒ Object
Initiate a multipart upload.
- #list_buckets(options = {}) ⇒ Object
- #list_multipart_uploads(bucket_name, _options = {}) ⇒ Object
- #list_objects(bucket_name, options = {}) ⇒ Object
- #list_parts(bucket_name, object_name, upload_id, _options = {}) ⇒ Object
- #object_to_path(object_name = nil) ⇒ Object
- #put_bucket(bucket_name, options = {}) ⇒ Object
-
#put_object(bucket_name, object_name, data, options = {}) ⇒ Object
Put details for object.
- #region_to_endpoint(region = nil) ⇒ Object
- #reload ⇒ Object
- #request(params) ⇒ Object
-
#sign(method, date, contentType, resource = nil, headers = nil) ⇒ Object
copmute signature.
-
#upload_part(bucket_name, object_name, upload_id, part_number, data) ⇒ Object
Upload a part for a multipart upload.
Constructor Details
#initialize(options = {}) ⇒ Real
Returns a new instance of Real.
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/fog/aliyun/storage.rb', line 84 def initialize( = {}) # initialize the parameters @aliyun_region_id = [:aliyun_region_id] || [:aliyun_oss_location] || DEFAULT_REGION @aliyun_oss_endpoint = [:aliyun_oss_endpoint] || region_to_endpoint(@aliyun_region_id) @aliyun_accesskey_id = [:aliyun_accesskey_id] @aliyun_accesskey_secret = [:aliyun_accesskey_secret] @aliyun_oss_bucket = [:aliyun_oss_bucket] @aliyun_oss_sdk_log_path=[:aliyun_oss_sdk_log_path] if @aliyun_oss_sdk_log_path && !::File.exist?(@aliyun_oss_sdk_log_path) `touch #{@aliyun_oss_sdk_log_path}` end ENV["ALIYUN_OSS_SDK_LOG_PATH"] = @aliyun_oss_sdk_log_path # check for the parameters missing_credentials = [] missing_credentials << :aliyun_oss_bucket unless @aliyun_oss_bucket missing_credentials << :aliyun_accesskey_id unless @aliyun_accesskey_id missing_credentials << :aliyun_accesskey_secret unless @aliyun_accesskey_secret raise ArgumentError, "Missing required arguments: #{missing_credentials.join(', ')}" unless missing_credentials.empty? @connection_options = [:connection_options] || {} endpoint = @aliyun_oss_endpoint @aliyun_oss_endpoint = "#{DEFAULT_SCHEME}://#{endpoint}" unless endpoint.start_with?(DEFAULT_SCHEME) uri = URI.parse(@aliyun_oss_endpoint) @host = uri.host @path = uri.path @scheme = uri.scheme || DEFAULT_SCHEME @port = uri.port || DEFAULT_SCHEME_PORT[@scheme] @persistent = [:persistent] || false @oss_client = AliyunOssSdk::Client.new( :endpoint => @aliyun_oss_endpoint, :access_key_id => @aliyun_accesskey_id, :access_key_secret => @aliyun_accesskey_secret ) # initiate a aliyun oss ruby sdk config and using sdk http to invoke the OSS openapi @oss_config = AliyunOssSdk::Config.new( :endpoint => @aliyun_oss_endpoint, :access_key_id => @aliyun_accesskey_id, :access_key_secret => @aliyun_accesskey_secret ) @oss_http = AliyunOssSdk::HTTP.new(@oss_config) @oss_protocol = AliyunOssSdk::Protocol.new(@oss_config) end |
Instance Attribute Details
#aliyun_accesskey_id ⇒ Object (readonly)
Initialize connection to OSS
Notes
options parameter must include values for :aliyun_accesskey_id, :aliyun_secret_access_key and :aliyun_oss_bucket in order to create a connection. :aliyun_oss_location will be replaced by :aliyun_region_id, and it has a default value cn-hangzhou if :aliyun_oss_endpoint is not specified, it will be generated by method region_to_endpoint
Examples
sdb = Fog::Storage.new(:provider=>'aliyun',
:aliyun_accesskey_id => your_:aliyun_accesskey_id,
:aliyun_secret_access_key => your_aliyun_secret_access_key
)
Parameters
-
options<~Hash> - config arguments for connection. Defaults to {}.
Returns
-
OSS object with connection to aliyun.
77 78 79 |
# File 'lib/fog/aliyun/storage.rb', line 77 def aliyun_accesskey_id @aliyun_accesskey_id end |
#aliyun_accesskey_secret ⇒ Object (readonly)
Returns the value of attribute aliyun_accesskey_secret.
78 79 80 |
# File 'lib/fog/aliyun/storage.rb', line 78 def aliyun_accesskey_secret @aliyun_accesskey_secret end |
#aliyun_oss_bucket ⇒ Object (readonly)
Returns the value of attribute aliyun_oss_bucket.
81 82 83 |
# File 'lib/fog/aliyun/storage.rb', line 81 def aliyun_oss_bucket @aliyun_oss_bucket end |
#aliyun_oss_endpoint ⇒ Object (readonly)
Returns the value of attribute aliyun_oss_endpoint.
79 80 81 |
# File 'lib/fog/aliyun/storage.rb', line 79 def aliyun_oss_endpoint @aliyun_oss_endpoint end |
#aliyun_oss_sdk_log_path ⇒ Object (readonly)
Returns the value of attribute aliyun_oss_sdk_log_path.
82 83 84 |
# File 'lib/fog/aliyun/storage.rb', line 82 def aliyun_oss_sdk_log_path @aliyun_oss_sdk_log_path end |
#aliyun_region_id ⇒ Object (readonly)
Returns the value of attribute aliyun_region_id.
80 81 82 |
# File 'lib/fog/aliyun/storage.rb', line 80 def aliyun_region_id @aliyun_region_id end |
Instance Method Details
#abort_multipart_upload(bucket_name, object_name, upload_id) ⇒ Object
Abort a multipart upload
15 16 17 |
# File 'lib/fog/aliyun/requests/storage/abort_multipart_upload.rb', line 15 def abort_multipart_upload(bucket_name, object_name, upload_id) @oss_protocol.abort_multipart_upload(bucket_name, object_name, upload_id) end |
#bucket_exists?(bucket_name) ⇒ Boolean
7 8 9 |
# File 'lib/fog/aliyun/requests/storage/get_bucket.rb', line 7 def bucket_exists?(bucket_name) @oss_client.bucket_exists?(bucket_name) end |
#complete_multipart_upload(bucket_name, object_name, upload_id, parts) ⇒ Object
Complete a multipart upload
15 16 17 |
# File 'lib/fog/aliyun/requests/storage/complete_multipart_upload.rb', line 15 def complete_multipart_upload(bucket_name, object_name, upload_id, parts) @oss_protocol.complete_multipart_upload(bucket_name, object_name, upload_id, parts) end |
#copy_object(source_bucket_name, source_object_name, target_bucket_name, target_object_name, options = {}) ⇒ Object
Copy object
Parameters
-
source_bucket_name<~String> - Name of source bucket
-
source_object_name<~String> - Name of source object
-
target_bucket_name<~String> - Name of bucket to create copy in
-
target_object_name<~String> - Name for new copy of object
-
options<~Hash> - Additional headers options={}
15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/fog/aliyun/requests/storage/copy_object.rb', line 15 def copy_object(source_bucket_name, source_object_name, target_bucket_name, target_object_name, = {}) headers = { 'x-oss-copy-source' => "/#{source_bucket_name}#{object_to_path(source_object_name)}" }.merge!() resources = { :bucket => target_bucket_name, :object => target_object_name } = { :headers => headers } @oss_http.put(resources, ) end |
#delete_bucket(bucket_name) ⇒ Object
Delete an existing bucket
Parameters
-
bucket_name<~String> - Name of bucket to delete
12 13 14 |
# File 'lib/fog/aliyun/requests/storage/delete_bucket.rb', line 12 def delete_bucket(bucket_name) @oss_protocol.delete_bucket(bucket_name) end |
#delete_multiple_objects(bucket_name, object_names, options = {}) ⇒ Object
Delete multiple objects from OSS
13 14 15 16 |
# File 'lib/fog/aliyun/requests/storage/delete_multiple_objects.rb', line 13 def delete_multiple_objects(bucket_name, object_names, = {}) bucket = @oss_client.get_bucket(bucket_name) bucket.batch_delete_objects(object_names, ) end |
#delete_object(bucket_name, object_name, options = {}) ⇒ Object
Delete an existing object
Parameters
-
bucket_name<~String> - Name of bucket to delete
-
object_name<~String> - Name of object to delete
13 14 15 16 17 18 19 20 21 |
# File 'lib/fog/aliyun/requests/storage/delete_object.rb', line 13 def delete_object(bucket_name, object_name, = {}) # TODO Support versionId # if version_id = options.delete('versionId') # query = {'versionId' => version_id} # else # query = {} # end @oss_http.delete({:bucket => bucket_name, :object => object_name}, {:headers => }) end |
#escape(string) ⇒ Object
149 150 151 152 153 |
# File 'lib/fog/aliyun/storage.rb', line 149 def escape(string) string.gsub(/([^a-zA-Z0-9_.\-~\/]+)/) { "%" + $1.unpack("H2" * $1.bytesize).join("%").upcase } end |
#get_bucket(bucket_name, options = {}) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/fog/aliyun/requests/storage/get_bucket.rb', line 11 def get_bucket(bucket_name, = {}) unless bucket_name raise ArgumentError.new('bucket_name is required') end # Set the GetBucket max limitation to 1000 maxKeys = [:max_keys] || 1000 maxKeys = maxKeys.to_i maxKeys = [maxKeys, 1000].min [:limit] = maxKeys .delete(:max_keys) @oss_protocol.list_objects(bucket_name, ) end |
#get_bucket_acl(bucket_name) ⇒ Object
27 28 29 |
# File 'lib/fog/aliyun/requests/storage/get_bucket.rb', line 27 def get_bucket_acl(bucket_name) @oss_protocol.get_bucket_acl(bucket_name) end |
#get_bucket_CORSRules(bucket_name) ⇒ Object
31 32 33 |
# File 'lib/fog/aliyun/requests/storage/get_bucket.rb', line 31 def get_bucket_CORSRules(bucket_name) @oss_protocol.get_bucket_cors(bucket_name) end |
#get_bucket_lifecycle(bucket_name) ⇒ Object
35 36 37 |
# File 'lib/fog/aliyun/requests/storage/get_bucket.rb', line 35 def get_bucket_lifecycle(bucket_name) @oss_protocol.get_bucket_lifecycle(bucket_name) end |
#get_bucket_location(bucket_name) ⇒ Object
Get location constraint for an OSS bucket
note: The OSS Ruby sdk does not support get_bucket_location and there needs to parse response
15 16 17 18 19 |
# File 'lib/fog/aliyun/requests/storage/get_bucket_location.rb', line 15 def get_bucket_location(bucket_name) data = @oss_http.get({:bucket => bucket_name, :sub_res => { 'location' => nil} }, {}) doc = parse_xml(data.body) doc.at_css("LocationConstraint").text end |
#get_bucket_logging(bucket_name) ⇒ Object
39 40 41 |
# File 'lib/fog/aliyun/requests/storage/get_bucket.rb', line 39 def get_bucket_logging(bucket_name) @oss_protocol.get_bucket_logging(bucket_name) end |
#get_bucket_referer(bucket_name) ⇒ Object
43 44 45 |
# File 'lib/fog/aliyun/requests/storage/get_bucket.rb', line 43 def get_bucket_referer(bucket_name) @oss_protocol.get_bucket_referer(bucket_name) end |
#get_bucket_website(bucket_name) ⇒ Object
47 48 49 |
# File 'lib/fog/aliyun/requests/storage/get_bucket.rb', line 47 def get_bucket_website(bucket_name) @oss_protocol.get_bucket_website(bucket_name) end |
#get_object(bucket_name, object_name, options = {}, &block) ⇒ Object
Get details for object
Parameters
-
object_name<~String> - Name of object to look for
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/fog/aliyun/requests/storage/get_object.rb', line 12 def get_object(bucket_name, object_name, = {}, &block) = .reject { |_key, value| value.nil? } unless bucket_name raise ArgumentError.new('bucket_name is required') end unless object_name raise ArgumentError.new('object_name is required') end # Using OSS ruby SDK to fix performance issue = { :headers => {} } [:query] = .delete('query') || {} [:headers].merge!() if ['If-Modified-Since'] [:headers]['If-Modified-Since'] = Fog::Time.at(['If-Modified-Since'].to_i).to_date_header end if ['If-Unmodified-Since'] [:headers]['If-Unmodified-Since'] = Fog::Time.at(['If-Unmodified-Since'].to_i).to_date_header end if block_given? [:response_block] = Proc.new {} end resources = { :bucket => bucket_name, :object => object_name } @oss_http.get(resources, , &block) end |
#get_object_acl(bucket_name, object_name, options = {}) ⇒ Object
Get access control list for an S3 object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/fog/aliyun/requests/storage/get_object_acl.rb', line 13 def get_object_acl(bucket_name, object_name, = {}) unless bucket_name raise ArgumentError.new('bucket_name is required') end unless object_name raise ArgumentError.new('object_name is required') end # At present, sdk does not support versionId # if version_id = options.delete('versionId') # query['versionId'] = version_id # end @oss_protocol.get_object_acl(bucket_name, object_name) end |
#get_object_http_url_public(bucket_name, object_name, expires) ⇒ Object
Get an expiring object http url
Parameters
-
bucket_name<~String> - Name of bucket
-
object_name<~String> - Name of object to get expiring url for
-
expires<~Integer> - An expiry time for this url
Returns
-
response<~Excon::Response>:
-
body<~String> - url for object
-
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/fog/aliyun/requests/storage/get_object_http_url.rb', line 19 def get_object_http_url_public(bucket_name, object_name, expires) bucket = @oss_client.get_bucket(bucket_name) acl = bucket.acl() if acl == 'private' expires_time = (Time.now.to_i + (expires.nil? ? 0 : expires.to_i)).to_s resource = bucket_name + '/' + object_name signature = sign('GET', expires_time, nil, resource) 'http://' + bucket_name + '.' + @host + '/' + object_name + '?OSSAccessKeyId=' + @aliyun_accesskey_id + '&Expires=' + expires_time + '&Signature=' + Addressable::URI.encode_component(signature, Addressable::URI::CharacterClasses::UNRESERVED + '|') elsif acl == 'public-read' || acl == 'public-read-write' 'http://' + bucket_name + '.' + @host + '/' + object_name else 'acl is wrong with value:' + acl end end |
#get_object_https_url_public(bucket_name, object_name, expires) ⇒ Object
Get an expiring object https url from Cloud Files
Parameters
-
bucket_name<~String> - Name of bucket
-
object_name<~String> - Name of object to get expiring url for
-
expires<~Integer> - An expiry time for this url
Returns
-
response<~Excon::Response>:
-
body<~String> - url for object
-
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/fog/aliyun/requests/storage/get_object_https_url.rb', line 19 def get_object_https_url_public(bucket_name, object_name, expires) bucket = @oss_client.get_bucket(bucket_name) acl = bucket.acl() if acl == 'private' expires_time = (Time.now.to_i + (expires.nil? ? 0 : expires.to_i)).to_s resource = bucket_name + '/' + object_name signature = sign('GET', expires_time, nil, resource) 'https://' + bucket_name + '.' + @host + '/' + object_name + '?OSSAccessKeyId=' + @aliyun_accesskey_id + '&Expires=' + expires_time + '&Signature=' + Addressable::URI.encode_component(signature, Addressable::URI::CharacterClasses::UNRESERVED + '|') elsif acl == 'public-read' || acl == 'public-read-write' 'https://' + bucket_name + '.' + @host + '/' + object_name else 'acl is wrong with value:' + acl end end |
#get_service ⇒ Object
List information about OSS buckets for authorized user
7 8 9 |
# File 'lib/fog/aliyun/requests/storage/get_service.rb', line 7 def get_service @oss_protocol.list_buckets end |
#head_object(bucket_name, object_name, options = {}) ⇒ Object
Get headers for object
Parameters
-
object_name<~String> - Name of object to look for
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/fog/aliyun/requests/storage/head_object.rb', line 12 def head_object(bucket_name, object_name, ={}) unless bucket_name raise ArgumentError.new('bucket_name is required') end unless object_name raise ArgumentError.new('object_name is required') end # Currently, the ruby sdk does not support versionId # if version_id = options.delete('versionId') # query = {'versionId' => version_id} # end headers = {} headers['If-Modified-Since'] = Fog::Time.at(['If-Modified-Since'].to_i).to_date_header if ['If-Modified-Since'] headers['If-Unmodified-Since'] = Fog::Time.at(['If-Unmodified-Since'].to_i).to_date_header if ['If-Modified-Since'] headers.merge!() resources = { :bucket => bucket_name, :object => object_name } = { :headers => headers } @oss_http.head(resources, ) end |
#initiate_multipart_upload(bucket_name, object_name, options = {}) ⇒ Object
Initiate a multipart upload
13 14 15 |
# File 'lib/fog/aliyun/requests/storage/initiate_multipart_upload.rb', line 13 def initiate_multipart_upload(bucket_name, object_name, = {}) @oss_protocol.initiate_multipart_upload(bucket_name, object_name, ) end |
#list_buckets(options = {}) ⇒ Object
7 8 9 10 11 12 13 14 15 |
# File 'lib/fog/aliyun/requests/storage/list_buckets.rb', line 7 def list_buckets( = {}) maxKeys = [:max_keys] || 1000 maxKeys = maxKeys.to_i maxKeys = [maxKeys, 1000].min [:limit] = maxKeys .delete(:max_keys) @oss_protocol.list_buckets() end |
#list_multipart_uploads(bucket_name, _options = {}) ⇒ Object
17 18 19 |
# File 'lib/fog/aliyun/requests/storage/list_objects.rb', line 17 def list_multipart_uploads(bucket_name, = {}) @oss_protocol.list_multipart_uploads(bucket_name, ) end |
#list_objects(bucket_name, options = {}) ⇒ Object
7 8 9 10 11 12 13 14 15 |
# File 'lib/fog/aliyun/requests/storage/list_objects.rb', line 7 def list_objects(bucket_name, = {}) maxKeys = [:max_keys] || 1000 maxKeys = maxKeys.to_i maxKeys = [maxKeys, 1000].min [:limit] = maxKeys .delete(:max_keys) @oss_protocol.list_objects(bucket_name, ) end |
#list_parts(bucket_name, object_name, upload_id, _options = {}) ⇒ Object
21 22 23 |
# File 'lib/fog/aliyun/requests/storage/list_objects.rb', line 21 def list_parts(bucket_name, object_name, upload_id, = {}) @oss_protocol.list_parts(bucket_name, object_name, upload_id, ) end |
#object_to_path(object_name = nil) ⇒ Object
145 146 147 |
# File 'lib/fog/aliyun/storage.rb', line 145 def object_to_path(object_name=nil) '/' + escape(object_name.to_s).gsub('%2F','/') end |
#put_bucket(bucket_name, options = {}) ⇒ Object
7 8 9 |
# File 'lib/fog/aliyun/requests/storage/put_bucket.rb', line 7 def put_bucket(bucket_name, = {}) @oss_protocol.create_bucket(bucket_name, ) end |
#put_object(bucket_name, object_name, data, options = {}) ⇒ Object
Put details for object
Parameters
-
bucket_name<~String> - Name of bucket to look for
-
object_name<~String> - Object of object to look for
-
data<~File>
-
options<~Hash>
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/fog/aliyun/requests/storage/put_object.rb', line 15 def put_object(bucket_name, object_name, data, = {}) if data.is_a? ::File @oss_protocol.put_object(bucket_name, object_name, )do |sw| while line = data.read(16*1024) sw.write(line) end end else content=StringIO.new(data.dup) @oss_protocol.put_object(bucket_name, object_name, )do |sw| while line=content.read(16*1024) sw.write(line) end end content.close end end |
#region_to_endpoint(region = nil) ⇒ Object
136 137 138 139 140 141 142 143 |
# File 'lib/fog/aliyun/storage.rb', line 136 def region_to_endpoint(region = nil) case region.to_s when '' "oss-#{DEFAULT_REGION}.aliyuncs.com" else "oss-#{region}.aliyuncs.com" end end |
#reload ⇒ Object
132 133 134 |
# File 'lib/fog/aliyun/storage.rb', line 132 def reload @connection.reset end |
#request(params) ⇒ Object
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 |
# File 'lib/fog/aliyun/storage.rb', line 155 def request(params) method = params[:method] time = Time.new.utc date = time.strftime('%a, %d %b %Y %H:%M:%S GMT') bucket = params[:bucket] tmpHost = if bucket bucket + '.' + @host else @host end @connection = Fog::Core::Connection.new("#{@scheme}://#{tmpHost}", @persistent, @connection_options) contentType = params[:contentType] begin headers = '' if params[:headers] params[:headers].each do |k, v| headers += "#{k}:#{v}\n" if k != 'Range' end end signature = sign(method, date, contentType, params[:resource], headers) response = @connection.request(params.merge(headers: { 'Content-Type' => contentType, 'Authorization' => 'OSS ' + @aliyun_accesskey_id + ':' + signature, 'Date' => date }.merge!(params[:headers] || {}), path: "#{@path}/#{params[:path]}", query: params[:query])) rescue Excon::Errors::HTTPStatusError => error raise case error when Excon::Errors::NotFound Fog::Aliyun::Storage::NotFound.slurp(error) else error end end response end |
#sign(method, date, contentType, resource = nil, headers = nil) ⇒ Object
copmute signature
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 |
# File 'lib/fog/aliyun/storage.rb', line 198 def sign(method, date, contentType, resource = nil, headers = nil) contentmd5 = '' canonicalizedResource = if resource '/' + resource else '/' end canonicalizedOSSHeaders = headers || '' contentTypeStr = contentType || '' stringToSign = method + "\n" + contentmd5 + "\n" + contentTypeStr + "\n" + date + "\n" + canonicalizedOSSHeaders + canonicalizedResource digVer = OpenSSL::Digest.new('sha1') digest = OpenSSL::HMAC.digest(digVer, @aliyun_accesskey_secret, stringToSign) signature = Base64.encode64(digest) signature[-1] = '' signature end |
#upload_part(bucket_name, object_name, upload_id, part_number, data) ⇒ Object
Upload a part for a multipart upload
16 17 18 19 20 |
# File 'lib/fog/aliyun/requests/storage/upload_part.rb', line 16 def upload_part(bucket_name, object_name, upload_id, part_number, data) @oss_protocol.upload_part(bucket_name, object_name, upload_id, part_number) do |sw| sw.write(data) end end |