forked from pinterest/rocksplicator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paths3util.h
100 lines (84 loc) · 2.97 KB
/
s3util.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/// Copyright 2016 Pinterest Inc.
///
/// Licensed under the Apache License, Version 2.0 (the "License");
/// you may not use this file except in compliance with the License.
/// You may obtain a copy of the License at
///
/// http://www.apache.org/licenses/LICENSE-2.0
/// Unless required by applicable law or agreed to in writing, software
/// distributed under the License is distributed on an "AS IS" BASIS,
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
/// See the License for the specific language governing permissions and
/// limitations under the License.
//
// @author shu ([email protected])
//
#pragma once
#include <aws/s3/S3Client.h>
#include <aws/core/client/ClientConfiguration.h>
#include <string>
#include <tuple>
#include <vector>
using std::string;
using std::vector;
using std::tuple;
using std::shared_ptr;
using Aws::S3::S3Client;
using Aws::Client::ClientConfiguration;
namespace common {
template <class T>
class S3UtilResponse {
public:
S3UtilResponse(T body, string error):
body_(std::move(body)), error_(std::move(error)) {}
T Body() {
return body_;
}
string Error() {
return error_;
}
private:
T body_;
string error_;
};
using GetObjectResponse = S3UtilResponse<bool>;
using SdkGetObjectResponse = Aws::S3::Model::GetObjectOutcome;
using ListObjectsResponse = S3UtilResponse<vector<string>>;
using GetObjectsResponse = S3UtilResponse<vector<GetObjectResponse>>;
class S3Util {
public:
// Don't recommend using this directly. Using BuildS3Util instead.
S3Util(const string& bucket,
const ClientConfiguration& client_config) :
bucket_(std::move(bucket)), s3Client(std::move(client_config)) {}
// Download an S3 Object to a local file
GetObjectResponse getObject(const string& key, const string& local_path);
// Get object using s3client
SdkGetObjectResponse sdkGetObject(const string& key,
const string& local_path="");
// Return a list of objects under the prefix.
ListObjectsResponse listObjects(const string& prefix);
// Download all objects under a prefix. We only assume
// For each object downloading,
// if the download is successful, the error message will be
// the object key.
// If not true, the error message will be the error message.
GetObjectsResponse getObjects(
const string& prefix, const string& local_directory,
const string& delimiter = "/");
// Some utility methods
// Given an s3 full path like "s3://<bucket>/<path>",
// return a tuple of bucketname and file path.
static tuple<string, string> parseFullS3Path(const string& s3_path);
static shared_ptr<S3Util> BuildS3Util(
const uint32_t read_ratelimit_mb = 50,
const string& bucket = "",
const uint32_t connect_timeout_ms = 60000,
const uint32_t request_timeout_ms = 60000);
private:
const string bucket_;
// S3Client is thread safe:
// https://github.com/aws/aws-sdk-cpp/issues/166
S3Client s3Client;
};
} // namespace common