aws-crt-cpp
C++ wrapper around the aws-c-* libraries. Provides Cross-Platform Transport Protocols and SSL/TLS implementations for C++.
Loading...
Searching...
No Matches
S3.h
Go to the documentation of this file.
1#pragma once
7#include <aws/crt/Exports.h>
8#include <aws/crt/Optional.h>
9#include <aws/crt/Types.h>
14#include <aws/crt/io/Uri.h>
16
17#include <cstdint>
18#include <functional>
19#include <future>
20#include <memory>
21
22struct aws_s3_client;
23struct aws_s3_client_config;
24struct aws_s3_meta_request;
25struct aws_s3_meta_request_options;
26struct aws_retry_strategy;
27
28namespace Aws
29{
30 namespace Crt
31 {
32 namespace Auth
33 {
34 class ICredentialsProvider;
35 }
36
37 namespace S3
38 {
44 // Mapped to their aws-c-s3 counterparts by a switch in S3.cpp.
46 {
47 Default,
51 };
52
59 {
60 None,
61 Header,
62 Trailer,
63 };
64
70 {
71 None,
72 Crc32c,
73 Crc32,
74 Sha1,
75 Sha256,
77 Sha512,
81 };
82
88 enum class S3TlsMode
89 {
90 Enabled,
92 };
93
100 enum class S3RecvFileMode
101 {
105 CreateNew,
113 };
114
121 {
123 Default,
125 Standard,
129 NoRetry,
130 };
131
145 {
151 size_t maxRetries = 0;
153 uint32_t scaleFactorMs = 500;
155 uint32_t maxBackoffSecs = 20;
156 };
157
169 {
170 public:
173 S3RetryStrategy(S3RetryStrategy &&other) noexcept = default;
174 S3RetryStrategy &operator=(S3RetryStrategy &&other) noexcept = default;
176
182 static S3RetryStrategy CreateStandard() noexcept;
183
192 static S3RetryStrategy CreateExponentialBackoff(
193 Io::EventLoopGroup &elGroup,
194 const S3RetryStrategyExponentialBackoffOptions &options = {}) noexcept;
195
201 static S3RetryStrategy CreateNoRetry() noexcept;
202
206 explicit operator bool() const noexcept { return m_strategy != nullptr; }
207
210 explicit S3RetryStrategy(aws_retry_strategy *strategy) noexcept;
211
213 aws_retry_strategy *GetUnderlyingHandle() const noexcept { return m_strategy.get(); }
214
215 private:
217 };
218
219 class S3Client;
220 class S3MetaRequest;
221 class S3MetaRequestOptions;
222
256
264 {
265 public:
266 S3ChecksumConfig() noexcept;
267
275 {
276 m_location = location;
277 return *this;
278 }
279
287 {
288 m_algorithm = algorithm;
289 return *this;
290 }
291
300 {
301 m_validateResponseChecksum = validate;
302 return *this;
303 }
304
309
314
318 bool GetValidateResponseChecksum() const noexcept { return m_validateResponseChecksum; }
319
320 private:
321 S3ChecksumLocation m_location;
322 S3ChecksumAlgorithm m_algorithm;
323 bool m_validateResponseChecksum;
324 };
325
335 {
336 public:
341
362 explicit S3ClientConfig(
363 const std::shared_ptr<Auth::ICredentialsProvider> &credentialsProvider) noexcept;
364
365 // Defined out-of-line in S3.cpp where Impl is a complete type
366 // (required to destroy the ScopedResource<Impl> pImpl member).
368
376 S3ClientConfig &SetRegion(const Crt::String &region) noexcept;
377
385 S3ClientConfig &SetThroughputTargetGbps(double gbps) noexcept;
386
397
403 S3ClientConfig &SetMultipartUploadThreshold(uint64_t bytes) noexcept;
404
412 S3ClientConfig &SetMemoryLimit(uint64_t bytes) noexcept;
413
422 S3ClientConfig &SetClientBootstrap(Io::ClientBootstrap &bootstrap) noexcept;
423
428 S3ClientConfig &SetConnectTimeoutMs(uint32_t timeoutMs) noexcept;
429
441 S3ClientConfig &SetReadBackpressure(bool enable, uint64_t initialReadWindow) noexcept;
442
451 S3ClientConfig &SetEnableS3Express(bool enable) noexcept;
452
462
471 S3ClientConfig &SetTlsConnectionOptions(const Io::TlsConnectionOptions &options) noexcept;
472
480 S3ClientConfig &SetProxyOptions(const Http::HttpClientConnectionProxyOptions &proxyOptions) noexcept;
481
492
502 S3ClientConfig &SetConnectionMonitoring(
505
520 S3ClientConfig &SetNetworkInterfaceNames(const Vector<String> &networkInterfaceNames) noexcept;
521
533 S3ClientConfig &SetRetryStrategy(
535 const S3RetryStrategyExponentialBackoffOptions &options = {}) noexcept;
536
547 S3ClientConfig &SetRetryStrategyFactory(
548 std::function<S3RetryStrategy(const S3ClientConfig &)> factory) noexcept;
549
563 S3ClientConfig &SetClientShutdownCallback(std::function<void()> callback) noexcept;
564
568 struct aws_s3_client_config *GetUnderlyingHandle() const noexcept;
569
573 S3RetryStrategyType GetRetryStrategyType() const noexcept { return m_retryStrategyType; }
574
580 {
581 return m_retryStrategyOptions;
582 }
583
589 {
590 return m_retryStrategyFactory;
591 }
592
597 const Vector<String> &GetNetworkInterfaceNames() const noexcept { return m_networkInterfaceNames; }
598
604 {
605 return m_clientShutdownCallback;
606 }
607
608 private:
609 // The CRT C-struct storage (aws_s3_client_config plus the proxy,
610 // TCP keep-alive, and connection-monitoring option backings) lives
611 // by value inside Impl, defined in S3.cpp. This keeps the C headers
612 // out of this public header and collapses what were four separate
613 // heap allocations into the single Impl allocation.
614 struct Impl;
616 String m_region;
617 Optional<Io::TlsConnectionOptions> m_tlsConnectionOptions;
619 Vector<String> m_networkInterfaceNames;
620
621 // Strategy is materialized at S3Client construction; the
622 // factory (when set) takes precedence over the enum.
623 S3RetryStrategyType m_retryStrategyType;
624 S3RetryStrategyExponentialBackoffOptions m_retryStrategyOptions;
625 std::function<S3RetryStrategy(const S3ClientConfig &)> m_retryStrategyFactory;
626
627 std::function<void()> m_clientShutdownCallback;
628 std::shared_ptr<Auth::ICredentialsProvider> m_credentialsProvider;
629 // Held by value (no heap): single-owner, scope-bound to this
630 // config. Optional carries the "no signing config" state the
631 // ctor relies on when no credentials provider is supplied.
632 Optional<Auth::AwsSigningConfig> m_signingConfig;
633 };
634
647 {
648 public:
656
668
675 using HeadersCallback = std::function<int(const Vector<Http::HttpHeader> &headers, int responseStatus)>;
676
683
692 using FinishCallback = std::function<void(const S3MetaRequestResult &result)>;
693
699 using ShutdownCallback = std::function<void()>;
700
705
707
719 S3MetaRequestOptions &SetSigningConfig(const Auth::AwsSigningConfig &config) noexcept;
720
727 S3MetaRequestOptions &SetChecksumConfig(const S3ChecksumConfig &config) noexcept;
728
741 S3MetaRequestOptions &SetEndpoint(const Io::Uri &endpoint) noexcept;
742
749 S3MetaRequestOptions &SetHeadersCallback(HeadersCallback cb) noexcept;
750
757 S3MetaRequestOptions &SetProgressCallback(ProgressCallback cb) noexcept;
758
767
776 S3MetaRequestOptions &SetShutdownCallback(ShutdownCallback cb) noexcept;
777
787
795 S3MetaRequestOptions &SetRecvFilePosition(uint64_t position) noexcept;
796
806 S3MetaRequestOptions &SetRecvFileDeleteOnFailure(bool deleteOnFailure) noexcept;
807
816 S3MetaRequestOptions &SetObjectSizeHint(uint64_t bytes) noexcept;
817
827
837 S3MetaRequestOptions &SetMultipartUploadThreshold(uint64_t bytes) noexcept;
838
842 struct aws_s3_meta_request_options *GetUnderlyingHandle() const noexcept;
843
845 const BodyCallback &GetBodyCallback() const noexcept { return m_bodyCb; }
847 const BodyCallbackEx &GetBodyCallbackEx() const noexcept { return m_bodyCbEx; }
849 const HeadersCallback &GetHeadersCallback() const noexcept { return m_headersCb; }
851 const ProgressCallback &GetProgressCallback() const noexcept { return m_progressCb; }
853 const FinishCallback &GetFinishCallback() const noexcept { return m_finishCb; }
855 const ShutdownCallback &GetShutdownCallback() const noexcept { return m_shutdownCb; }
856
862 int GetLastError() const noexcept { return m_lastError; }
863
864 protected:
876 const std::shared_ptr<Http::HttpRequest> &request) noexcept;
877
878 // The CRT C-struct storage (aws_s3_meta_request_options plus the
879 // checksum-config backing) lives by value inside Impl, defined in
880 // S3.cpp. Keeps the C headers out of this public header and
881 // collapses two heap allocations into the single Impl allocation.
882 // Protected so subclass ctors can reach it (through m_impl) to
883 // populate shape-specific fields directly rather than via a setter.
884 struct Impl;
886 std::shared_ptr<Http::HttpRequest> m_httpRequest;
890 // Endpoint override parsed in place (no heap). aws_uri's internal
891 // cursors point into its own buffer, so it must never be moved
892 // after parsing - hence a value member parsed directly, not a
893 // relocatable wrapper. m_endpointInit tracks whether it holds a
894 // parsed URI needing cleanup; m_options->endpoint being non-null
895 // is the "set" signal.
898 // Backs the borrowed pointer the CRT holds via object_size_hint.
906 // Sticky validation error set by a Set* setter, surfaced at
907 // MakeMetaRequest (mirrors the MqttClient builder's LastError()).
908 int m_lastError = AWS_ERROR_SUCCESS;
909 };
910
920 {
921 public:
926
936 const std::shared_ptr<Http::HttpRequest> &request,
937 BodyCallback cb) noexcept;
938
949 const std::shared_ptr<Http::HttpRequest> &request,
950 BodyCallbackEx cb) noexcept;
951
962 const std::shared_ptr<Http::HttpRequest> &request,
963 const Crt::String &recvFilepath) noexcept;
964
967 explicit S3GetObjectMetaRequestOptions(const std::shared_ptr<Http::HttpRequest> &request) noexcept;
968 };
969
978 {
979 public:
984
993 const std::shared_ptr<Http::HttpRequest> &request) noexcept;
994
1005 const std::shared_ptr<Http::HttpRequest> &request,
1006 const Crt::String &sendFilepath) noexcept;
1007
1018 static ScopedResource<S3MetaRequestOptions> CreateWithAsyncWrites(
1019 const std::shared_ptr<Http::HttpRequest> &request) noexcept;
1020
1023 explicit S3PutObjectMetaRequestOptions(const std::shared_ptr<Http::HttpRequest> &request) noexcept;
1024 };
1025
1032 {
1033 public:
1038
1047 const std::shared_ptr<Http::HttpRequest> &request) noexcept;
1048
1050 explicit S3CopyObjectMetaRequestOptions(const std::shared_ptr<Http::HttpRequest> &request) noexcept;
1051 };
1052
1087
1097 {
1098 public:
1099 S3Client(const S3Client &) = delete;
1100 S3Client(S3Client &&) = delete;
1101 S3Client &operator=(const S3Client &) = delete;
1103
1104 explicit S3Client(const S3ClientConfig &config) noexcept;
1106
1121 std::shared_ptr<S3MetaRequest> MakeMetaRequest(S3MetaRequestOptions &options) noexcept;
1122
1137 static bool MakeDefaultSigningConfig(
1138 Auth::AwsSigningConfig &config,
1139 const String &region,
1140 const std::shared_ptr<Auth::ICredentialsProvider> &provider) noexcept;
1141
1145 explicit operator bool() const noexcept { return m_client != nullptr; }
1146
1152 int LastError() const noexcept;
1153
1154 private:
1156 int m_lastError;
1157 };
1158
1166 {
1167 public:
1168 S3MetaRequest(const S3MetaRequest &) = delete;
1172
1174
1180 void Cancel() noexcept;
1181
1192 void IncrementReadWindow(uint64_t bytes) noexcept;
1193
1206 std::future<int> Write(ByteCursor data, bool eof) noexcept;
1207
1213 int LastError() const noexcept;
1214
1219
1223 void SetUnderlyingHandle(struct aws_s3_meta_request *handle) noexcept;
1224
1226 void SetLastError(int errorCode) noexcept { m_lastError = errorCode; }
1227
1228 private:
1230 int m_lastError;
1231 };
1232
1233 } // namespace S3
1234 } // namespace Crt
1235} // namespace Aws
#define AWS_CRT_CPP_API
Definition Exports.h:36
Definition S3BufferTicket.h:29
bool GetValidateResponseChecksum() const noexcept
Definition S3.h:318
S3ChecksumLocation GetLocation() const noexcept
Definition S3.h:308
S3ChecksumConfig & SetValidateResponseChecksum(bool validate) noexcept
Definition S3.h:299
S3ChecksumConfig & SetLocation(S3ChecksumLocation location) noexcept
Definition S3.h:274
S3ChecksumAlgorithm GetChecksumAlgorithm() const noexcept
Definition S3.h:313
S3ChecksumConfig & SetChecksumAlgorithm(S3ChecksumAlgorithm algorithm) noexcept
Definition S3.h:286
Definition S3.h:335
const S3RetryStrategyExponentialBackoffOptions & GetRetryStrategyOptions() const noexcept
Definition S3.h:579
S3ClientConfig(const S3ClientConfig &)=delete
S3ClientConfig & operator=(S3ClientConfig &&)=delete
const std::function< S3RetryStrategy(const S3ClientConfig &)> & GetRetryStrategyFactory() const noexcept
Definition S3.h:588
const std::function< void()> & GetClientShutdownCallback() const noexcept
Definition S3.h:603
const Vector< String > & GetNetworkInterfaceNames() const noexcept
Definition S3.h:597
S3ClientConfig(S3ClientConfig &&)=delete
S3ClientConfig & operator=(const S3ClientConfig &)=delete
Definition S3.h:1097
S3Client & operator=(S3Client &&)=delete
~S3Client() noexcept=default
S3Client(S3Client &&)=delete
S3Client(const S3Client &)=delete
S3Client & operator=(const S3Client &)=delete
S3CopyObjectMetaRequestOptions & operator=(const S3CopyObjectMetaRequestOptions &)=delete
S3CopyObjectMetaRequestOptions(S3CopyObjectMetaRequestOptions &&)=delete
S3CopyObjectMetaRequestOptions(const S3CopyObjectMetaRequestOptions &)=delete
S3CopyObjectMetaRequestOptions & operator=(S3CopyObjectMetaRequestOptions &&)=delete
S3DefaultObjectMetaRequestOptions(S3DefaultObjectMetaRequestOptions &&)=delete
S3DefaultObjectMetaRequestOptions & operator=(const S3DefaultObjectMetaRequestOptions &)=delete
S3DefaultObjectMetaRequestOptions & operator=(S3DefaultObjectMetaRequestOptions &&)=delete
S3DefaultObjectMetaRequestOptions(const S3DefaultObjectMetaRequestOptions &)=delete
S3GetObjectMetaRequestOptions(S3GetObjectMetaRequestOptions &&)=delete
S3GetObjectMetaRequestOptions(const S3GetObjectMetaRequestOptions &)=delete
S3GetObjectMetaRequestOptions & operator=(S3GetObjectMetaRequestOptions &&)=delete
S3GetObjectMetaRequestOptions & operator=(const S3GetObjectMetaRequestOptions &)=delete
Definition S3.h:1166
~S3MetaRequest() noexcept=default
S3MetaRequest(const S3MetaRequest &)=delete
S3MetaRequest & operator=(S3MetaRequest &&)=delete
S3MetaRequest(S3MetaRequest &&)=delete
S3MetaRequest & operator=(const S3MetaRequest &)=delete
BodyCallbackEx m_bodyCbEx
Definition S3.h:901
String m_recvFilepath
Definition S3.h:889
S3MetaRequestOptions(const S3MetaRequestOptions &)=delete
FinishCallback m_finishCb
Definition S3.h:904
S3MetaRequestOptions & operator=(const S3MetaRequestOptions &)=delete
BodyCallback m_bodyCb
Definition S3.h:900
std::function< void(uint64_t bytesTransferred, uint64_t contentLength)> ProgressCallback
Definition S3.h:682
bool m_endpointInit
Definition S3.h:897
std::shared_ptr< Http::HttpRequest > m_httpRequest
Definition S3.h:886
String m_sendFilepath
Definition S3.h:888
S3MetaRequestOptions(S3MetaRequestOptions &&)=delete
const ProgressCallback & GetProgressCallback() const noexcept
Definition S3.h:851
const BodyCallbackEx & GetBodyCallbackEx() const noexcept
Definition S3.h:847
S3MetaRequestOptions & operator=(S3MetaRequestOptions &&)=delete
int GetLastError() const noexcept
Definition S3.h:862
Optional< uint64_t > m_objectSizeHint
Definition S3.h:899
const FinishCallback & GetFinishCallback() const noexcept
Definition S3.h:853
std::function< void()> ShutdownCallback
Definition S3.h:699
std::function< int(ByteCursor body, uint64_t rangeStart, S3BufferTicket &ticket)> BodyCallbackEx
Definition S3.h:667
HeadersCallback m_headersCb
Definition S3.h:902
ScopedResource< Impl > m_impl
Definition S3.h:885
ProgressCallback m_progressCb
Definition S3.h:903
std::function< int(const Vector< Http::HttpHeader > &headers, int responseStatus)> HeadersCallback
Definition S3.h:675
const ShutdownCallback & GetShutdownCallback() const noexcept
Definition S3.h:855
const HeadersCallback & GetHeadersCallback() const noexcept
Definition S3.h:849
ShutdownCallback m_shutdownCb
Definition S3.h:905
aws_uri m_endpoint
Definition S3.h:896
std::function< int(ByteCursor body, uint64_t rangeStart)> BodyCallback
Definition S3.h:655
String m_operationName
Definition S3.h:887
std::function< void(const S3MetaRequestResult &result)> FinishCallback
Definition S3.h:692
S3PutObjectMetaRequestOptions & operator=(S3PutObjectMetaRequestOptions &&)=delete
S3PutObjectMetaRequestOptions(S3PutObjectMetaRequestOptions &&)=delete
S3PutObjectMetaRequestOptions(const S3PutObjectMetaRequestOptions &)=delete
S3PutObjectMetaRequestOptions & operator=(const S3PutObjectMetaRequestOptions &)=delete
S3RetryStrategy(S3RetryStrategy &&other) noexcept=default
S3RetryStrategy & operator=(S3RetryStrategy &&other) noexcept=default
S3RetryStrategy & operator=(const S3RetryStrategy &)=delete
S3RetryStrategy(const S3RetryStrategy &)=delete
~S3RetryStrategy() noexcept=default
S3TlsMode
Definition S3.h:89
S3MetaRequestType
Definition S3.h:46
S3ChecksumAlgorithm
Definition S3.h:70
S3RetryStrategyType
Definition S3.h:121
S3ChecksumLocation
Definition S3.h:59
S3RecvFileMode
Definition S3.h:101
aws_byte_cursor ByteCursor
Definition Types.h:31
std::basic_string< char, std::char_traits< char >, StlAllocator< char > > String
Definition Types.h:45
std::unique_ptr< T, std::function< void(T *)> > ScopedResource
Definition Types.h:164
std::vector< T, StlAllocator< T > > Vector
Definition Types.h:53
AWS_CRT_CPP_API int LastError() noexcept
Definition Api.cpp:464
Definition Allocator.h:11
Definition StringView.h:862
int errorCode
Definition S3.h:233
ByteCursor errorResponseBody
Definition S3.h:248
Vector< Http::HttpHeader > errorResponseHeaders
Definition S3.h:240
S3ChecksumAlgorithm validationAlgorithm
Definition S3.h:254
int responseStatus
Definition S3.h:237
bool didValidateChecksum
Definition S3.h:251