@@ -371,31 +371,48 @@ inline bool iEndsWith(const std::string& str, const std::string& suffix) {
371371}
372372
373373#ifdef WITH_AWS_SDK
374+
375+ // Static shared pointer to reuse the S3 client
376+ inline std::shared_ptr<Aws::S3::S3Client> getS3Client () {
377+ static std::shared_ptr<Aws::S3::S3Client> s3Client;
378+ static std::once_flag initFlag;
379+
380+ std::call_once (initFlag, []() {
381+ auto clientConfig = Aws::Client::ClientConfiguration ();
382+ if (const char * env_p = std::getenv (" AWS_ENDPOINT_URL" )) {
383+ clientConfig.endpointOverride = env_p;
384+ } else if (const char * env_p = std::getenv (" AWS_ENDPOINT_URL_S3" )) {
385+ clientConfig.endpointOverride = env_p;
386+ }
387+
388+ bool usePathStyle = false ; // Default to virtual-style addressing
389+ if (const char * env_p = std::getenv (" AWS_USE_PATH_STYLE" )) {
390+ std::string env_value (env_p);
391+ usePathStyle = (env_value == " true" || env_value == " 1" );
392+ }
393+
394+ s3Client = std::make_shared<Aws::S3::S3Client>(
395+ clientConfig,
396+ Aws::Client::AWSAuthV4Signer::PayloadSigningPolicy::Never,
397+ !usePathStyle
398+ );
399+ });
400+
401+ return s3Client;
402+ }
403+
374404inline string readAWSS3 (string path, string range) {
375405 auto no_proto = path.substr (5 );
376406 auto parts = split (no_proto, ' /' );
377407 auto bucket = parts[0 ];
378408 auto key = no_proto.substr (bucket.size () + 1 );
409+
379410 if (std::getenv (" DEBUG" ) == " TRUE" ) {
380411 cout << " bucket: " << bucket << endl;
381412 cout << " key: " << key << endl;
382413 }
383414
384- auto clientConfig = Aws::Client::ClientConfiguration ();
385- if (const char * env_p = std::getenv (" AWS_ENDPOINT_URL" )) {
386- clientConfig.endpointOverride = env_p;
387- }
388- else if (const char * env_p = std::getenv (" AWS_ENDPOINT_URL_S3" )) {
389- clientConfig.endpointOverride = env_p;
390- }
391-
392- bool usePathStyle = false ; // Default to virtual-style addressing
393- if (const char * env_p = std::getenv (" AWS_USE_PATH_STYLE" )) {
394- std::string env_value (env_p);
395- usePathStyle = (env_value == " true" || env_value == " 1" );
396- }
397-
398- auto client = Aws::S3::S3Client (clientConfig, Aws::Client::AWSAuthV4Signer::PayloadSigningPolicy::Never, !usePathStyle);
415+ auto client = getS3Client ();
399416 auto request = Aws::S3::Model::GetObjectRequest ();
400417 request.SetBucket (bucket.c_str ());
401418 request.SetKey (key.c_str ());
@@ -406,7 +423,7 @@ inline string readAWSS3(string path, string range) {
406423 request.SetRange (range.c_str ());
407424 }
408425
409- auto outcome = client. GetObject (request);
426+ auto outcome = client-> GetObject (request);
410427
411428 if (outcome.IsSuccess ()) {
412429 auto & stream = outcome.GetResult ().GetBody ();
0 commit comments