Skip to content

Commit 10966ae

Browse files
author
shuwen5
committed
add make_aws_put_s3_writer
1 parent 1fce89a commit 10966ae

1 file changed

Lines changed: 47 additions & 1 deletion

File tree

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)
@@ -255,4 +257,48 @@ function _M.make_quorum_http_writers(dests, writer_opts, quorum)
255257
return nil, 'NotEnoughConnect', to_str('quorum:', quorum, ", actual:", n_ok)
256258
end
257259

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

0 commit comments

Comments
 (0)