Class: Aliyun::Connection

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

Instance Method Summary collapse

Constructor Details

#initialize(options = Paperclip::Attachment.default_options[:aliyun]) ⇒ Connection

Returns a new instance of Connection.



11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/aliyun/connection.rb', line 11

def initialize(options = Paperclip::Attachment.default_options[:aliyun])
  @aliyun_access_id = options[:access_id]
  @aliyun_access_key = options[:access_key]
  @aliyun_bucket = options[:bucket]

  data_centre = options[:data_centre].to_s.downcase == 'qingdao' ? 'qingdao' : 'hangzhou'
  internal = options[:internal] == true ? true : false
  @aliyun_data_centre = "oss-cn-#{data_centre}#{internal ? '-internal' : nil}.aliyuncs.com"

  @aliyun_upload_host = "#{@aliyun_bucket}.#{@aliyun_data_centre}"

  @aliyun_host = options[:host] || @aliyun_upload_host
end

Instance Method Details

#delete(path) ⇒ Object

删除 Remote 的文件

参数:

  • path - remote 存储路径

返回值:

图片的下载地址



70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/aliyun/connection.rb', line 70

def delete(path)
  path = format_path(path)
  bucket_path = get_bucket_path(path)
  date = gmtdate
  headers = {
    "Host" => @aliyun_upload_host,
    "Date" => date,
    "Authorization" => sign("DELETE", bucket_path, "", "" ,date)
  }
  url = path_to_url(path)
  response = RestClient.delete(URI.encode(url), headers)
  response.code == 204 ? url : nil
end

#exists?(path) ⇒ Boolean

检查远程服务器是否已存在指定文件

参数:

  • path - remote 存储路径

返回值:

true/false

Returns:

  • (Boolean)


116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/aliyun/connection.rb', line 116

def exists?(path)
  path = format_path(path)
  bucket_path = get_bucket_path(path)
  date = gmtdate
  headers = {
    "Host" => @aliyun_upload_host,
    "Date" => date,
    "Authorization" => sign("HEAD", bucket_path, "", "", date)
  }
  url = path_to_url(path)

  # rest_client will throw exception if requested resource not found
  begin
    response = RestClient.head(URI.encode(url), headers)
  rescue RestClient::ResourceNotFound
    return false
  end

  true
end

#fetch_file_hostObject



25
26
27
# File 'lib/aliyun/connection.rb', line 25

def fetch_file_host
  @aliyun_host
end

#format_path(path) ⇒ Object



143
144
145
146
147
148
# File 'lib/aliyun/connection.rb', line 143

def format_path(path)
  return "" if path.blank?
  path.gsub!(/^\/+/,"")

  path
end

#get(path) ⇒ Object

下载 Remote 的文件

参数:

  • path - remote 存储路径

返回值:

请求的图片的数据流



93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/aliyun/connection.rb', line 93

def get(path)
  path = format_path(path)
  bucket_path = get_bucket_path(path)
  date = gmtdate
  headers = {
    "Host" => @aliyun_upload_host,
    "Date" => date,
    "Authorization" => sign("GET", bucket_path, "", "" ,date)
  }
  url = path_to_url(path)
  response = RestClient.get(URI.encode(url), headers)
  response.body
end

#get_bucket_path(path) ⇒ Object



150
151
152
# File 'lib/aliyun/connection.rb', line 150

def get_bucket_path(path)
  [@aliyun_bucket,path].join("/")
end

#gmtdateObject

阿里云需要的 GMT 时间格式



139
140
141
# File 'lib/aliyun/connection.rb', line 139

def gmtdate
  Time.now.gmtime.strftime("%a, %d %b %Y %H:%M:%S GMT")
end

#path_to_url(path) ⇒ Object

根据配置返回完整的上传文件的访问地址



156
157
158
# File 'lib/aliyun/connection.rb', line 156

def path_to_url(path)
  "http://#{fetch_file_host}/#{path}"
end

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

上传文件

参数:

  • path - remote 存储路径

  • file - 需要上传文件的 File 对象

  • options:

    • content_type - 上传文件的 MimeType,默认 ‘image/jpg`

返回值:

图片的下载地址



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/aliyun/connection.rb', line 41

def put(path, file, options={})
  path = format_path(path)
  bucket_path = get_bucket_path(path)
  content_md5 = Digest::MD5.file(file)
  content_type = options[:content_type] || "image/jpg"
  date = gmtdate
  url = path_to_url(path)
  auth_sign = sign("PUT", bucket_path, content_md5, content_type, date)
  headers = {
    "Authorization" => auth_sign,
    "Content-Type" => content_type,
    "Content-Length" => file.size,
    "Date" => date,
    "Host" => @aliyun_upload_host,
    "Expect" => "100-Continue"
  }
  response = RestClient.put(URI.encode(url), file, headers)
  response.code == 200 ? path_to_url(path) : nil
end