1#include <Common/config.h>
2
3#if USE_AWS_S3
4
5#include <IO/ReadBufferFromS3.h>
6#include <IO/ReadBufferFromIStream.h>
7
8#include <common/logger_useful.h>
9#include <aws/s3/model/GetObjectRequest.h>
10#include <aws/s3/S3Client.h>
11
12namespace DB
13{
14
15namespace ErrorCodes
16{
17 extern const int S3_ERROR;
18}
19
20
21ReadBufferFromS3::ReadBufferFromS3(const std::shared_ptr<Aws::S3::S3Client> & client_ptr,
22 const String & bucket,
23 const String & key,
24 size_t buffer_size_): ReadBuffer(nullptr, 0)
25{
26 Aws::S3::Model::GetObjectRequest req;
27 req.SetBucket(bucket);
28 req.SetKey(key);
29
30 Aws::S3::Model::GetObjectOutcome outcome = client_ptr->GetObject(req);
31
32 if (outcome.IsSuccess())
33 {
34 read_result = outcome.GetResultWithOwnership();
35 impl = std::make_unique<ReadBufferFromIStream>(read_result.GetBody(), buffer_size_);
36 }
37 else
38 throw Exception(outcome.GetError().GetMessage(), ErrorCodes::S3_ERROR);
39}
40
41bool ReadBufferFromS3::nextImpl()
42{
43 if (!impl->next())
44 return false;
45 internal_buffer = impl->buffer();
46 working_buffer = internal_buffer;
47 return true;
48}
49
50}
51
52#endif
53