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.



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

def initialize(options)
  return nil unless options
  @bucket_name = options[:bucket_name]
  @appid = options[:appid]
  @appkey = options[:appkey]
  @aliyun_host = options[:bucket_internal_host] || options[:bucket_host] || "oss.aliyuncs.com"
end

Instance Method Details

#copy(sour_path, dest_path) ⇒ Object



63
64
65
# File 'lib/ross/client.rb', line 63

def copy(sour_path, dest_path)
  put(dest_path, nil, {content_type: nil}, {'copy-source' => bucket_path(sour_path)})
end

#delete(path) ⇒ Object



31
32
33
34
35
36
37
38
39
40
# File 'lib/ross/client.rb', line 31

def delete(path)
  date = Time.now.gmtime.strftime("%a, %d %b %Y %H:%M:%S GMT")
  headers = {
    "Authorization" => auth_sign("DELETE", path, date),
    "Content-Type" => nil,
    "Date" => date,
    "Host" => @aliyun_host
  }
  response = RestClient.delete(request_url(path), headers)
end

#exists?(path) ⇒ Boolean

Returns:

  • (Boolean)


53
54
55
56
57
58
59
60
61
# File 'lib/ross/client.rb', line 53

def exists?(path)
  begin
    head(path)
  rescue RestClient::ResourceNotFound
    false
  else
    true
  end
end

#head(path) ⇒ Object



42
43
44
45
46
47
48
49
50
51
# File 'lib/ross/client.rb', line 42

def head(path)
  date = Time.now.gmtime.strftime("%a, %d %b %Y %H:%M:%S GMT")
  headers = {
    "Authorization" => auth_sign("HEAD", path, date),
    "Content-Type" => nil,
    "Date" => date,
    "Host" => @aliyun_host
  }
  response = RestClient.head(request_url(path), headers)
end

#private_url(path, expires_in = 3600) ⇒ Object



82
83
84
85
86
87
88
89
90
# File 'lib/ross/client.rb', line 82

def private_url(path, expires_in = 3600)
  expires_at = (8*3600 + Time.now.gmtime.to_i) + expires_in
  params = {
    'Expires' => expires_at,
    'OSSAccessKeyId' => @appid,
    'Signature' => sign('GET', path, expires_at)
  }
  public_url(path) + '?' + URI.encode_www_form(params)
end

#public_url(path) ⇒ Object Also known as: get



76
77
78
# File 'lib/ross/client.rb', line 76

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

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



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/ross/client.rb', line 16

def put(path, content, options={}, xoss = {})
  # content_md5 = Digest::MD5.hexdigest(content)
  content_type = options.has_key?(:content_type) ? options[:content_type] : "application/octet-stream"
  date = Time.now.gmtime.strftime("%a, %d %b %Y %H:%M:%S GMT")
  headers = {
    "Authorization" => auth_sign("PUT", path, date, content_type, nil, xoss),
    "Content-Type" => content_type,
    "Date" => date,
    "Host" => @aliyun_host
  }
  headers["Content-Length"] = content.length unless content.nil? # content can be nil
  xoss.each_pair{|k,v| headers["x-oss-#{k}"] = v }
  response = RestClient.put(request_url(path), content, headers)
end

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



72
73
74
# File 'lib/ross/client.rb', line 72

def put_file(path, file, options = {})
  put(path, File.read(file), options)
end

#rename(sour_path, dest_path) ⇒ Object



67
68
69
70
# File 'lib/ross/client.rb', line 67

def rename(sour_path, dest_path)
  copy(sour_path, dest_path)
  delete(sour_path)
end