1#include <s3api/clients/client.hpp>
6#include <boost/algorithm/string.hpp>
9#include <userver/http/common_headers.hpp>
10#include <userver/http/url.hpp>
11#include <userver/logging/log.hpp>
12#include <userver/utils/algo.hpp>
13#include <userver/utils/exception.hpp>
15#include <userver/s3api/authenticators/access_key.hpp>
17#include <s3api/s3_connection.hpp>
18#include <s3api/s3api_methods.hpp>
20USERVER_NAMESPACE_BEGIN
25const std::string kMeta =
"x-amz-meta-";
26const std::string kTagging =
"X-Amz-Tagging";
27constexpr const std::size_t kMaxS3Keys = 1000;
29constexpr http::
headers::PredefinedHeader kEtagHeader{
"ETag"};
31void SaveMeta(clients::http::Headers& headers,
const ClientImpl::Meta& meta) {
32 for (
const auto& [header, value] : meta) {
33 headers
[kMeta + header
] = value;
37void ReadMeta(
const clients::http::Headers& headers,
ClientImpl::Meta& meta) {
38 for (
const auto& [header, value] : headers) {
39 if (boost::istarts_with(header, kMeta)) {
40 meta
[header.substr(kMeta.length())
] = value;
45void SaveTags(clients::http::Headers& headers,
const std::vector<
ClientImpl::
Tag>& tags) {
47 for (
const auto& [key, value] : tags) {
48 size += key.size() + value.size() + 2;
50 std::string tag_values;
51 tag_values.reserve(size);
52 for (
const auto& [key, value] : tags) {
53 tag_values.append(key);
54 tag_values.append(
"=");
55 tag_values.append(value);
56 tag_values.append(
"&");
58 if (!tag_values.empty()) {
60 tag_values.pop_back();
62 headers
[kTagging
] = std::move(tag_values);
65void AddQueryParamsToPresignedUrl(
66 std::ostringstream& generated_url,
71 if (!req.req.empty()) {
72 generated_url <<
"/" + req.req;
75 auto params = authenticator->Sign(req, expires_at);
77 if (!params.empty()) {
83std::string GeneratePresignedUrl(
85 std::string_view host,
86 std::string_view protocol,
87 const std::chrono::system_clock::time_point& expires_at,
90 std::ostringstream generated_url;
93 generated_url << protocol << request.bucket <<
"." << host;
94 const auto expires_at_time_t = std::chrono::system_clock::to_time_t(expires_at);
95 AddQueryParamsToPresignedUrl(generated_url, expires_at_time_t, request, std::move(authenticator));
96 return generated_url.str();
99std::vector<
ObjectMeta> ParseS3ListResponse(std::string_view s3_response) {
101 pugi::xml_document xml;
102 const pugi::xml_parse_result parse_result = xml.load_string(s3_response.data());
103 if (parse_result.status != pugi::status_ok) {
105 "Failed to parse S3 list response as xml, error: {}, response: {}", parse_result.description(), s3_response
109 const auto items = xml.child(
"ListBucketResult").children(
"Contents");
110 for (
const auto& item : items) {
111 const auto key = item.child(
"Key").child_value();
112 const auto size = std::stoull(item.child(
"Size").child_value());
113 const auto last_modified = item.child(
"LastModified").child_value();
114 result.push_back(
ObjectMeta{key, size, last_modified});
116 }
catch (
const pugi::xpath_exception& ex) {
118 fmt::format(
"Bad xml structure for S3 list response, error: {}, response: {}", ex.what(), s3_response)
124std::vector<std::string> ParseS3DirectoriesListResponse(std::string_view s3_response) {
125 std::vector<std::string> result;
126 pugi::xml_document xml;
127 const pugi::xml_parse_result parse_result = xml.load_string(s3_response.data());
128 if (parse_result.status != pugi::status_ok) {
130 "Failed to parse S3 directories list response as xml, error: {}, "
132 parse_result.description(),
137 const auto items = xml.child(
"ListBucketResult").children(
"CommonPrefixes");
138 for (
const auto& item : items) {
139 result.push_back(item.child(
"Prefix").child_value());
141 }
catch (
const pugi::xpath_exception& ex) {
143 "Bad xml structure for S3 directories list response, error: {}, "
161 : conn_(std::move(s3conn)), authenticator_{std::move(authenticator)}, bucket_(std::move(bucket)) {}
174std::string_view
ClientImpl::GetBucketName()
const {
return bucket_; }
177 std::string_view path,
179 const std::optional<Meta>& meta,
180 std::string_view content_type,
181 const std::optional<std::string>& content_disposition,
182 const std::optional<std::vector<
Tag>>& tags
184 auto req = api_methods::PutObject(
192 if (meta.has_value()) {
193 SaveMeta(req.headers, meta.value());
195 if (tags.has_value()) {
196 SaveTags(req.headers, tags.value());
198 return RequestApi(req,
"put_object");
201void ClientImpl::DeleteObject(std::string_view path)
const {
202 auto req = api_methods::DeleteObject(bucket_, path);
203 RequestApi(req,
"delete_object");
206std::optional<std::string>
ClientImpl::GetObject(
207 std::string_view path,
208 std::optional<std::string> version,
213 return std::make_optional(TryGetObject(path, std::move(version), headers_data, headers_request));
215 if (e.code() == 404) {
216 LOG_INFO() <<
"Can't get object with path: " << path <<
", object not found:" << e.what();
218 LOG_ERROR() <<
"Can't get object with path: " << path <<
", unknown error:" << e.what();
221 }
catch (
const std::exception& e) {
222 LOG_ERROR() <<
"Can't get object with path: " << path <<
", unknown error:" << e.what();
228 std::string_view path,
229 std::optional<std::string> version,
233 auto req = api_methods::GetObject(bucket_, path, std::move(version));
234 return RequestApi(req,
"get_object", headers_data, headers_request);
237std::optional<std::string>
ClientImpl::GetPartialObject(
238 std::string_view path,
239 std::string_view range,
240 std::optional<std::string> version,
245 return std::make_optional(TryGetPartialObject(path, range, std::move(version), headers_data, headers_request));
247 if (e.code() == 404) {
248 LOG_INFO() <<
"Can't get object with path: " << path <<
", object not found:" << e.what();
250 LOG_ERROR() <<
"Can't get object with path: " << path <<
", unknown error:" << e.what();
253 }
catch (
const std::exception& e) {
254 LOG_ERROR() <<
"Can't get object with path: " << path <<
", unknown error:" << e.what();
260 std::string_view path,
261 std::string_view range,
262 std::optional<std::string> version,
266 auto req = api_methods::GetObject(bucket_, path, std::move(version));
267 api_methods::SetRange(req, range);
268 return RequestApi(req,
"get_object", headers_data, headers_request);
274 auto req = api_methods::GetObjectHead(bucket_, path);
276 RequestApi(req,
"get_object_head", &headers_data, headers_request);
277 }
catch (
const std::exception& e) {
278 LOG_INFO() <<
"Can't get object with path: " << path <<
", error:" << e.what();
281 return std::make_optional(std::move(headers_data));
284[[deprecated]] std::string
ClientImpl::GenerateDownloadUrl(std::string_view path, time_t expires_at,
bool use_ssl)
286 auto req = api_methods::GetObject(bucket_, path);
288 std::ostringstream generated_url;
289 auto host = conn_->GetHost();
290 if (host.find(
"://") == std::string::npos) {
291 generated_url << (use_ssl ?
"https" :
"http") <<
"://";
293 generated_url << host;
295 if (!req.bucket.empty()) {
296 generated_url <<
"/" + req.bucket;
298 AddQueryParamsToPresignedUrl(generated_url, expires_at, req, authenticator_);
299 return generated_url.str();
302std::string
ClientImpl::GenerateDownloadUrlVirtualHostAddressing(
303 std::string_view path,
304 const std::chrono::system_clock::time_point& expires_at,
305 std::string_view protocol
307 auto req = api_methods::GetObject(bucket_, path);
308 if (req.bucket.empty()) {
309 throw NoBucketError(
"presigned url for empty bucket string");
311 return GeneratePresignedUrl(req, conn_->GetHost(), protocol, expires_at, authenticator_);
314std::string
ClientImpl::GenerateUploadUrlVirtualHostAddressing(
315 std::string_view data,
316 std::string_view content_type,
317 std::string_view path,
318 const std::chrono::system_clock::time_point& expires_at,
319 std::string_view protocol
321 auto req = api_methods::PutObject(bucket_, path, std::string{data}, content_type);
322 if (req.bucket.empty()) {
323 throw NoBucketError(
"presigned url for empty bucket string");
325 return GeneratePresignedUrl(req, conn_->GetHost(), protocol, expires_at, authenticator_);
329 if (!authenticator_) {
334 auto auth_headers = authenticator_->Auth(request);
337 auto it = std::find_if(
338 auth_headers.cbegin(),
340 [&request](
const decltype(auth_headers)::value_type& header) {
341 return request.headers
.count(std::get<0>(header)
);
345 if (it != auth_headers.cend()) {
351 std::make_move_iterator(std::begin(auth_headers))
, std::make_move_iterator(std::end(auth_headers))
357 std::string_view method_name,
363 auto response = conn_->RequestApi(request, method_name);
366 if (headers_request.need_meta) {
367 headers_data->meta.emplace();
368 ReadMeta(response->headers(), *headers_data->meta);
370 if (headers_request.headers) {
371 headers_data->headers.emplace();
372 for (
const auto& header : *headers_request.headers) {
373 if (
auto it = response->headers()
.find(header
); it != response->headers()
.end()) {
374 headers_data->headers->
emplace(it->first
, it->second
);
383std::optional<std::string>
384ClientImpl::ListBucketContents(std::string_view path,
int max_keys, std::string marker, std::string delimiter)
const {
385 auto req = api_methods::ListBucketContents(bucket_, path, max_keys, marker, delimiter);
386 std::string reply = RequestApi(req,
"list_bucket_contents");
390 return std::optional<std::string>{std::move(reply)};
393std::vector<
ObjectMeta>
ClientImpl::ListBucketContentsParsed(std::string_view path_prefix)
const {
396 std::string marker =
"";
397 bool is_finished =
false;
398 while (!is_finished) {
399 auto response = ListBucketContents(path_prefix, kMaxS3Keys, marker, {});
401 LOG_WARNING() <<
"Empty S3 bucket listing response for path prefix " << path_prefix;
404 auto response_result = ParseS3ListResponse(*response);
405 if (response_result.empty()) {
408 if (response_result.size() < kMaxS3Keys) {
413 std::make_move_iterator(response_result.begin()),
414 std::make_move_iterator(response_result.end())
416 marker = result.back().key;
421std::vector<std::string>
ClientImpl::ListBucketDirectories(std::string_view path_prefix)
const {
422 std::vector<std::string> result;
424 std::string marker =
"";
425 bool is_finished =
false;
426 while (!is_finished) {
427 auto response = ListBucketContents(path_prefix, kMaxS3Keys, marker,
"/");
429 LOG_WARNING() <<
"Empty S3 directory bucket listing response "
434 auto response_result = ParseS3DirectoriesListResponse(*response);
435 if (response_result.empty()) {
438 if (response_result.size() < kMaxS3Keys) {
443 std::make_move_iterator(response_result.begin()),
444 std::make_move_iterator(response_result.end())
446 marker = result.back();
453 std::string_view key_from,
454 std::string_view bucket_to,
455 std::string_view key_to,
456 const std::optional<Meta>& meta
458 const auto object_head = [&] {
460 header_request.headers.emplace();
461 header_request.headers->emplace(USERVER_NAMESPACE::
http::
headers::kContentType);
462 header_request.need_meta =
false;
463 return GetObjectHead(key_from, header_request);
469 const auto content_type = [&object_head]() -> std::optional<std::string> {
470 if (!object_head->headers) {
475 *object_head->headers
, USERVER_NAMESPACE::http::
headers::kContentType
482 auto req = api_methods::CopyObject(bucket_, key_from, bucket_to, key_to, *content_type);
484 SaveMeta(req.headers, *meta);
486 return RequestApi(req,
"copy_object");
490ClientImpl::CopyObject(std::string_view key_from, std::string_view key_to,
const std::optional<Meta>& meta) {
491 return CopyObject(key_from, bucket_, key_to, meta);
497 auto api_request = api_methods::CreateInternalApiRequest(bucket_, request);
498 const auto api_response_body = RequestApi(api_request,
"create_multipart_upload");
502 fmt::format(
"failed to parse CreateMultipartUpload action response - {}; key: {}", exc.what(), request.key)
508 auto api_request = api_methods::CreateInternalApiRequest(bucket_, request);
513 RequestApi(api_request,
"upload_part", &response_headers_data, expected_headers);
514 if (!response_headers_data.headers) {
517 const auto iter = response_headers_data.headers->
find(kEtagHeader
);
518 if (iter == response_headers_data.headers->
end()) {
521 if (iter->second.empty()) {
524 return {std::move(iter->second)};
528 "failed to parse UploadPart action response - {}; upload_id '{}'; key '{}'",
538 auto api_request = api_methods::CreateInternalApiRequest(bucket_, request);
539 const auto api_response_body = RequestApi(api_request,
"complete_multipart_upload");
543 "failed to parse CompleteMultipartUpload action response - {}; upload_id '{}'; key '{}'",
551 auto api_request = api_methods::CreateInternalApiRequest(bucket_, request);
552 RequestApi(api_request,
"abort_multipart_upload");
556 auto api_request = api_methods::CreateInternalApiRequest(bucket_, request);
557 const auto api_response_body = RequestApi(api_request,
"list_parts");
561 "failed to parse ListParts action response - {}; upload_id '{}'; key '{}'",
571 auto api_request = api_methods::CreateInternalApiRequest(bucket_, request);
572 const auto api_response_body = RequestApi(api_request,
"list_multipart_uploads");
575 throw MultipartUploadError(fmt::format(
"failed to parse ListMultipartUploads action response - {}", exc.what()));
578ClientPtr GetS3Client(
588ClientPtr GetS3Client(
593 return std::static_pointer_cast<
Client>(std::make_shared<
ClientImpl>(s3conn, authenticator, bucket));