Class: ROSS::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/ross/client.rb

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Client

Returns a new instance of Client.



7
8
9
10
11
12
13
14
15
16
# File 'lib/ross/client.rb', line 7

def initialize(options)
  return nil unless options
  @bucket_name = options[:bucket_name]
  @appid = options[:appid]
  @appkey = options[:appkey]
  @aliyun_host = "oss.aliyuncs.com"
  if options[:aliyun_internal] == true
    @aliyun_host = "oss-internal.aliyuncs.com"
  end
end

Instance Method Details

#get(path) ⇒ Object



36
37
38
# File 'lib/ross/client.rb', line 36

def get(path)
  return "http://#{@bucket_name}.#{@aliyun_host}/#{path}"
end

#put(path, file, options = {}) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/ross/client.rb', line 18

def put(path, file, options={})
  content_md5 = Digest::MD5.hexdigest(file)
  content_type = options[:content_type] || "image/jpg"
  date = Time.now.gmtime.strftime("%a, %d %b %Y %H:%M:%S GMT")
  path = "#{@bucket_name}/#{path}"
  url = "http://#{@aliyun_host}/#{path}"
  auth_sign = sign("PUT", path, content_md5, content_type, date)
  headers = {
              "Authorization" => auth_sign, 
              "Content-Type" => content_type,
              "Content-Length" => file.length,
              "Date" => date,
              "Host" => @aliyun_host,
            }
            
  response = RestClient.put(url, file, headers)
end