Class: Upyun::Bucket

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

Instance Method Summary collapse

Constructor Details

#initialize(bucket, operator, password, options = {}) ⇒ Bucket

Returns a new instance of Bucket.



3
4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/upyun-rb/bucket.rb', line 3

def initialize(bucket, operator, password, options={})
  upyun_host = options[:upyun_host] || "http://v0.api.upyun.com"

  @bucket = bucket
  @operator = operator
  @signed_password = Digest::MD5.hexdigest(password)

  @connection = Faraday.new(:url => upyun_host) do |faraday|
    faraday.request  :url_encoded             # form-encode POST params
    faraday.response :logger                  # log requests to STDOUT
    faraday.adapter  Faraday.default_adapter  # make requests with Net::HTTP
  end
end

Instance Method Details

#delete(path) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/upyun-rb/bucket.rb', line 49

def delete(path)
  uri = "/#{@bucket}/#{path}"
  date = Upyun::Util.current_date
  sign = Digest::MD5.hexdigest("DELETE&#{uri}&#{date}&0&#{@signed_password}")
  
  @connection.delete do |req|
    req.url uri
    req.headers['Authorization'] = "UpYun #{@operator}:#{sign}"
    req.headers['Date'] = date
    req.headers['Expect'] = ''
  end
end

#head(path) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/upyun-rb/bucket.rb', line 17

def head(path)
  uri = "/#{@bucket}/#{path}"
  date = Upyun::Util.current_date
  sign = Digest::MD5.hexdigest("HEAD&#{uri}&#{date}&0&#{@signed_password}")

  @connection.head do |req|
    req.url uri
    req.headers['Authorization'] = "UpYun #{@operator}:#{sign}"
    req.headers['Date'] = date
    req.headers['Expect'] = ''
  end
end

#put(path, file) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/upyun-rb/bucket.rb', line 30

def put(path, file)
  uri = "/#{@bucket}/#{path}"
  date = Upyun::Util.current_date
  mime_type = `file -b --mime #{file}`.gsub(/\n/,"").split(';')[0]
  file_size = file.nil? ? 0 : File.size(file) 
  sign = Digest::MD5.hexdigest("PUT&#{uri}&#{date}&#{file_size}&#{@signed_password}")

  @connection.put do |req|
    req.url uri
    req.body = Faraday::UploadIO.new(file, mime_type)
    req.headers['Mkdir'] = 'true'
    req.headers['Content-Type'] = mime_type
    req.headers['Content-Length'] = file_size.to_s
    req.headers['Authorization'] = "UpYun #{@operator}:#{sign}"
    req.headers['Date'] = date
    req.headers['Expect'] = ''
  end
end