Class: Aliyun::OSS::HTTP

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/aliyun/oss/http.rb

Overview

HTTP wraps the HTTP functionalities for accessing OSS RESTful API. It handles the OSS-specific protocol elements, and rest-client details for the user, which includes:

  • automatically generate signature for every request

  • parse response headers/body

  • raise exceptions and capture the request id

  • encapsulates streaming upload/download

Examples:

simple get

headers, body = http.get({:bucket => 'bucket'})

streaming download

http.get({:bucket => 'bucket', :object => 'object'}) do |chunk|
  # handle chunk
end

streaming upload

def streaming_upload(&block)
  http.put({:bucket => 'bucket', :object => 'object'},
           {:body => HTTP::StreamPlayload.new(block)})
end

streaming_upload do |stream|
  stream << "hello world"
end

Defined Under Namespace

Classes: StreamPayload, StreamWriter

Constant Summary collapse

DEFAULT_CONTENT_TYPE =
'application/octet-stream'

Constants included from Logging

Logging::DEFAULT_LOG_FILE

Instance Method Summary collapse

Methods included from Logging

#logger, set_log_file, set_log_level

Constructor Details

#initialize(config) ⇒ HTTP

Returns a new instance of HTTP.



117
118
119
# File 'lib/aliyun/oss/http.rb', line 117

def initialize(config)
  @config = config
end

Instance Method Details

#delete(resources = {}, http_options = {}, &block) ⇒ Object



167
168
169
# File 'lib/aliyun/oss/http.rb', line 167

def delete(resources = {}, http_options = {}, &block)
  do_request('DELETE', resources, http_options, &block)
end

#get(resources = {}, http_options = {}, &block) ⇒ Object

helper methods



155
156
157
# File 'lib/aliyun/oss/http.rb', line 155

def get(resources = {}, http_options = {}, &block)
  do_request('GET', resources, http_options, &block)
end

#get_request_url(bucket, object) ⇒ Object



121
122
123
124
125
126
127
128
129
# File 'lib/aliyun/oss/http.rb', line 121

def get_request_url(bucket, object)
  url = ""
  url += "#{@config.endpoint.scheme}://"
  url += "#{bucket}." if bucket and not @config.cname
  url += @config.endpoint.host
  url += "/#{CGI.escape(object)}" if object

  url
end

#get_resource_path(bucket, object) ⇒ Object



131
132
133
134
135
136
137
# File 'lib/aliyun/oss/http.rb', line 131

def get_resource_path(bucket, object)
  if bucket
    res = "/#{bucket}/"
    res += "#{object}" if object
    res
  end
end

#handle_response(r, &block) ⇒ Object

Handle Net::HTTPRespoonse



140
141
142
143
144
145
146
147
148
149
150
# File 'lib/aliyun/oss/http.rb', line 140

def handle_response(r, &block)
  # read all body on error
  if r.code.to_i >= 300
    r.read_body
  else
  # streaming read body on success
    r.read_body do |chunk|
      yield RestClient::Request.decode(r['content-encoding'], chunk)
    end
  end
end

#head(resources = {}, http_options = {}, &block) ⇒ Object



171
172
173
# File 'lib/aliyun/oss/http.rb', line 171

def head(resources = {}, http_options = {}, &block)
  do_request('HEAD', resources, http_options, &block)
end

#options(resources = {}, http_options = {}, &block) ⇒ Object



175
176
177
# File 'lib/aliyun/oss/http.rb', line 175

def options(resources = {}, http_options = {}, &block)
  do_request('OPTIONS', resources, http_options, &block)
end

#post(resources = {}, http_options = {}, &block) ⇒ Object



163
164
165
# File 'lib/aliyun/oss/http.rb', line 163

def post(resources = {}, http_options = {}, &block)
  do_request('POST', resources, http_options, &block)
end

#put(resources = {}, http_options = {}, &block) ⇒ Object



159
160
161
# File 'lib/aliyun/oss/http.rb', line 159

def put(resources = {}, http_options = {}, &block)
  do_request('PUT', resources, http_options, &block)
end