|
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)
|
@@ -253,4 +255,48 @@ function _M.make_quorum_http_writers(dests, writer_opts, quorum)
|
253 | 255 | return nil, 'NotEnoughConnect', to_str('quorum:', quorum, ", actual:", n_ok)
|
254 | 256 | end
|
255 | 257 |
|
| 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 | + |
256 | 302 | return _M
|
0 commit comments