-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathoss.lua
164 lines (151 loc) · 5.19 KB
/
oss.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
local http = require 'socket.http'
local ltn12 = require 'ltn12'
local crypto = require 'crypto'
local mimetypes = require 'mimetypes'
local oss = {
bucket = "",
access_id = "",
access_key = ""
}
local conn = {}
function oss:Connect(options)
self.bucket = options["bucket"]
self.access_id = options["access_id"]
self.access_key = options["access_key"]
self.host = "http://"..self.bucket.."."..options["endpoint"]..".aliyuncs.com"
return setmetatable(conn,{
__index = oss
})
end
function conn:Put(bytes,object)
local url = self.host.."/"..object
local method = "PUT"
local mime = mimetypes.guess(object)
local result,code,headers,status = http.request{
url = url,
method = method,
headers = {
["Content-Length"] = #bytes,
["Content-Md5"] = self:hex_to_base64(ngx.md5(bytes)),
["Content-Type"] = mime,
["Date"] = ngx.http_time(ngx.now()),
["Authorization"] = self:sign(method,object,mime,bytes)
},
source = ltn12.source.string(bytes)
}
return {result=result,code=code,headers=headers,status=status},url
end
function conn:Get(object)
local url = self.host.."/"..object
local method = "GET"
local mime = "application/x-www-form-urlencoded"
local body = {}
local result,code,headers,status = http.request{
url = url,
method = method,
headers = {
["Content-Type"] = mime,
["Date"] = ngx.http_time(ngx.now()),
["Authorization"] = self:sign(method,object,mime)
},
sink = ltn12.sink.table(body)
}
return {result=result,code=code,headers=headers,status=status},table.concat(body)
end
function conn:InitUpload(object,options)
local url = self.host.."/"..object.."?uploads"
local method = "POST"
local mime = mimetypes.guess(object)
local body = {}
local disposition = ""
if options and options["disposition"] then
disposition = options["disposition"]
end
local result,code,headers,status = http.request{
url = url,
method = method,
headers = {
["Content-Type"] = mime,
["Date"] = ngx.http_time(ngx.now()),
["Authorization"] = self:sign(method.."\n",object.."?uploads",mime),
["Content-Disposition"] = "attachment; filename=\""..disposition.."\""
},
sink = ltn12.sink.table(body)
}
return {result=result,code=code,headers=headers,status=status},table.concat(body)
end
function conn:UploadPart(bytes,object,partNum,uploadId)
local url = self.host.."/"..object.."?partNumber="..partNum.."&uploadId="..uploadId
local method = "PUT"
local mime = mimetypes.guess(object)
local result,code,headers,status = http.request{
url = url,
method = method,
headers = {
["Content-Type"] = mime,
["Date"] = ngx.http_time(ngx.now()),
["Authorization"] = self:sign(method.."\n",object.."?partNumber="..partNum.."&uploadId="..uploadId,mime),
["Content-Length"] = #bytes
},
source = ltn12.source.string(bytes)
}
return {result=result,code=code,headers=headers,status=status}
end
function conn:AbortUpload(object,uploadId)
local url = self.host.."/"..object.."?uploadId="..uploadId
local method = "DELETE"
local result,code,headers,status = http.request{
url = url,
method = method,
headers = {
["Date"] = ngx.http_time(ngx.now()),
["Authorization"] = self:sign(method.."\n\n",object.."?".."uploadId="..uploadId)
}
}
return {result=result,code=code,headers=headers,status=status}
end
function conn:CompleteUpload(bytes,object,uploadId)
local url = self.host.."/"..object.."?uploadId="..uploadId
local method = "POST"
local mime = mimetypes.guess(object)
local body = {}
local result,code,headers,status = http.request{
url = url,
method = method,
headers = {
["Content-Length"] = #bytes,
["Content-Md5"] = self:hex_to_base64(ngx.md5(bytes)),
["Content-Type"] = mime,
["Date"] = ngx.http_time(ngx.now()),
["Authorization"] = self:sign(method,object.."?uploadId="..uploadId,mime,bytes),
},
source = ltn12.source.string(bytes),
sink = ltn12.sink.table(body)
}
return {result=result,code=code,headers=headers,status=status},table.concat(body)
end
function conn:sign(method,object,mime,bytes)
local LF = "\n"
if method == "GET" then
method = method..LF
end
local sign = method..LF
if bytes then
sign = sign..self:hex_to_base64(ngx.md5(bytes))..LF
end
if mime then
sign = sign..mime..LF
end
sign = sign..ngx.http_time(ngx.now())..LF
sign = sign.."/"..self.bucket.."/"..object
return "OSS "..self.access_id..":"..ngx.encode_base64(crypto.hmac.digest("sha1",sign,self.access_key,true))
end
function conn:hex_to_base64(str)
local result = ''
for i = 1, #str, 2 do
local tmp = string.sub(str, i, i+1)
result = result..string.char(tonumber(tmp,16))
end
return ngx.encode_base64(result)
end
return oss