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 * ,
108 virtual const CPLString &GetURL()
const = 0;
111 virtual CPLString GetCopySourceHeader()
const
113 return std::string();
115 virtual const char *GetMetadataDirectiveREPLACE()
const
120 static bool GetBucketAndObjectKey(
const char *pszURI,
121 const char *pszFSPrefix,
127 BuildCanonicalizedHeaders(std::map<CPLString, CPLString> &oSortedMapHeaders,
128 const struct curl_slist *psExistingHeaders,
129 const char *pszHeaderPrefix);
134 enum class AWSCredentialsSource
148 class VSIS3HandleHelper final :
public IVSIS3LikeHandleHelper
161 bool m_bUseHTTPS =
false;
162 bool m_bUseVirtualHosting =
false;
163 AWSCredentialsSource m_eCredentialsSource = AWSCredentialsSource::REGULAR;
165 void RebuildURL()
override;
167 static bool GetOrRefreshTemporaryCredentialsForRole(
168 bool bForceRefresh,
CPLString &osSecretAccessKey,
172 static bool GetConfigurationFromAssumeRoleWithWebIdentity(
173 bool bForceRefresh,
const std::string &osPathForOption,
174 const std::string &osRoleArnIn,
175 const std::string &osWebIdentityTokenFileIn,
179 static bool GetConfigurationFromEC2(
bool bForceRefresh,
180 const std::string &osPathForOption,
185 static bool GetConfigurationFromAWSConfigFiles(
186 const std::string &osPathForOption,
const char *pszProfile,
194 static bool GetConfiguration(
const std::string &osPathForOption,
199 AWSCredentialsSource &eCredentialsSource);
201 void RefreshCredentials(
const std::string &osPathForOption,
202 bool bForceRefresh)
const;
206 VSIS3HandleHelper(
const CPLString &osSecretAccessKey,
212 bool bUseHTTPS,
bool bUseVirtualHosting,
213 AWSCredentialsSource eCredentialsSource);
214 ~VSIS3HandleHelper();
216 static VSIS3HandleHelper *BuildFromURI(
const char *pszURI,
217 const char *pszFSPrefix,
222 const CPLString &osObjectKey,
bool bUseHTTPS,
223 bool bUseVirtualHosting);
227 const struct curl_slist *psExistingHeaders,
228 const void *pabyDataContent =
nullptr,
229 size_t nBytesContent = 0)
const override;
231 bool AllowAutomaticRedirection()
override
235 bool CanRestartOnError(
const char *,
const char *pszHeaders,
236 bool bSetError)
override;
248 return m_osObjectKey;
260 return m_osRequestPayer;
262 bool GetVirtualHosting()
const
264 return m_bUseVirtualHosting;
266 void SetEndpoint(
const CPLString &osStr);
268 void SetRequestPayer(
const CPLString &osStr);
269 void SetVirtualHosting(
bool b);
271 CPLString GetCopySourceHeader()
const override
273 return "x-amz-copy-source";
275 const char *GetMetadataDirectiveREPLACE()
const override
277 return "x-amz-metadata-directive: REPLACE";
282 static void CleanMutex();
283 static void ClearCache();
286 class VSIS3UpdateParams
292 bool m_bUseVirtualHosting =
false;
294 explicit VSIS3UpdateParams(
const VSIS3HandleHelper *poHelper)
295 : m_osRegion(poHelper->GetRegion()),
296 m_osEndpoint(poHelper->GetEndpoint()),
297 m_osRequestPayer(poHelper->GetRequestPayer()),
298 m_bUseVirtualHosting(poHelper->GetVirtualHosting())
302 void UpdateHandlerHelper(VSIS3HandleHelper *poHelper)
304 poHelper->SetRegion(m_osRegion);
305 poHelper->SetEndpoint(m_osEndpoint);
306 poHelper->SetRequestPayer(m_osRequestPayer);
307 poHelper->SetVirtualHosting(m_bUseVirtualHosting);
310 static std::mutex gsMutex;
311 static std::map<CPLString, VSIS3UpdateParams> goMapBucketsToS3Params;
314 VSIS3UpdateParams() =
default;
316 static void UpdateMapFromHandle(VSIS3HandleHelper *poS3HandleHelper);
317 static void UpdateHandleFromMap(VSIS3HandleHelper *poS3HandleHelper);
318 static void ClearCache();