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,
60 const struct curl_slist *psExistingHeaders,
const CPLString &osHost,
62 const CPLString &osXAMZContentSHA256,
bool bAddHeaderAMZContentSHA256,
69 const CPLString &osVerb,
const struct curl_slist *psExistingHeaders,
72 const CPLString &osXAMZContentSHA256,
bool bAddHeaderAMZContentSHA256,
75 class IVSIS3LikeHandleHelper
80 std::map<CPLString, CPLString> m_oMapQueryParameters{};
82 virtual void RebuildURL() = 0;
83 CPLString GetQueryString(
bool bAddEmptyValueAfterEqual)
const;
86 IVSIS3LikeHandleHelper() =
default;
87 virtual ~IVSIS3LikeHandleHelper() =
default;
89 void ResetQueryParameters();
92 virtual struct curl_slist *
94 const struct curl_slist *psExistingHeaders,
95 const void *pabyDataContent =
nullptr,
96 size_t nBytesContent = 0)
const = 0;
98 virtual bool AllowAutomaticRedirection()
102 virtual bool CanRestartOnError(
const char *,
const char * ,
109 virtual const CPLString &GetURL()
const = 0;
112 virtual CPLString GetCopySourceHeader()
const
114 return std::string();
116 virtual const char *GetMetadataDirectiveREPLACE()
const
121 static bool GetBucketAndObjectKey(
const char *pszURI,
122 const char *pszFSPrefix,
128 BuildCanonicalizedHeaders(std::map<CPLString, CPLString> &oSortedMapHeaders,
129 const struct curl_slist *psExistingHeaders,
130 const char *pszHeaderPrefix);
135 enum class AWSCredentialsSource
149 class VSIS3HandleHelper final :
public IVSIS3LikeHandleHelper
162 bool m_bUseHTTPS =
false;
163 bool m_bUseVirtualHosting =
false;
164 AWSCredentialsSource m_eCredentialsSource = AWSCredentialsSource::REGULAR;
166 void RebuildURL()
override;
168 static bool GetOrRefreshTemporaryCredentialsForRole(
169 bool bForceRefresh,
CPLString &osSecretAccessKey,
173 static bool GetConfigurationFromAssumeRoleWithWebIdentity(
174 bool bForceRefresh,
const std::string &osPathForOption,
175 const std::string &osRoleArnIn,
176 const std::string &osWebIdentityTokenFileIn,
180 static bool GetConfigurationFromEC2(
bool bForceRefresh,
181 const std::string &osPathForOption,
186 static bool GetConfigurationFromAWSConfigFiles(
187 const std::string &osPathForOption,
const char *pszProfile,
195 static bool GetConfiguration(
const std::string &osPathForOption,
200 AWSCredentialsSource &eCredentialsSource);
202 void RefreshCredentials(
const std::string &osPathForOption,
203 bool bForceRefresh)
const;
207 VSIS3HandleHelper(
const CPLString &osSecretAccessKey,
213 bool bUseHTTPS,
bool bUseVirtualHosting,
214 AWSCredentialsSource eCredentialsSource);
215 ~VSIS3HandleHelper();
217 static VSIS3HandleHelper *BuildFromURI(
const char *pszURI,
218 const char *pszFSPrefix,
223 const CPLString &osObjectKey,
bool bUseHTTPS,
224 bool bUseVirtualHosting);
228 const struct curl_slist *psExistingHeaders,
229 const void *pabyDataContent =
nullptr,
230 size_t nBytesContent = 0)
const override;
232 bool AllowAutomaticRedirection()
override
236 bool CanRestartOnError(
const char *,
const char *pszHeaders,
bool bSetError,
237 bool *pbUpdateMap =
nullptr)
override;
249 return m_osObjectKey;
261 return m_osRequestPayer;
263 bool GetVirtualHosting()
const
265 return m_bUseVirtualHosting;
267 void SetEndpoint(
const CPLString &osStr);
269 void SetRequestPayer(
const CPLString &osStr);
270 void SetVirtualHosting(
bool b);
272 CPLString GetCopySourceHeader()
const override
274 return "x-amz-copy-source";
276 const char *GetMetadataDirectiveREPLACE()
const override
278 return "x-amz-metadata-directive: REPLACE";
283 static void CleanMutex();
284 static void ClearCache();
287 class VSIS3UpdateParams
293 bool m_bUseVirtualHosting =
false;
295 VSIS3UpdateParams() =
default;
297 explicit VSIS3UpdateParams(
const VSIS3HandleHelper *poHelper)
298 : m_osRegion(poHelper->GetRegion()),
299 m_osEndpoint(poHelper->GetEndpoint()),
300 m_osRequestPayer(poHelper->GetRequestPayer()),
301 m_bUseVirtualHosting(poHelper->GetVirtualHosting())
305 void UpdateHandlerHelper(VSIS3HandleHelper *poHelper)
307 poHelper->SetRegion(m_osRegion);
308 poHelper->SetEndpoint(m_osEndpoint);
309 poHelper->SetRequestPayer(m_osRequestPayer);
310 poHelper->SetVirtualHosting(m_bUseVirtualHosting);
313 static std::mutex gsMutex;
314 static std::map<CPLString, VSIS3UpdateParams> goMapBucketsToS3Params;
315 static void UpdateMapFromHandle(IVSIS3LikeHandleHelper *poHandleHelper);
316 static void UpdateHandleFromMap(IVSIS3LikeHandleHelper *poHandleHelper);
317 static void ClearCache();