31 #ifndef CPL_AWS_INCLUDED_H
32 #define CPL_AWS_INCLUDED_H
43 #include <curl/curl.h>
46 CPLString CPLGetLowerCaseHexSHA256(
const void *pabyData,
size_t nBytes );
53 CPLString CPLAWSGetHeaderVal(
const struct curl_slist* psExistingHeaders,
57 CPLGetAWS_SIGN4_Signature(
const CPLString& osSecretAccessKey,
63 const struct curl_slist* psExistingHeaders,
68 bool bAddHeaderAMZContentSHA256,
79 const struct curl_slist* psExistingHeaders,
86 class IVSIS3LikeHandleHelper
91 std::map<CPLString, CPLString> m_oMapQueryParameters{};
93 virtual void RebuildURL() = 0;
94 CPLString GetQueryString(
bool bAddEmptyValueAfterEqual)
const;
97 IVSIS3LikeHandleHelper() =
default;
98 virtual ~IVSIS3LikeHandleHelper() =
default;
100 void ResetQueryParameters();
103 virtual struct curl_slist* GetCurlHeaders(
const CPLString& osVerb,
104 const struct curl_slist* psExistingHeaders,
105 const void *pabyDataContent =
nullptr,
106 size_t nBytesContent = 0)
const = 0;
108 virtual bool AllowAutomaticRedirection() {
return true; }
109 virtual bool CanRestartOnError(
const char*,
const char* ,
110 bool ,
bool* =
nullptr) {
return false;}
112 virtual const CPLString& GetURL()
const = 0;
115 virtual CPLString GetCopySourceHeader()
const {
return std::string(); }
116 virtual const char* GetMetadataDirectiveREPLACE()
const {
return ""; }
118 static bool GetBucketAndObjectKey(
const char* pszURI,
119 const char* pszFSPrefix,
124 static CPLString BuildCanonicalizedHeaders(
125 std::map<CPLString, CPLString>& oSortedMapHeaders,
126 const struct curl_slist* psExistingHeaders,
127 const char* pszHeaderPrefix);
132 enum class AWSCredentialsSource
141 class VSIS3HandleHelper final:
public IVSIS3LikeHandleHelper
154 bool m_bUseHTTPS =
false;
155 bool m_bUseVirtualHosting =
false;
156 AWSCredentialsSource m_eCredentialsSource = AWSCredentialsSource::REGULAR;
158 void RebuildURL()
override;
160 static bool GetConfigurationFromEC2(
const std::string& osPathForOption,
165 static bool GetConfigurationFromAWSConfigFiles(
166 const std::string& osPathForOption,
178 static bool GetConfiguration(
const std::string& osPathForOption,
184 AWSCredentialsSource& eCredentialsSource);
188 VSIS3HandleHelper(
const CPLString& osSecretAccessKey,
196 bool bUseHTTPS,
bool bUseVirtualHosting,
197 AWSCredentialsSource eCredentialsSource);
198 ~VSIS3HandleHelper();
200 static VSIS3HandleHelper* BuildFromURI(
const char* pszURI,
201 const char* pszFSPrefix,
207 bool bUseHTTPS,
bool bUseVirtualHosting);
209 struct curl_slist* GetCurlHeaders(
211 const struct curl_slist* psExistingHeaders,
212 const void *pabyDataContent =
nullptr,
213 size_t nBytesContent = 0)
const override;
215 bool AllowAutomaticRedirection()
override {
return false; }
216 bool CanRestartOnError(
const char*,
const char* pszHeaders,
218 bool* pbUpdateMap =
nullptr)
override;
220 const CPLString& GetURL()
const override {
return m_osURL; }
221 const CPLString& GetBucket()
const {
return m_osBucket; }
222 const CPLString& GetObjectKey()
const {
return m_osObjectKey; }
223 const CPLString& GetEndpoint()
const {
return m_osEndpoint; }
224 const CPLString& GetRegion()
const {
return m_osRegion; }
225 const CPLString& GetRequestPayer()
const {
return m_osRequestPayer; }
226 bool GetVirtualHosting()
const {
return m_bUseVirtualHosting; }
227 void SetEndpoint(
const CPLString &osStr);
229 void SetRequestPayer(
const CPLString &osStr);
230 void SetVirtualHosting(
bool b);
232 CPLString GetCopySourceHeader()
const override {
return "x-amz-copy-source"; }
233 const char* GetMetadataDirectiveREPLACE()
const override {
return "x-amz-metadata-directive: REPLACE"; }
237 static void CleanMutex();
238 static void ClearCache();
241 class VSIS3UpdateParams
247 bool m_bUseVirtualHosting =
false;
249 VSIS3UpdateParams() =
default;
251 explicit VSIS3UpdateParams(
const VSIS3HandleHelper* poHelper) :
252 m_osRegion(poHelper->GetRegion()),
253 m_osEndpoint(poHelper->GetEndpoint()),
254 m_osRequestPayer(poHelper->GetRequestPayer()),
255 m_bUseVirtualHosting(poHelper->GetVirtualHosting()) {}
257 void UpdateHandlerHelper(VSIS3HandleHelper* poHelper) {
258 poHelper->SetRegion(m_osRegion);
259 poHelper->SetEndpoint(m_osEndpoint);
260 poHelper->SetRequestPayer(m_osRequestPayer);
261 poHelper->SetVirtualHosting(m_bUseVirtualHosting);
264 static std::mutex gsMutex;
265 static std::map< CPLString, VSIS3UpdateParams > goMapBucketsToS3Params;
266 static void UpdateMapFromHandle( IVSIS3LikeHandleHelper* poHandleHelper );
267 static void UpdateHandleFromMap( IVSIS3LikeHandleHelper* poHandleHelper );
268 static void ClearCache();