| 1 | #pragma once |
|---|---|
| 2 | |
| 3 | #include <Common/config.h> |
| 4 | |
| 5 | #if USE_AWS_S3 |
| 6 | |
| 7 | #include <memory> |
| 8 | #include <vector> |
| 9 | #include <Core/Types.h> |
| 10 | #include <IO/HTTPCommon.h> |
| 11 | #include <IO/BufferWithOwnMemory.h> |
| 12 | #include <IO/WriteBuffer.h> |
| 13 | #include <IO/WriteBufferFromString.h> |
| 14 | |
| 15 | namespace Aws::S3 |
| 16 | { |
| 17 | class S3Client; |
| 18 | } |
| 19 | |
| 20 | namespace DB |
| 21 | { |
| 22 | /* Perform S3 HTTP PUT request. |
| 23 | */ |
| 24 | class WriteBufferFromS3 : public BufferWithOwnMemory<WriteBuffer> |
| 25 | { |
| 26 | private: |
| 27 | String bucket; |
| 28 | String key; |
| 29 | std::shared_ptr<Aws::S3::S3Client> client_ptr; |
| 30 | size_t minimum_upload_part_size; |
| 31 | String buffer_string; |
| 32 | std::unique_ptr<WriteBufferFromString> temporary_buffer; |
| 33 | size_t last_part_size; |
| 34 | |
| 35 | /// Upload in S3 is made in parts. |
| 36 | /// We initiate upload, then upload each part and get ETag as a response, and then finish upload with listing all our parts. |
| 37 | String upload_id; |
| 38 | std::vector<String> part_tags; |
| 39 | |
| 40 | Logger * log = &Logger::get("WriteBufferFromS3"); |
| 41 | |
| 42 | public: |
| 43 | explicit WriteBufferFromS3(std::shared_ptr<Aws::S3::S3Client> client_ptr_, |
| 44 | const String & bucket_, |
| 45 | const String & key_, |
| 46 | size_t minimum_upload_part_size_, |
| 47 | size_t buffer_size_ = DBMS_DEFAULT_BUFFER_SIZE); |
| 48 | |
| 49 | void nextImpl() override; |
| 50 | |
| 51 | /// Receives response from the server after sending all data. |
| 52 | void finalize() override; |
| 53 | |
| 54 | ~WriteBufferFromS3() override; |
| 55 | |
| 56 | private: |
| 57 | void initiate(); |
| 58 | void writePart(const String & data); |
| 59 | void complete(); |
| 60 | }; |
| 61 | |
| 62 | } |
| 63 | |
| 64 | #endif |
| 65 |