userver: /data/code/userver/libraries/s3api/src/s3api/s3api_methods.cpp Source File
Loading...
Searching...
No Matches
s3api_methods.cpp
2
3#include <fmt/format.h>
4
5#include <unordered_map>
6
7#include <userver/http/common_headers.hpp>
8#include <userver/http/url.hpp>
9
10USERVER_NAMESPACE_BEGIN
11
12namespace s3api::api_methods {
13
14Request PutObject(
15 std::string_view bucket,
16 std::string_view path,
17 std::string data,
18 std::string_view content_type,
19 const std::optional<std::string_view>& content_disposition
20) {
21 Request req;
22 req.method = clients::http::HttpMethod::kPut;
23 req.bucket = bucket;
24 req.req = path;
25
26 req.headers[USERVER_NAMESPACE::http::headers::kContentLength] = std::to_string(data.size());
27 req.headers[USERVER_NAMESPACE::http::headers::kContentType] = content_type;
28
29 if (content_disposition.has_value()) {
30 req.headers[USERVER_NAMESPACE::http::headers::kContentDisposition] = *content_disposition;
31 }
32
33 req.body = std::move(data);
34 return req;
35}
36
37Request DeleteObject(std::string_view bucket, std::string_view path) {
38 Request req;
39 req.method = clients::http::HttpMethod::kDelete;
40 req.bucket = bucket;
41 req.req = path;
42 return req;
43}
44
45Request GetObject(std::string_view bucket, std::string_view path, std::optional<std::string_view> version) {
46 Request req;
47 req.method = clients::http::HttpMethod::kGet;
48 req.bucket = bucket;
49 req.req = path;
50
51 if (version) {
52 req.req += "?" + USERVER_NAMESPACE::http::MakeQuery({{"versionId", *version}});
53 }
54
55 return req;
56}
57
58Request GetObjectHead(std::string_view bucket, std::string_view path) {
59 Request req;
60 req.method = clients::http::HttpMethod::kHead;
61 req.bucket = bucket;
62 req.req = path;
63 return req;
64}
65
66void SetRange(Request& req, size_t begin, size_t end) {
67 req.headers[USERVER_NAMESPACE::http::headers::kRange] =
68 "bytes=" + std::to_string(begin) + '-' + std::to_string(end);
69}
70
71void SetRange(Request& req, std::string_view range) { req.headers[USERVER_NAMESPACE::http::headers::kRange] = range; }
72
73Request GetBuckets() { return Request{{}, "", "", "", clients::http::HttpMethod::kGet}; }
74
75Request ListBucketContents(
76 std::string_view bucket,
77 std::string_view path,
78 int max_keys,
79 std::string_view marker,
80 std::string_view delimiter
81) {
82 Request req;
83 req.method = clients::http::HttpMethod::kGet;
84 req.bucket = bucket;
85
86 std::unordered_map<std::string, std::string> params;
87 params.emplace("prefix", path);
88 if (max_keys > 0) {
89 params.emplace("max-keys", std::to_string(max_keys));
90 }
91
92 if (!marker.empty()) {
93 params.emplace("marker", marker);
94 }
95
96 if (!delimiter.empty()) {
97 params.emplace("delimiter", delimiter);
98 }
99
100 req.req = "?" + USERVER_NAMESPACE::http::MakeQuery(params);
101 return req;
102}
103Request CopyObject(
104 std::string_view source_bucket,
105 std::string_view source_key,
106 std::string_view dest_bucket,
107 std::string_view dest_key,
108 std::string_view content_type
109) {
110 Request req;
111 req.method = clients::http::HttpMethod::kPut;
112 req.bucket = dest_bucket;
113 req.req = dest_key;
114
115 req.headers[headers::kAmzCopySource] = fmt::format("/{}/{}", source_bucket, source_key);
116 req.headers[USERVER_NAMESPACE::http::headers::kContentType] = content_type;
117
118 return req;
119}
120
121} // namespace s3api::api_methods
122
123USERVER_NAMESPACE_END