Class: Aliyun::Oss::OssRequest
- Inherits:
-
Object
- Object
- Aliyun::Oss::OssRequest
- Includes:
- ConfigHelper
- Defined in:
- lib/aliyunoss/oss_request.rb
Instance Attribute Summary collapse
-
#body ⇒ Object
Returns the value of attribute body.
-
#bucket ⇒ Object
Returns the value of attribute bucket.
-
#domain ⇒ Object
Returns the value of attribute domain.
-
#path ⇒ Object
Returns the value of attribute path.
-
#queris ⇒ Object
Returns the value of attribute queris.
Class Method Summary collapse
Instance Method Summary collapse
-
#[](key) ⇒ Object
Get http header value by attribute.
-
#[]=(key, value) ⇒ Object
Set http header value by attribute.
-
#get_uri ⇒ Object
Get complete url for this request.
- #headers_for_write(filename: nil, content_type:, content_length:, checksum:, custom_metadata: {}) ⇒ Object
-
#initialize(bucket, path, domain = nil, queries = {}, headers = {}) ⇒ OssRequest
constructor
Create a new oss request, parameters will be sent to methods of Net::HTTP later.
-
#url_for_sharing(expires_in) ⇒ Object
Get sharing url for this request, pass expires_in as parameter.
Methods included from ConfigHelper
#access_key_id, #access_key_secret, #config, #host, #logger
Constructor Details
#initialize(bucket, path, domain = nil, queries = {}, headers = {}) ⇒ OssRequest
Create a new oss request, parameters will be sent to methods of Net::HTTP later.
16 17 18 19 20 21 22 |
# File 'lib/aliyunoss/oss_request.rb', line 16 def initialize(bucket, path, domain = nil, queries = {}, headers = {}) @bucket = bucket @path = path @queries = queries @domain = domain @headers = {"Content-Type" => "", "Content-MD5" => "", "Date" => Time.now.utc.strftime('%a, %d %b %Y %H:%M:%S GMT')}.merge(headers) end |
Instance Attribute Details
#body ⇒ Object
Returns the value of attribute body.
11 12 13 |
# File 'lib/aliyunoss/oss_request.rb', line 11 def body @body end |
#bucket ⇒ Object
Returns the value of attribute bucket.
11 12 13 |
# File 'lib/aliyunoss/oss_request.rb', line 11 def bucket @bucket end |
#domain ⇒ Object
Returns the value of attribute domain.
11 12 13 |
# File 'lib/aliyunoss/oss_request.rb', line 11 def domain @domain end |
#path ⇒ Object
Returns the value of attribute path.
11 12 13 |
# File 'lib/aliyunoss/oss_request.rb', line 11 def path @path end |
#queris ⇒ Object
Returns the value of attribute queris.
11 12 13 |
# File 'lib/aliyunoss/oss_request.rb', line 11 def queris @queris end |
Class Method Details
.add_operation(verb) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/aliyunoss/oss_request.rb', line 42 def self.add_operation(verb) define_method(verb) do uri = get_uri request = Net::HTTP.send(:const_get, verb.to_s.capitalize).new(uri) @headers.each_pair {|k,v| request[k] = v} if @body request.body = @body digest = OpenSSL::Digest::MD5.digest(@body) request['Content-MD5'] = Base64.encode64(digest).strip request['Content-Length'] = @body.bytesize end request['Authorization'] = 'OSS ' + access_key_id + ':' + signature(request) logger.info(verb.to_s.upcase + ' ' + uri.to_s + ' ' + request.to_hash.to_s) response = nil Net::HTTP.start(uri.host, uri.port, :use_ssl => true) do |http| response = http.request(request) logger.info(response.code.to_s + ' ' + response.) end response end end |
Instance Method Details
#[](key) ⇒ Object
Get http header value by attribute
116 117 118 |
# File 'lib/aliyunoss/oss_request.rb', line 116 def [](key) @headers[key] end |
#[]=(key, value) ⇒ Object
Set http header value by attribute
123 124 125 |
# File 'lib/aliyunoss/oss_request.rb', line 123 def []=(key, value) @headers[key] = value end |
#get_uri ⇒ Object
Get complete url for this request
27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/aliyunoss/oss_request.rb', line 27 def get_uri if @domain uri = URI("https://#{@domain}/") else if @bucket uri = URI("https://#{@bucket.name}.#{@bucket.location}.#{host}") else uri = URI("https://oss.#{host}") end end uri.path = @path uri.query = @queries.to_query_string if @queries.count > 0 uri end |
#headers_for_write(filename: nil, content_type:, content_length:, checksum:, custom_metadata: {}) ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/aliyunoss/oss_request.rb', line 92 def headers_for_write(filename: nil, content_type:, content_length:, checksum:, custom_metadata: {}) @headers = { "Content-Type" => content_type, "Content-MD5" => checksum, "Date" => Time.now.utc.strftime('%a, %d %b %Y %H:%M:%S GMT'), "Content-Length" => content_length } @headers["x-oss-date"] = @headers["Date"] if filename != nil @headers["Content-Disposition"] = "attachment; filename=\"#{filename}\"" end request = Net::HTTP.send(:const_get, 'Put').new(get_uri) @headers.each_pair {|k,v| request[k] = v} @headers['Authorization'] = 'OSS ' + access_key_id + ':' + signature(request) @headers end |
#url_for_sharing(expires_in) ⇒ Object
Get sharing url for this request, pass expires_in as parameter.
80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/aliyunoss/oss_request.rb', line 80 def url_for_sharing(expires_in) uri = get_uri request = Net::HTTP::Get.new(uri) @headers.each_pair {|k,v| request[k] = v} expires_at = (Time.now + expires_in).utc.to_i request["Date"] = expires_at uri.query = URI.encode_www_form({"OSSAccessKeyId" => access_key_id, "Expires" => expires_at, "Signature" => signature(request)}) uri.to_s end |