userver: userver/s3api/clients/s3api.hpp Source File
Loading...
Searching...
No Matches
s3api.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/s3api/clients/s3api.hpp
4/// @brief Client for any S3 api service
5
6#include <chrono>
7#include <optional>
8#include <string>
9#include <unordered_set>
10#include <vector>
11
12#include <userver/http/header_map.hpp>
13
14#include <userver/s3api/authenticators/access_key.hpp>
15#include <userver/s3api/authenticators/interface.hpp>
16#include <userver/s3api/models/fwd.hpp>
17#include <userver/s3api/models/s3api_connection_type.hpp>
18
19USERVER_NAMESPACE_BEGIN
20
21namespace clients::http {
22class Client;
23}
24
25/// @brief Top namespace for S3 library.
26///
27/// For more information see @ref s3_library.
28namespace s3api {
29
30class AuthHeaderConflictError : public std::runtime_error {
31 using runtime_error::runtime_error;
32};
33
34class NoBucketError : public std::runtime_error {
35 using runtime_error::runtime_error;
36};
37
38class ListBucketError : public std::runtime_error {
39 using runtime_error::runtime_error;
40};
41
42/// Connection settings - retries, timeouts, and so on
44 explicit ConnectionCfg(
45 std::chrono::milliseconds s3timeout,
46 int s3retries = 1,
47 std::optional<std::string> proxy = {}
48 )
49 : timeout(s3timeout), retries(s3retries), proxy(proxy) {}
50
51 std::chrono::milliseconds timeout{1000};
52 int retries = 1;
53 std::optional<std::string> proxy{};
54};
55
56/// https://docs.aws.amazon.com/AmazonS3/latest/API/API_ListObjects.html
57/// may also include other fields like Owner, ETag, etc.
58struct ObjectMeta {
59 std::string key;
60 std::size_t size;
61 std::string last_modified;
62};
63
64/// Represents a connection to s3 api. This object is only forward-declared,
65/// with private implementation (mostly because it is very very ugly)
66class S3Connection;
67
68/// Create an S3Connection object. By itself, it does nothing, but you need
69/// one to create S3 client
70std::shared_ptr<S3Connection> MakeS3Connection(
71 clients::http::Client& http_client,
72 S3ConnectionType connection_type,
73 std::string server_url,
74 const ConnectionCfg& params
75);
76
77/// @ingroup userver_clients
78///
79/// Main interface for the S3 api
80class Client {
81public:
82 using CiUnorderedMap = USERVER_NAMESPACE::http::headers::HeaderMap;
83 using Meta = CiUnorderedMap;
84 using Headers = CiUnorderedMap;
85
87 HeaderDataRequest() {}
88 HeaderDataRequest(std::optional<std::unordered_set<std::string>> headers, bool need_meta)
89 : headers(std::move(headers)), need_meta(need_meta) {}
90 std::optional<std::unordered_set<std::string>> headers{std::nullopt};
91 bool need_meta{true};
92 };
93
95 std::optional<Headers> headers;
96 std::optional<Meta> meta;
97 };
98
99 struct Tag {
100 std::string key;
101 std::string value;
102 };
103
104 virtual ~Client() = default;
105
106 // NOLINTBEGIN(google-default-arguments)
107
108 virtual std::string PutObject(
109 std::string_view path,
110 std::string data,
111 const std::optional<Meta>& meta = std::nullopt,
112 std::string_view content_type = "text/plain",
113 const std::optional<std::string>& content_disposition = std::nullopt,
114 const std::optional<std::vector<Tag>>& tags = std::nullopt
115 ) const = 0;
116
117 virtual void DeleteObject(std::string_view path) const = 0;
118
119 virtual std::optional<std::string> GetObject(
120 std::string_view path,
121 std::optional<std::string> version = std::nullopt,
122 HeadersDataResponse* headers_data = nullptr,
123 const HeaderDataRequest& headers_request = HeaderDataRequest()
124 ) const = 0;
125
126 virtual std::string TryGetObject(
127 std::string_view path,
128 std::optional<std::string> version = std::nullopt,
129 HeadersDataResponse* headers_data = nullptr,
130 const HeaderDataRequest& headers_request = HeaderDataRequest()
131 ) const = 0;
132
133 virtual std::optional<std::string> GetPartialObject(
134 std::string_view path,
135 std::string_view range,
136 std::optional<std::string> version = std::nullopt,
137 HeadersDataResponse* headers_data = nullptr,
138 const HeaderDataRequest& headers_request = HeaderDataRequest()
139 ) const = 0;
140
141 virtual std::string TryGetPartialObject(
142 std::string_view path,
143 std::string_view range,
144 std::optional<std::string> version = std::nullopt,
145 HeadersDataResponse* headers_data = nullptr,
146 const HeaderDataRequest& headers_request = HeaderDataRequest()
147 ) const = 0;
148
149 virtual std::string CopyObject(
150 std::string_view key_from,
151 std::string_view bucket_to,
152 std::string_view key_to,
153 const std::optional<Meta>& meta = std::nullopt
154 ) = 0;
155
156 virtual std::string
157 CopyObject(std::string_view key_from, std::string_view key_to, const std::optional<Meta>& meta = std::nullopt) = 0;
158
159 virtual std::optional<HeadersDataResponse>
160 GetObjectHead(std::string_view path, const HeaderDataRequest& request = HeaderDataRequest()) const = 0;
161
162 virtual std::string GenerateDownloadUrl(std::string_view path, time_t expires, bool use_ssl = false) const = 0;
163
164 virtual std::string GenerateDownloadUrlVirtualHostAddressing(
165 std::string_view path,
166 const std::chrono::system_clock::time_point& expires_at,
167 std::string_view protocol = "https://"
168 ) const = 0;
169
170 virtual std::string GenerateUploadUrlVirtualHostAddressing(
171 std::string_view data,
172 std::string_view content_type,
173 std::string_view path,
174 const std::chrono::system_clock::time_point& expires_at,
175 std::string_view protocol = "https://"
176 ) const = 0;
177
178 virtual std::optional<std::string> ListBucketContents(
179 std::string_view path,
180 int max_keys,
181 std::string marker = "",
182 std::string delimiter = ""
183 ) const = 0;
184
185 virtual std::vector<ObjectMeta> ListBucketContentsParsed(std::string_view path_prefix) const = 0;
186
187 virtual std::vector<std::string> ListBucketDirectories(std::string_view path_prefix) const = 0;
188
189 virtual void UpdateConfig(ConnectionCfg&& config) = 0;
190
191 virtual std::string_view GetBucketName() const = 0;
192
193 // NOLINTEND(google-default-arguments)
194};
195
196using ClientPtr = std::shared_ptr<Client>;
197
198ClientPtr GetS3Client(
199 std::shared_ptr<S3Connection> s3conn,
200 std::shared_ptr<authenticators::AccessKey> authenticator,
201 std::string bucket
202);
203
204ClientPtr GetS3Client(
205 std::shared_ptr<S3Connection> s3conn,
206 std::shared_ptr<authenticators::Authenticator> authenticator,
207 std::string bucket
208);
209
210} // namespace s3api
211
212USERVER_NAMESPACE_END