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();
101 pugi::xml_document xml;
102 const pugi::xml_parse_result parse_result = xml.load_string(s3_response.c_str());
103 if (parse_result.status != pugi::status_ok) {
105 "Failed to parse S3 list response as xml, error: {}, response: {}",
106 parse_result.description(),
111 const auto items = xml.child(
"ListBucketResult").children(
"Contents");
112 for (
const auto& item : items) {
113 const auto key = item.child(
"Key").child_value();
114 const auto size = std::stoull(item.child(
"Size").child_value());
115 const auto last_modified = item.child(
"LastModified").child_value();
116 result.push_back(
ObjectMeta{key, size, last_modified});
118 }
catch (
const pugi::xpath_exception& ex) {
120 fmt::format(
"Bad xml structure for S3 list response, error: {}, response: {}", ex.what(), s3_response)
126std::vector<std::string> ParseS3DirectoriesListResponse(utils::
zstring_view s3_response) {
127 std::vector<std::string> result;
128 pugi::xml_document xml;
129 const pugi::xml_parse_result parse_result = xml.load_string(s3_response.c_str());
130 if (parse_result.status != pugi::status_ok) {
132 "Failed to parse S3 directories list response as xml, error: {}, "
134 parse_result.description(),
139 const auto items = xml.child(
"ListBucketResult").children(
"CommonPrefixes");
140 for (
const auto& item : items) {
141 result.push_back(item.child(
"Prefix").child_value());
143 }
catch (
const pugi::xpath_exception& ex) {
145 "Bad xml structure for S3 directories list response, error: {}, "
163 : conn_(std::move(s3conn)),
164 authenticator_{std::move(authenticator)},
165 bucket_(std::move(bucket))
180std::string_view
ClientImpl::GetBucketName()
const {
return bucket_; }
183 std::string_view path,
185 const std::optional<Meta>& meta,
186 std::string_view content_type,
187 const std::optional<std::string>& content_disposition,
188 const std::optional<std::vector<
Tag>>& tags
190 auto req = api_methods::PutObject(
198 if (meta.has_value()) {
199 SaveMeta(req.headers, meta.value());
201 if (tags.has_value()) {
202 SaveTags(req.headers, tags.value());
204 return RequestApi(req,
"put_object");
207void ClientImpl::DeleteObject(std::string_view path)
const {
208 auto req = api_methods::DeleteObject(bucket_, path);
209 RequestApi(req,
"delete_object");
212std::optional<std::string>
ClientImpl::GetObject(
213 std::string_view path,
214 std::optional<std::string> version,
219 return std::make_optional(TryGetObject(path, std::move(version), headers_data, headers_request));
221 if (e.code() == 404) {
222 LOG_INFO() <<
"Can't get object with path: " << path <<
", object not found:" << e.what();
224 LOG_ERROR() <<
"Can't get object with path: " << path <<
", unknown error:" << e.what();
227 }
catch (
const std::exception& e) {
228 LOG_ERROR() <<
"Can't get object with path: " << path <<
", unknown error:" << e.what();
234 std::string_view path,
235 std::optional<std::string> version,
239 auto req = api_methods::GetObject(bucket_, path, std::move(version));
240 return RequestApi(req,
"get_object", headers_data, headers_request);
243std::optional<std::string>
ClientImpl::GetPartialObject(
244 std::string_view path,
245 std::string_view range,
246 std::optional<std::string> version,
251 return std::make_optional(TryGetPartialObject(path, range, std::move(version), headers_data, headers_request));
253 if (e.code() == 404) {
254 LOG_INFO() <<
"Can't get object with path: " << path <<
", object not found:" << e.what();
256 LOG_ERROR() <<
"Can't get object with path: " << path <<
", unknown error:" << e.what();
259 }
catch (
const std::exception& e) {
260 LOG_ERROR() <<
"Can't get object with path: " << path <<
", unknown error:" << e.what();
266 std::string_view path,
267 std::string_view range,
268 std::optional<std::string> version,
272 auto req = api_methods::GetObject(bucket_, path, std::move(version));
273 api_methods::SetRange(req, range);
274 return RequestApi(req,
"get_object", headers_data, headers_request);
278 std::string_view path,
282 auto req = api_methods::GetObjectHead(bucket_, path);
284 RequestApi(req,
"get_object_head", &headers_data, headers_request);
285 }
catch (
const std::exception& e) {
286 LOG_INFO() <<
"Can't get object with path: " << path <<
", error:" << e.what();
289 return std::make_optional(std::move(headers_data));
292[[deprecated]] std::string
ClientImpl::GenerateDownloadUrl(std::string_view path, time_t expires_at,
bool use_ssl)
294 auto req = api_methods::GetObject(bucket_, path);
296 std::ostringstream generated_url;
297 auto host = conn_->GetHost();
298 if (host.find(
"://") == std::string::npos) {
299 generated_url << (use_ssl ?
"https" :
"http") <<
"://";
301 generated_url << host;
303 if (!req.bucket.empty()) {
304 generated_url <<
"/" + req.bucket;
306 AddQueryParamsToPresignedUrl(generated_url, expires_at, req, authenticator_);
307 return generated_url.str();
310std::string
ClientImpl::GenerateDownloadUrlVirtualHostAddressing(
311 std::string_view path,
312 const std::chrono::system_clock::time_point& expires_at,
313 std::string_view protocol
315 auto req = api_methods::GetObject(bucket_, path);
316 if (req.bucket.empty()) {
317 throw NoBucketError(
"presigned url for empty bucket string");
319 return GeneratePresignedUrl(req, conn_->GetHost(), protocol, expires_at, authenticator_);
322std::string
ClientImpl::GenerateUploadUrlVirtualHostAddressing(
323 std::string_view data,
324 std::string_view content_type,
325 std::string_view path,
326 const std::chrono::system_clock::time_point& expires_at,
327 std::string_view protocol
329 auto req = api_methods::PutObject(bucket_, path, std::string{data}, content_type);
330 if (req.bucket.empty()) {
331 throw NoBucketError(
"presigned url for empty bucket string");
333 return GeneratePresignedUrl(req, conn_->GetHost(), protocol, expires_at, authenticator_);
337 if (!authenticator_) {
342 auto auth_headers = authenticator_->Auth(request);
345 auto it = std::find_if(
346 auth_headers.cbegin(),
348 [&request](
const decltype(auth_headers)::value_type& header) {
349 return request.headers
.count(std::get<0>(header)
);
353 if (it != auth_headers.cend()) {
359 .insert(std::make_move_iterator(std::begin(auth_headers))
, std::make_move_iterator(std::end(auth_headers))
);
364 std::string_view method_name,
370 auto response = conn_->RequestApi(request, method_name);
373 if (headers_request.need_meta) {
374 headers_data->meta.emplace();
375 ReadMeta(response->headers(), *headers_data->meta);
377 if (headers_request.headers) {
378 headers_data->headers.emplace();
379 for (
const auto& header : *headers_request.headers) {
380 if (
auto it = response->headers()
.find(header
); it != response->headers()
.end()) {
381 headers_data->headers->
emplace(it->first
, it->second
);
390std::optional<std::string>
ClientImpl::ListBucketContents(
391 std::string_view path,
394 std::string delimiter
396 auto req = api_methods::ListBucketContents(bucket_, path, max_keys, marker, delimiter);
397 std::string reply = RequestApi(req,
"list_bucket_contents");
401 return std::optional<std::string>{std::move(reply)};
404std::vector<
ObjectMeta>
ClientImpl::ListBucketContentsParsed(std::string_view path_prefix)
const {
407 std::string marker =
"";
408 bool is_finished =
false;
409 while (!is_finished) {
410 auto response = ListBucketContents(path_prefix, kMaxS3Keys, marker, {});
412 LOG_WARNING() <<
"Empty S3 bucket listing response for path prefix " << path_prefix;
415 auto response_result = ParseS3ListResponse(*response);
416 if (response_result.empty()) {
419 if (response_result.size() < kMaxS3Keys) {
424 std::make_move_iterator(response_result.begin()),
425 std::make_move_iterator(response_result.end())
427 marker = result.back().key;
432std::vector<std::string>
ClientImpl::ListBucketDirectories(std::string_view path_prefix)
const {
433 std::vector<std::string> result;
435 std::string marker =
"";
436 bool is_finished =
false;
437 while (!is_finished) {
438 auto response = ListBucketContents(path_prefix, kMaxS3Keys, marker,
"/");
441 <<
"Empty S3 directory bucket listing response "
446 auto response_result = ParseS3DirectoriesListResponse(*response);
447 if (response_result.empty()) {
450 if (response_result.size() < kMaxS3Keys) {
455 std::make_move_iterator(response_result.begin()),
456 std::make_move_iterator(response_result.end())
458 marker = result.back();
465 std::string_view key_from,
466 std::string_view bucket_to,
467 std::string_view key_to,
468 const std::optional<Meta>& meta
470 const auto object_head = [&] {
472 header_request.headers.emplace();
473 header_request.headers->emplace(USERVER_NAMESPACE::
http::
headers::kContentType);
474 header_request.need_meta =
false;
475 return GetObjectHead(key_from, header_request);
481 const auto content_type = [&object_head]() -> std::optional<std::string> {
482 if (!object_head->headers) {
487 *object_head->headers
,
488 USERVER_NAMESPACE::http::
headers::kContentType
495 auto req = api_methods::CopyObject(bucket_, key_from, bucket_to, key_to, *content_type);
497 SaveMeta(req.headers, *meta);
499 return RequestApi(req,
"copy_object");
503 std::string_view key_from,
504 std::string_view key_to,
505 const std::optional<Meta>& meta
507 return CopyObject(key_from, bucket_, key_to, meta);
514 auto api_request = api_methods::CreateInternalApiRequest(bucket_, request);
515 const auto api_response_body = RequestApi(api_request,
"create_multipart_upload");
519 fmt::format(
"failed to parse CreateMultipartUpload action response - {}; key: {}", exc.what(), request.key)
525 auto api_request = api_methods::CreateInternalApiRequest(bucket_, request);
530 RequestApi(api_request,
"upload_part", &response_headers_data, expected_headers);
531 if (!response_headers_data.headers) {
534 const auto iter = response_headers_data.headers->
find(kEtagHeader
);
535 if (iter == response_headers_data.headers->
end()) {
538 if (iter->second.empty()) {
541 return {std::move(iter->second)};
545 "failed to parse UploadPart action response - {}; upload_id '{}'; key '{}'",
556 auto api_request = api_methods::CreateInternalApiRequest(bucket_, request);
557 const auto api_response_body = RequestApi(api_request,
"complete_multipart_upload");
561 "failed to parse CompleteMultipartUpload action response - {}; upload_id '{}'; key '{}'",
569 auto api_request = api_methods::CreateInternalApiRequest(bucket_, request);
570 RequestApi(api_request,
"abort_multipart_upload");
575 auto api_request = api_methods::CreateInternalApiRequest(bucket_, request);
576 const auto api_response_body = RequestApi(api_request,
"list_parts");
580 "failed to parse ListParts action response - {}; upload_id '{}'; key '{}'",
591 auto api_request = api_methods::CreateInternalApiRequest(bucket_, request);
592 const auto api_response_body = RequestApi(api_request,
"list_multipart_uploads");
595 throw MultipartUploadError(fmt::format(
"failed to parse ListMultipartUploads action response - {}", exc.what()));
598ClientPtr GetS3Client(
610ClientPtr GetS3Client(
615 return std::static_pointer_cast<
Client>(std::make_shared<
ClientImpl>(s3conn, authenticator, bucket));