Class: CarrierWave::Storage::Aliyun::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/carrierwave/storage/aliyun.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Connection

Returns a new instance of Connection.



14
15
16
17
18
19
20
21
22
23
# File 'lib/carrierwave/storage/aliyun.rb', line 14

def initialize(options={})
  @aliyun_access_id = options[:aliyun_access_id]
  @aliyun_access_key = options[:aliyun_access_key]
  @aliyun_bucket = options[:aliyun_bucket]
  @aliyun_host = "oss.aliyuncs.com"
  if options[:aliyun_internal] == true
    @aliyun_host = "oss-internal.aliyuncs.com"
  end
  @http = Net::HTTP.new(@aliyun_host)
end

Instance Method Details

#delete(path) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/carrierwave/storage/aliyun.rb', line 40

def delete path
  path = "#{@aliyun_bucket}/#{path}"
  url = "http://#{@aliyun_host}/#{path}"
  date = Time.now.gmtime.strftime("%a, %d %b %Y %H:%M:%S GMT")
  headers =           {
      "Authorization" => sign("DELETE", path, "", "", date),
      "Date" => date,
      "Host" => @aliyun_host,
  }
  RestClient.delete URI.escape(url), headers
end

#get(path, options = {}) ⇒ Object



33
34
35
36
37
38
# File 'lib/carrierwave/storage/aliyun.rb', line 33

def get(path, options={})
  path = "#{@aliyun_bucket}/#{path}"
  url = "http://#{@aliyun_host}/#{path}"
  headers = generate_header "GET", path, "", options
  RestClient.get URI.escape(url), headers
end

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



25
26
27
28
29
30
31
# File 'lib/carrierwave/storage/aliyun.rb', line 25

def put(path, file, options={})
  content_md5 = Digest::MD5.hexdigest(file)
  path = "#{@aliyun_bucket}/#{path}"
  url = "http://#{@aliyun_host}/#{path}"
  headers = generate_header("PUT", path, content_md5, options).merge!({"Content-Length" => file.length})
  RestClient.put URI.escape(url), file, headers
end