15
15
#include < boost/algorithm/string/predicate.hpp>
16
16
#include < boost/algorithm/string/split.hpp>
17
17
#include < boost/algorithm/string/trim.hpp>
18
+ #include < boost/optional/optional_io.hpp>
18
19
#include < boost/regex.hpp>
19
20
#include < boost/tokenizer.hpp>
20
21
#include < chrono>
@@ -122,6 +123,9 @@ bool bucket_name_valid(const std::string& bucket_name) {
122
123
123
124
} // anonymous namespace
124
125
126
+ /*
127
+ * @param: parsed_url output parameter, its state will be modified
128
+ */
125
129
S3Client init_aws_sdk_with_turi_env (s3url& parsed_url) {
126
130
// s3 client config
127
131
// DefaultCredentialProviderChain
@@ -426,38 +430,24 @@ std::string quote_and_escape_path(const std::string& path) {
426
430
return ret;
427
431
}
428
432
429
- list_objects_response list_objects_impl (s3url parsed_url, std::string proxy,
433
+ list_objects_response list_objects_impl (const s3url& parsed_url,
434
+ std::string proxy,
430
435
std::string endpoint) {
431
- // credentials
432
- Aws::Auth::AWSCredentials credentials (parsed_url.access_key_id .c_str (),
433
- parsed_url.secret_key .c_str ());
436
+ // do not modify the parsed_url because string_from_s3url() will
437
+ // be called on it to retrieve its original url (prefix) in is_directory.
434
438
435
- // s3 client config
436
- Aws::Client::ClientConfiguration clientConfiguration ;
439
+ // let init_aws_sdk to be aware of the endpoint to override
440
+ auto temp_url = parsed_url ;
437
441
438
- if (turi::fileio::insecure_ssl_cert_checks ()) {
439
- clientConfiguration. verifySSL = false ;
442
+ if (temp_url. endpoint . empty ()) {
443
+ temp_url. endpoint = endpoint ;
440
444
}
441
445
442
- if (parsed_url.endpoint .empty ()) {
443
- clientConfiguration.endpointOverride = endpoint.c_str ();
444
- } else {
445
- clientConfiguration.endpointOverride = parsed_url.endpoint .c_str ();
446
+ if (!temp_url.sdk_proxy || temp_url.sdk_proxy ->empty ()) {
447
+ temp_url.sdk_proxy = proxy;
446
448
}
447
449
448
- clientConfiguration.proxyHost = proxy.c_str ();
449
- clientConfiguration.requestTimeoutMs = 5 * 60000 ;
450
- clientConfiguration.connectTimeoutMs = 20000 ;
451
- std::string region = fileio::get_region_name_from_endpoint (
452
- clientConfiguration.endpointOverride .c_str ());
453
- if (!region.empty ()) {
454
- clientConfiguration.region = region.c_str ();
455
- parsed_url.sdk_region = region;
456
- }
457
-
458
- S3Client client (credentials, clientConfiguration,
459
- Aws::Client::AWSAuthV4Signer::PayloadSigningPolicy::Never,
460
- false );
450
+ S3Client client = init_aws_sdk_with_turi_env (temp_url);
461
451
462
452
list_objects_response ret;
463
453
@@ -522,8 +512,7 @@ list_objects_response list_objects_impl(s3url parsed_url, std::string proxy,
522
512
if (n_retry == 3 ) {
523
513
// amend the error msg on the last retry failure
524
514
std::stringstream ss;
525
- reportS3ErrorDetailed (ss, parsed_url, S3Operation::List,
526
- clientConfiguration, outcome)
515
+ reportS3ErrorDetailed (ss, temp_url, S3Operation::List, outcome)
527
516
<< std::endl;
528
517
ret.error = ss.str ();
529
518
logstream (LOG_DEBUG)
@@ -536,8 +525,7 @@ list_objects_response list_objects_impl(s3url parsed_url, std::string proxy,
536
525
537
526
} else {
538
527
std::stringstream ss;
539
- reportS3ErrorDetailed (ss, parsed_url, S3Operation::List,
540
- clientConfiguration, outcome)
528
+ reportS3ErrorDetailed (ss, temp_url, S3Operation::List, outcome)
541
529
<< std::endl;
542
530
ret.error = ss.str ();
543
531
logstream (LOG_DEBUG)
@@ -566,47 +554,25 @@ list_objects_response list_objects_impl(s3url parsed_url, std::string proxy,
566
554
}
567
555
568
556
// / returns an error string on failure. Empty string on success
569
- std::string delete_object_impl (s3url parsed_url, std::string proxy,
557
+ std::string delete_object_impl (const s3url& parsed_url, std::string proxy,
570
558
std::string endpoint) {
571
- std::string ret;
572
-
573
- // credentials
574
- Aws::Auth::AWSCredentials credentials (parsed_url.access_key_id .c_str (),
575
- parsed_url.secret_key .c_str ());
576
-
577
- // s3 client config
578
- Aws::Client::ClientConfiguration clientConfiguration;
579
-
580
- clientConfiguration.requestTimeoutMs = 5 * 60000 ;
581
- clientConfiguration.connectTimeoutMs = 20000 ;
559
+ // do not modify the parsed_url because string_from_s3url() will
560
+ // be called on it to retrieve its original url (prefix) in is_directory.
582
561
583
- if (turi::fileio::insecure_ssl_cert_checks ()) {
584
- clientConfiguration.verifySSL = false ;
585
- }
562
+ // let init_aws_sdk to be aware of the endpoint to override
563
+ auto temp_url = parsed_url;
586
564
587
- if (parsed_url.endpoint .empty ()) {
588
- clientConfiguration.endpointOverride = endpoint.c_str ();
589
- parsed_url.sdk_endpoint = endpoint;
590
- } else {
591
- clientConfiguration.endpointOverride = parsed_url.endpoint .c_str ();
565
+ if (temp_url.endpoint .empty ()) {
566
+ temp_url.endpoint = endpoint;
592
567
}
593
568
594
- if (!proxy.empty ()) {
595
- clientConfiguration.proxyHost = proxy.c_str ();
596
- parsed_url.sdk_proxy = proxy;
569
+ if (!temp_url.sdk_proxy || temp_url.sdk_proxy ->empty ()) {
570
+ temp_url.sdk_proxy = proxy;
597
571
}
598
572
599
- std::string region = fileio::get_region_name_from_endpoint (
600
- clientConfiguration.endpointOverride .c_str ());
601
- if (!region.empty ()) {
602
- clientConfiguration.region = region.c_str ();
603
- parsed_url.sdk_region = region;
604
- }
573
+ S3Client client = init_aws_sdk_with_turi_env (temp_url);
605
574
606
- S3Client client (credentials, clientConfiguration,
607
- /* default value */
608
- Aws::Client::AWSAuthV4Signer::PayloadSigningPolicy::Never,
609
- /* use virtual address */ false );
575
+ std::string ret;
610
576
611
577
Aws::S3::Model::DeleteObjectRequest request;
612
578
request.WithBucket (parsed_url.bucket .c_str ());
@@ -615,8 +581,7 @@ std::string delete_object_impl(s3url parsed_url, std::string proxy,
615
581
auto outcome = client.DeleteObject (request);
616
582
if (!outcome.IsSuccess ()) {
617
583
std::stringstream ss;
618
- reportS3ErrorDetailed (ss, parsed_url, S3Operation::Delete,
619
- clientConfiguration, outcome)
584
+ reportS3ErrorDetailed (ss, temp_url, S3Operation::Delete, outcome)
620
585
<< std::endl;
621
586
ret = ss.str ();
622
587
}
@@ -625,46 +590,25 @@ std::string delete_object_impl(s3url parsed_url, std::string proxy,
625
590
}
626
591
627
592
// / returns an error string on failure. Empty string on success
628
- std::string delete_prefix_impl (s3url parsed_url, std::string proxy,
593
+ std::string delete_prefix_impl (const s3url& parsed_url, std::string proxy,
629
594
std::string endpoint) {
630
- // List objects and then create a DeleteObjects request from the resulting
631
- std::string ret;
595
+ // do not modify the parsed_url because string_from_s3url() will
596
+ // be called on it to retrieve its original url (prefix) in is_directory.
632
597
633
- // credentials
634
- Aws::Auth::AWSCredentials credentials (parsed_url.access_key_id .c_str (),
635
- parsed_url.secret_key .c_str ());
598
+ // let init_aws_sdk to be aware of the endpoint to override
599
+ auto temp_url = parsed_url;
636
600
637
- // s3 client config
638
- Aws::Client::ClientConfiguration clientConfiguration;
639
- if (turi::fileio::insecure_ssl_cert_checks ()) {
640
- clientConfiguration.verifySSL = false ;
601
+ if (temp_url.endpoint .empty ()) {
602
+ temp_url.endpoint = endpoint;
641
603
}
642
604
643
- if (parsed_url.endpoint .empty ()) {
644
- clientConfiguration.endpointOverride = endpoint.c_str ();
645
- // for report
646
- parsed_url.endpoint = endpoint;
647
- } else {
648
- clientConfiguration.endpointOverride = parsed_url.endpoint .c_str ();
605
+ if (!temp_url.sdk_proxy || temp_url.sdk_proxy ->empty ()) {
606
+ temp_url.sdk_proxy = proxy;
649
607
}
650
608
651
- if (proxy.size ()) {
652
- clientConfiguration.proxyHost = proxy.c_str ();
653
- parsed_url.sdk_proxy = proxy;
654
- }
655
- clientConfiguration.requestTimeoutMs = 5 * 60000 ;
656
- clientConfiguration.connectTimeoutMs = 20000 ;
657
- std::string region = fileio::get_region_name_from_endpoint (
658
- clientConfiguration.endpointOverride .c_str ());
659
- if (!region.empty ()) {
660
- clientConfiguration.region = region.c_str ();
661
- parsed_url.sdk_region = region;
662
- }
609
+ S3Client client = init_aws_sdk_with_turi_env (temp_url);
663
610
664
- S3Client client (credentials, clientConfiguration,
665
- /* default value */
666
- Aws::Client::AWSAuthV4Signer::PayloadSigningPolicy::Never,
667
- /* use virtual address */ false );
611
+ std::string ret;
668
612
669
613
Aws::S3::Model::ListObjectsV2Request request;
670
614
request.WithBucket (parsed_url.bucket .c_str ());
@@ -695,8 +639,7 @@ std::string delete_prefix_impl(s3url parsed_url, std::string proxy,
695
639
696
640
} else {
697
641
std::stringstream ss;
698
- reportS3ErrorDetailed (ss, parsed_url, S3Operation::List,
699
- clientConfiguration, outcome)
642
+ reportS3ErrorDetailed (ss, temp_url, S3Operation::List, outcome)
700
643
<< std::endl;
701
644
ret = ss.str ();
702
645
}
@@ -711,8 +654,7 @@ std::string delete_prefix_impl(s3url parsed_url, std::string proxy,
711
654
auto outcome = client.DeleteObjects (delRequest);
712
655
if (!outcome.IsSuccess ()) {
713
656
std::stringstream ss;
714
- reportS3ErrorDetailed (ss, parsed_url, S3Operation::Delete,
715
- clientConfiguration, outcome)
657
+ reportS3ErrorDetailed (ss, parsed_url, S3Operation::Delete, outcome)
716
658
<< std::endl;
717
659
ret = ss.str ();
718
660
}
0 commit comments