Class: UpYun

Inherits:
Object
  • Object
show all
Defined in:
lib/upyun-sdk.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(bucket, username, password, endpoint = 0) ⇒ UpYun

Returns a new instance of UpYun.



14
15
16
17
18
19
# File 'lib/upyun-sdk.rb', line 14

def initialize(bucket, username, password, endpoint=0)
  @bucket = bucket
  @username = username
  @password = password
  self.endpoint = endpoint
end

Instance Attribute Details

#bucketObject

Returns the value of attribute bucket.



11
12
13
# File 'lib/upyun-sdk.rb', line 11

def bucket
  @bucket
end

#endpointObject

Returns the value of attribute endpoint.



12
13
14
# File 'lib/upyun-sdk.rb', line 12

def endpoint
  @endpoint
end

#passwordObject

Returns the value of attribute password.



11
12
13
# File 'lib/upyun-sdk.rb', line 11

def password
  @password
end

#usernameObject

Returns the value of attribute username.



11
12
13
# File 'lib/upyun-sdk.rb', line 11

def username
  @username
end

Instance Method Details

#delete(path) ⇒ Object

删除目录或文件



67
68
69
70
# File 'lib/upyun-sdk.rb', line 67

def delete(path)
  res = http_request('DELETE', path)
  res_msg(res)
end

#get(path) ⇒ Object

下载文件



39
40
41
42
# File 'lib/upyun-sdk.rb', line 39

def get(path)
  res = http_request('GET', path)
  res_msg(res, res.body)
end

#getinfo(path) ⇒ Object

获取文件信息



45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/upyun-sdk.rb', line 45

def getinfo(path)
  res = http_request('HEAD', path)
  if res.code.eql? '200'
    {
      :file_type => res['x-upyun-file-type'],
      :file_size => res['x-upyun-file-size'],
      :file_date => res['x-upyun-file-date']
    }
  else
    errmsg(res)
  end
end

#getlist(path = '/') ⇒ Object

获取目录文件列表



73
74
75
76
77
78
79
80
81
82
# File 'lib/upyun-sdk.rb', line 73

def getlist(path='/')
  res = http_request('GET', path)
  if res.code.eql? '200'
    res.body.split("\n").map do |file|
      Hash[[:name, :type, :size, :time].zip(file.split("\t"))]
    end
  else
    errmsg(res)
  end
end

#mkdir(path, auto_mkdir = true) ⇒ Object

创建目录



59
60
61
62
63
64
# File 'lib/upyun-sdk.rb', line 59

def mkdir(path, auto_mkdir=true)
  headers = {'folder' => 'true'}
  headers['mkdir'] = auto_mkdir.to_s
  res = http_request('POST', path, nil, headers)
  res_msg(res)
end

#put(path, data, auto_mkdir = true, checksum = nil, options = {}) ⇒ Object

上传文件



28
29
30
31
32
33
34
35
36
# File 'lib/upyun-sdk.rb', line 28

def put(path, data, auto_mkdir=true, checksum=nil, options={})
  headers = {}
  headers['mkdir'] = auto_mkdir.to_s
  headers['Content-MD5'] = checksum if checksum
  headers['Content-Length'] = data.size.to_s
  headers.merge(options)
  res = http_request('PUT', path, data, headers)
  res_msg(res)
end

#usage(path = '/') ⇒ Object

获取空间使用情况



85
86
87
88
# File 'lib/upyun-sdk.rb', line 85

def usage(path='/')
  res = http_request('GET', "#{path}?usage")
  res_msg(res, res.body)
end