class UpYun

Attributes

bucket[RW]
endpoint[R]
password[RW]
username[RW]

Public Class Methods

new(bucket, username, password, endpoint=0) click to toggle source
# File lib/upyun-sdk.rb, line 13
def initialize(bucket, username, password, endpoint=0)
  @bucket = bucket
  @username = username
  @password = password
  self.endpoint = endpoint
end

Public Instance Methods

delete(path) click to toggle source

删除目录或文件

# File lib/upyun-sdk.rb, line 66
def delete(path)
  res = http_request('DELETE', path)
  res_msg(res)
end
endpoint=(value) click to toggle source

public API

# File lib/upyun-sdk.rb, line 22
def endpoint=(value)
  @endpoint = "v#{value}.api.upyun.com"
end
get(path) click to toggle source

下载文件

# File lib/upyun-sdk.rb, line 38
def get(path)
  res = http_request('GET', path)
  res_msg(res, res.body)
end
getinfo(path) click to toggle source

获取文件信息

# File lib/upyun-sdk.rb, line 44
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='/') click to toggle source

获取目录文件列表

# File lib/upyun-sdk.rb, line 72
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) click to toggle source

创建目录

# File lib/upyun-sdk.rb, line 58
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={}) click to toggle source

上传文件

# File lib/upyun-sdk.rb, line 27
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='/') click to toggle source

获取空间使用情况

# File lib/upyun-sdk.rb, line 84
def usage(path='/')
  res = http_request('GET', "#{path}?usage")
  res_msg(res, res.body)
end

Private Instance Methods

errmsg(res) click to toggle source
# File lib/upyun-sdk.rb, line 95
def errmsg(res)
  { :code => res.code, :message => res.message }
end
http_request(method, path, data=nil, headers={}) click to toggle source
# File lib/upyun-sdk.rb, line 99
def http_request(method, path, data=nil, headers={})
  url = "http://#{@endpoint}/#{@bucket}#{path}"
  uri = URI.parse(URI.encode(url))

  date = Time.now.utc.strftime('%a, %d %b %Y %H:%M:%S GMT')
  length = headers['Content-Length'] || 0
  headers['Date'] = date
  headers['Authorization'] = make_signature(method, uri.path, date, length)
  Net::HTTP.start(uri.host, uri.port) do |http|
    if method == 'HEAD'
      return http.head(uri.request_uri, headers)
    else
      return http.send_request(method, uri.request_uri, data, headers)
    end
  end
end
make_signature(method, uri, date, length) click to toggle source
# File lib/upyun-sdk.rb, line 116
def make_signature(method, uri, date, length)
  signature = "#{method}&#{uri}&#{date}&#{length}&#{md5(@password)}"
  "UpYun #{@username}:#{md5(signature)}"
end
md5(data) click to toggle source
# File lib/upyun-sdk.rb, line 121
def md5(data)
  Digest::MD5.hexdigest(data)
end
res_msg(res, msg=true) click to toggle source
# File lib/upyun-sdk.rb, line 91
def res_msg(res, msg=true)
  (res.code.eql? '200') ? msg : errmsg(res)
end