Skip to content

Commit 62d8779

Browse files
author
shuwen5
committed
add make_aws_put_s3_writer
1 parent 433dce7 commit 62d8779

File tree

1 file changed

+47
-1
lines changed

1 file changed

+47
-1
lines changed

lib/pipe/writer.lua

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
local httplib = require("pipe.httplib")
22
local strutil = require("acid.strutil")
3-
local to_str = strutil.to_str
43
local rpc_logging = require("acid.rpc_logging")
54
local acid_setutil = require("acid.setutil")
5+
local s3_client = require('resty.aws_s3.client')
6+
local aws_chunk_writer = require("resty.aws_chunk.writer")
67

78
local _M = { _VERSION = '1.0' }
89

10+
local to_str = strutil.to_str
911
local INF = math.huge
1012

1113
local function write_data_to_ngx(pobj, ident, opts)
@@ -253,4 +255,48 @@ function _M.make_quorum_http_writers(dests, writer_opts, quorum)
253255
return nil, 'NotEnoughConnect', to_str('quorum:', quorum, ", actual:", n_ok)
254256
end
255257

258+
function _M.make_aws_put_s3_writer(access_key, secret_key, endpoint, params, opts)
259+
local s3_cli, err_code, err_msg =
260+
s3_client.new(access_key, secret_key, endpoint, opts)
261+
if err_code ~= nil then
262+
return nil, err_code, err_msg
263+
end
264+
265+
local request, err_code, err_msg =
266+
s3_cli:get_signed_request(params, 'put_object', opts)
267+
if err_code ~= nil then
268+
return nil, err_code, err_msg
269+
end
270+
271+
return function(pobj, ident)
272+
local chunk_writer =
273+
aws_chunk_writer:new(request.signer, request.auth_ctx)
274+
275+
local _, err_code, err_msg = s3_cli:send_request(
276+
request.verb, request.uri, request.headers,request.body)
277+
if err_code ~= nil then
278+
return nil, err_code, err_msg
279+
end
280+
281+
while true do
282+
local data, err_code, err_msg = pobj:read_pipe(ident)
283+
if err_code ~= nil then
284+
return nil, err_code, err_msg
285+
end
286+
287+
local chunked_data = chunk_writer:make_chunk(data)
288+
local _, err_code, err_msg = s3_cli:send_body(chunked_data)
289+
if err_code ~= nil then
290+
return nil, err_code, err_msg
291+
end
292+
293+
if data == '' then
294+
break
295+
end
296+
end
297+
298+
return s3_cli:finish_request()
299+
end
300+
end
301+
256302
return _M

0 commit comments

Comments
 (0)