|
1 | 1 | local httplib = require("pipe.httplib") |
2 | 2 | local strutil = require("acid.strutil") |
3 | | -local to_str = strutil.to_str |
4 | 3 | local rpc_logging = require("acid.rpc_logging") |
5 | 4 | 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") |
6 | 7 |
|
7 | 8 | local _M = { _VERSION = '1.0' } |
8 | 9 |
|
| 10 | +local to_str = strutil.to_str |
9 | 11 | local INF = math.huge |
10 | 12 |
|
11 | 13 | local function write_data_to_ngx(pobj, ident, opts) |
@@ -255,4 +257,48 @@ function _M.make_quorum_http_writers(dests, writer_opts, quorum) |
255 | 257 | return nil, 'NotEnoughConnect', to_str('quorum:', quorum, ", actual:", n_ok) |
256 | 258 | end |
257 | 259 |
|
| 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 | + |
258 | 304 | return _M |
0 commit comments