6#include <unordered_map>
8#include <userver/http/common_headers.hpp>
9#include <userver/http/url.hpp>
11USERVER_NAMESPACE_BEGIN
13namespace s3api::api_methods {
16class StringWriter :
public pugi::xml_writer {
18 explicit StringWriter(std::string& out)
22 void write(
const void* data, size_t size)
override { out_.append(
static_cast<
const char*>(data), size); }
29constexpr std::string_view kDelimiter =
"delimiter";
30constexpr std::string_view kEncodingType =
"encoding-type";
31constexpr std::string_view kMarker =
"marker";
32constexpr std::string_view kMaxKeys =
"max-keys";
33constexpr std::string_view kMaxParts =
"max-parts";
34constexpr std::string_view kMaxUploads =
"max-uploads";
35constexpr std::string_view kPartNumber =
"partNumber";
36constexpr std::string_view kPartNumberMarker =
"part-number-marker";
37constexpr std::string_view kPrefix =
"prefix";
38constexpr std::string_view kUploadId =
"uploadId";
39constexpr std::string_view kUploadIdMarker =
"upload-id-marker";
42namespace content_types {
43constexpr std::string_view kDefault =
"application/octet-stream";
44constexpr std::string_view kApplicationXml =
"application/xml; charset=UTF-8";
47constexpr std::string_view kEncodingTypeValueUrl =
"url";
49constexpr std::string_view kS3AwsXmlNamespace =
"http://s3.amazonaws.com/doc/2006-03-01/";
52const unsigned kMaxUploadsListLimit = 1000u;
57 std::string_view bucket,
58 std::string_view path,
60 std::string_view content_type,
61 const std::optional<std::string_view>& content_disposition
68 req.headers
[USERVER_NAMESPACE::http::
headers::kContentLength
] = std::to_string(data.size());
69 req.headers
[USERVER_NAMESPACE::http::
headers::kContentType
] = content_type;
71 if (content_disposition.has_value()) {
72 req.headers
[USERVER_NAMESPACE::http::
headers::kContentDisposition
] = *content_disposition;
75 req.body = std::move(data);
79Request DeleteObject(std::string_view bucket, std::string_view path) {
81 req.method = clients::http::
HttpMethod::kDelete;
87Request GetObject(std::string_view bucket, std::string_view path, std::optional<std::string_view> version) {
94 req.req +=
"?" + USERVER_NAMESPACE
::http
::MakeQuery({{
"versionId", *version}}
);
100Request GetObjectHead(std::string_view bucket, std::string_view path) {
102 req.method = clients::http::
HttpMethod::kHead;
108void SetRange(
Request& req, size_t begin, size_t end) {
110 [USERVER_NAMESPACE::http::
headers::
kRange] =
"bytes=" + std::to_string(begin) +
'-' + std::to_string(end);
113void SetRange(
Request& req, std::string_view range) { req.headers
[USERVER_NAMESPACE::http::
headers::
kRange] = range; }
118 std::string_view bucket,
119 std::string_view path,
121 std::string_view marker,
122 std::string_view delimiter
128 std::unordered_map<std::string, std::string> params;
129 params.emplace(
"prefix", path);
131 params.emplace(query_args::kMaxKeys, std::to_string(max_keys));
134 if (!marker.empty()) {
135 params.emplace(query_args::kMarker, marker);
138 if (!delimiter.empty()) {
139 params.emplace(query_args::kDelimiter, delimiter);
146 std::string_view source_bucket,
147 std::string_view source_key,
148 std::string_view dest_bucket,
149 std::string_view dest_key,
150 std::string_view content_type
154 req.bucket = dest_bucket;
157 req.headers
[headers::kAmzCopySource
] = fmt::format(
"/{}/{}", source_bucket, http
::EncodeS3Key(source_key
));
158 req.headers
[USERVER_NAMESPACE::http::
headers::kContentType
] = content_type;
163Request CreateInternalApiRequest(
164 const std::string& bucket,
168 result.method = clients::http::
HttpMethod::kPost;
169 result.bucket = bucket;
170 result.req = fmt::format(
"{}?uploads", http
::EncodeS3Key(request.key
));
172 if (request.content_type) {
173 result.headers
[http::
headers::kContentType
] = *request.content_type;
176 if (request.content_disposition) {
177 result.headers
[http::
headers::kContentDisposition
] = *request.content_disposition;
180 if (request.content_encoding) {
181 result.headers
[http::
headers::kContentEncoding
] = *request.content_encoding;
184 if (request.content_language) {
185 result.headers
[http::
headers::kContentLanguage
] = *request.content_language;
191Request CreateInternalApiRequest(
192 const std::string& bucket,
196 result.method = clients::http::
HttpMethod::kDelete;
197 result.bucket = bucket;
198 result.req = fmt::format(
201 http
::MakeQuery({{query_args::kUploadId, request.upload_id}}
)
206Request CreateInternalApiRequest(
207 const std::string& bucket,
211 result.method = clients::http::
HttpMethod::kPost;
212 result.bucket = bucket;
213 result.req = fmt::format(
216 http
::MakeQuery({{query_args::kUploadId, request.upload_id}}
)
219 pugi::xml_document doc;
220 auto multipart_upload_node = doc.append_child(
"CompleteMultipartUpload");
221 multipart_upload_node.append_attribute(
"xmlns").set_value(kS3AwsXmlNamespace.data());
222 for (
const auto& part : request.completed_parts) {
223 auto part_node = multipart_upload_node.append_child(
"Part");
224 part_node.append_child(
"PartNumber")
225 .append_child(pugi::node_pcdata)
226 .set_value(std::to_string(part.part_number).c_str());
227 part_node.append_child(
"ETag").append_child(pugi::node_pcdata).set_value(part.etag.c_str());
229 StringWriter writer{result.body};
230 doc.save(writer,
"", pugi::format_raw);
231 result.headers
= clients
::http
::Headers{
232 {http::
headers::kContentType, content_types::kApplicationXml},
233 {http::
headers::kContentLength, std::to_string(result.body.size())}
241 result.method = clients::http::
HttpMethod::kPut;
246 result.headers
= clients
::http
::Headers{
247 {http::
headers::kContentLength, std::to_string(request.data.size())},
248 {http::
headers::kContentType, std::string{content_types::kDefault}}
251 result.bucket = bucket;
252 result.req = fmt::format(
256 {{query_args::kUploadId, request.upload_id}, {query_args::kPartNumber, std::to_string(request.part_number)}}
259 result.body = std::move(request.data);
263Request CreateInternalApiRequest(
const std::string& bucket,
const multipart_upload::
ListPartsRequest& request) {
265 result.method = clients::http::
HttpMethod::kGet;
266 result.bucket = bucket;
269 http::Args params{{std::string{query_args::kUploadId}, request.upload_id}};
271 if (request.max_parts > 0 && request.max_parts < kMaxUploadsListLimit) {
272 params.emplace(query_args::kMaxParts, std::to_string(request.max_parts));
275 if (
auto part_number_marker = request.part_number_marker.value_or(0u)) {
276 params.emplace(query_args::kPartNumberMarker, std::to_string(part_number_marker));
284Request CreateInternalApiRequest(
285 const std::string& bucket,
291 result.method = clients::http::
HttpMethod::kGet;
292 result.bucket = bucket;
295 if (request.delimiter) {
296 params.emplace(query_args::kDelimiter, *request.delimiter);
299 if (request.prefix) {
300 params.emplace(query_args::kPrefix, *request.prefix);
303 if (request.max_uploads > 0 && request.max_uploads < kMaxUploadsListLimit) {
304 params.emplace(query_args::kMaxUploads, std::to_string(request.max_uploads));
307 if (request.key_marker) {
308 params.emplace(query_args::kMarker, *request.key_marker);
311 if (request.upload_id_marker) {
312 params.emplace(query_args::kUploadIdMarker, *request.upload_id_marker);
315 if (request.encoding_type == EncodingType::kUrl) {
316 params.emplace(query_args::kEncodingType, kEncodingTypeValueUrl);
319 result.req =
"?uploads";
320 if (!params.empty()) {