Class: Aliyun::OSS::HTTP

Inherits:
Object
  • Object
show all
Includes:
Common::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'
STS_HEADER =
'x-oss-security-token'
OPEN_TIMEOUT =
10
READ_TIMEOUT =
120

Constants included from Common::Logging

Common::Logging::DEFAULT_LOG_FILE, Common::Logging::MAX_NUM_LOG, Common::Logging::ROTATE_SIZE

Instance Method Summary collapse

Methods included from Common::Logging

#logger, set_log_file, set_log_level

Constructor Details

#initialize(config) ⇒ HTTP

Returns a new instance of HTTP.



130
131
132
# File 'lib/aliyun/oss/http.rb', line 130

def initialize(config)
  @config = config
end

Instance Method Details

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



180
181
182
# File 'lib/aliyun/oss/http.rb', line 180

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

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

helper methods



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

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

#get_request_url(bucket, object) ⇒ Object



134
135
136
137
138
139
140
141
142
# File 'lib/aliyun/oss/http.rb', line 134

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



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

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



153
154
155
156
157
158
159
160
161
162
163
# File 'lib/aliyun/oss/http.rb', line 153

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



184
185
186
# File 'lib/aliyun/oss/http.rb', line 184

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

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



188
189
190
# File 'lib/aliyun/oss/http.rb', line 188

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

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



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

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

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



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

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