-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy paths3fs.cpp
1242 lines (1054 loc) · 48.3 KB
/
s3fs.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "s3fs.hpp"
#include "crypto.hpp"
#include "duckdb.hpp"
#ifndef DUCKDB_AMALGAMATION
#include "duckdb/common/exception/http_exception.hpp"
#include "duckdb/common/helper.hpp"
#include "duckdb/common/thread.hpp"
#include "duckdb/common/types/timestamp.hpp"
#include "duckdb/function/scalar/strftime_format.hpp"
#include "http_state.hpp"
#endif
#include "duckdb/common/string_util.hpp"
#include "duckdb/function/scalar/string_common.hpp"
#include "duckdb/main/secret/secret_manager.hpp"
#include "duckdb/storage/buffer_manager.hpp"
#include "create_secret_functions.hpp"
#include <iostream>
#include <thread>
namespace duckdb {
static HeaderMap create_s3_header(string url, string query, string host, string service, string method,
const S3AuthParams &auth_params, string date_now = "", string datetime_now = "",
string payload_hash = "", string content_type = "") {
HeaderMap res;
res["Host"] = host;
// If access key is not set, we don't set the headers at all to allow accessing public files through s3 urls
if (auth_params.secret_access_key.empty() && auth_params.access_key_id.empty()) {
return res;
}
if (payload_hash == "") {
payload_hash = "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"; // Empty payload hash
}
// we can pass date/time but this is mostly useful in testing. normally we just get the current datetime here.
if (datetime_now.empty()) {
auto timestamp = Timestamp::GetCurrentTimestamp();
date_now = StrfTimeFormat::Format(timestamp, "%Y%m%d");
datetime_now = StrfTimeFormat::Format(timestamp, "%Y%m%dT%H%M%SZ");
}
// Only some S3 operations supports SSE-KMS, which this "heuristic" attempts to detect.
// https://docs.aws.amazon.com/AmazonS3/latest/userguide/specifying-kms-encryption.html#sse-request-headers-kms
bool use_sse_kms = auth_params.kms_key_id.length() > 0 && (method == "POST" || method == "PUT") &&
query.find("uploadId") == std::string::npos;
res["x-amz-date"] = datetime_now;
res["x-amz-content-sha256"] = payload_hash;
if (auth_params.session_token.length() > 0) {
res["x-amz-security-token"] = auth_params.session_token;
}
if (use_sse_kms) {
res["x-amz-server-side-encryption"] = "aws:kms";
res["x-amz-server-side-encryption-aws-kms-key-id"] = auth_params.kms_key_id;
}
string signed_headers = "";
hash_bytes canonical_request_hash;
hash_str canonical_request_hash_str;
if (content_type.length() > 0) {
signed_headers += "content-type;";
}
signed_headers += "host;x-amz-content-sha256;x-amz-date";
if (auth_params.session_token.length() > 0) {
signed_headers += ";x-amz-security-token";
}
if (use_sse_kms) {
signed_headers += ";x-amz-server-side-encryption;x-amz-server-side-encryption-aws-kms-key-id";
}
auto canonical_request = method + "\n" + S3FileSystem::UrlEncode(url) + "\n" + query;
if (content_type.length() > 0) {
canonical_request += "\ncontent-type:" + content_type;
}
canonical_request += "\nhost:" + host + "\nx-amz-content-sha256:" + payload_hash + "\nx-amz-date:" + datetime_now;
if (auth_params.session_token.length() > 0) {
canonical_request += "\nx-amz-security-token:" + auth_params.session_token;
}
if (use_sse_kms) {
canonical_request += "\nx-amz-server-side-encryption:aws:kms";
canonical_request += "\nx-amz-server-side-encryption-aws-kms-key-id:" + auth_params.kms_key_id;
}
canonical_request += "\n\n" + signed_headers + "\n" + payload_hash;
sha256(canonical_request.c_str(), canonical_request.length(), canonical_request_hash);
hex256(canonical_request_hash, canonical_request_hash_str);
auto string_to_sign = "AWS4-HMAC-SHA256\n" + datetime_now + "\n" + date_now + "/" + auth_params.region + "/" +
service + "/aws4_request\n" + string((char *)canonical_request_hash_str, sizeof(hash_str));
// compute signature
hash_bytes k_date, k_region, k_service, signing_key, signature;
hash_str signature_str;
auto sign_key = "AWS4" + auth_params.secret_access_key;
hmac256(date_now, sign_key.c_str(), sign_key.length(), k_date);
hmac256(auth_params.region, k_date, k_region);
hmac256(service, k_region, k_service);
hmac256("aws4_request", k_service, signing_key);
hmac256(string_to_sign, signing_key, signature);
hex256(signature, signature_str);
res["Authorization"] = "AWS4-HMAC-SHA256 Credential=" + auth_params.access_key_id + "/" + date_now + "/" +
auth_params.region + "/" + service + "/aws4_request, SignedHeaders=" + signed_headers +
", Signature=" + string((char *)signature_str, sizeof(hash_str));
return res;
}
static duckdb::unique_ptr<duckdb_httplib_openssl::Headers> initialize_http_headers(HeaderMap &header_map) {
auto headers = make_uniq<duckdb_httplib_openssl::Headers>();
for (auto &entry : header_map) {
headers->insert(entry);
}
return headers;
}
string S3FileSystem::UrlDecode(string input) {
return StringUtil::URLDecode(input, true);
}
string S3FileSystem::UrlEncode(const string &input, bool encode_slash) {
return StringUtil::URLEncode(input, encode_slash);
}
void AWSEnvironmentCredentialsProvider::SetExtensionOptionValue(string key, const char *env_var_name) {
char *evar;
if ((evar = std::getenv(env_var_name)) != NULL) {
if (StringUtil::Lower(evar) == "false") {
this->config.SetOption(key, Value(false));
} else if (StringUtil::Lower(evar) == "true") {
this->config.SetOption(key, Value(true));
} else {
this->config.SetOption(key, Value(evar));
}
}
}
void AWSEnvironmentCredentialsProvider::SetAll() {
this->SetExtensionOptionValue("s3_region", DEFAULT_REGION_ENV_VAR);
this->SetExtensionOptionValue("s3_region", REGION_ENV_VAR);
this->SetExtensionOptionValue("s3_access_key_id", ACCESS_KEY_ENV_VAR);
this->SetExtensionOptionValue("s3_secret_access_key", SECRET_KEY_ENV_VAR);
this->SetExtensionOptionValue("s3_session_token", SESSION_TOKEN_ENV_VAR);
this->SetExtensionOptionValue("s3_endpoint", DUCKDB_ENDPOINT_ENV_VAR);
this->SetExtensionOptionValue("s3_use_ssl", DUCKDB_USE_SSL_ENV_VAR);
this->SetExtensionOptionValue("s3_kms_key_id", DUCKDB_KMS_KEY_ID_ENV_VAR);
}
S3AuthParams AWSEnvironmentCredentialsProvider::CreateParams() {
S3AuthParams params;
params.region = DEFAULT_REGION_ENV_VAR;
params.region = REGION_ENV_VAR;
params.access_key_id = ACCESS_KEY_ENV_VAR;
params.secret_access_key = SECRET_KEY_ENV_VAR;
params.session_token = SESSION_TOKEN_ENV_VAR;
params.endpoint = DUCKDB_ENDPOINT_ENV_VAR;
params.kms_key_id = DUCKDB_KMS_KEY_ID_ENV_VAR;
params.use_ssl = DUCKDB_USE_SSL_ENV_VAR;
return params;
}
S3AuthParams S3AuthParams::ReadFrom(optional_ptr<FileOpener> opener, FileOpenerInfo &info) {
auto result = S3AuthParams();
// Without a FileOpener we can not access settings nor secrets: return empty auth params
if (!opener) {
return result;
}
const char *secret_types[] = {"s3", "r2", "gcs", "aws"};
KeyValueSecretReader secret_reader(*opener, info, secret_types, 3);
// These settings we just set or leave to their S3AuthParams default value
secret_reader.TryGetSecretKeyOrSetting("region", "s3_region", result.region);
secret_reader.TryGetSecretKeyOrSetting("key_id", "s3_access_key_id", result.access_key_id);
secret_reader.TryGetSecretKeyOrSetting("secret", "s3_secret_access_key", result.secret_access_key);
secret_reader.TryGetSecretKeyOrSetting("session_token", "s3_session_token", result.session_token);
secret_reader.TryGetSecretKeyOrSetting("region", "s3_region", result.region);
secret_reader.TryGetSecretKeyOrSetting("use_ssl", "s3_use_ssl", result.use_ssl);
secret_reader.TryGetSecretKeyOrSetting("kms_key_id", "s3_kms_key_id", result.kms_key_id);
secret_reader.TryGetSecretKeyOrSetting("s3_url_compatibility_mode", "s3_url_compatibility_mode",
result.s3_url_compatibility_mode);
// Endpoint and url style are slightly more complex and require special handling for gcs and r2
auto endpoint_result = secret_reader.TryGetSecretKeyOrSetting("endpoint", "s3_endpoint", result.endpoint);
auto url_style_result = secret_reader.TryGetSecretKeyOrSetting("url_style", "s3_url_style", result.url_style);
if (StringUtil::StartsWith(info.file_path, "gcs://") || StringUtil::StartsWith(info.file_path, "gs://")) {
// For GCS urls we force the endpoint and vhost path style, allowing only to be overridden by secrets
if (result.endpoint.empty() || endpoint_result.GetScope() != SettingScope::SECRET) {
result.endpoint = "storage.googleapis.com";
}
if (result.url_style.empty() || url_style_result.GetScope() != SettingScope::SECRET) {
result.url_style = "path";
}
}
if (result.endpoint.empty()) {
result.endpoint = "s3.amazonaws.com";
}
return result;
}
unique_ptr<KeyValueSecret> CreateSecret(vector<string> &prefix_paths_p, string &type, string &provider, string &name,
S3AuthParams ¶ms) {
auto return_value = make_uniq<KeyValueSecret>(prefix_paths_p, type, provider, name);
//! Set key value map
return_value->secret_map["region"] = params.region;
return_value->secret_map["key_id"] = params.access_key_id;
return_value->secret_map["secret"] = params.secret_access_key;
return_value->secret_map["session_token"] = params.session_token;
return_value->secret_map["endpoint"] = params.endpoint;
return_value->secret_map["url_style"] = params.url_style;
return_value->secret_map["use_ssl"] = params.use_ssl;
return_value->secret_map["kms_key_id"] = params.kms_key_id;
return_value->secret_map["s3_url_compatibility_mode"] = params.s3_url_compatibility_mode;
//! Set redact keys
return_value->redact_keys = {"secret", "session_token"};
return return_value;
}
S3FileHandle::~S3FileHandle() {
if (Exception::UncaughtException()) {
// We are in an exception, don't do anything
return;
}
try {
Close();
} catch (...) { // NOLINT
}
}
S3ConfigParams S3ConfigParams::ReadFrom(optional_ptr<FileOpener> opener) {
uint64_t uploader_max_filesize;
uint64_t max_parts_per_file;
uint64_t max_upload_threads;
Value value;
if (FileOpener::TryGetCurrentSetting(opener, "s3_uploader_max_filesize", value)) {
uploader_max_filesize = DBConfig::ParseMemoryLimit(value.GetValue<string>());
} else {
uploader_max_filesize = S3ConfigParams::DEFAULT_MAX_FILESIZE;
}
if (FileOpener::TryGetCurrentSetting(opener, "s3_uploader_max_parts_per_file", value)) {
max_parts_per_file = value.GetValue<uint64_t>();
} else {
max_parts_per_file = S3ConfigParams::DEFAULT_MAX_PARTS_PER_FILE; // AWS Default
}
if (FileOpener::TryGetCurrentSetting(opener, "s3_uploader_thread_limit", value)) {
max_upload_threads = value.GetValue<uint64_t>();
} else {
max_upload_threads = S3ConfigParams::DEFAULT_MAX_UPLOAD_THREADS;
}
return {uploader_max_filesize, max_parts_per_file, max_upload_threads};
}
void S3FileHandle::Close() {
auto &s3fs = (S3FileSystem &)file_system;
if (flags.OpenForWriting() && !upload_finalized) {
s3fs.FlushAllBuffers(*this);
if (parts_uploaded) {
s3fs.FinalizeMultipartUpload(*this);
}
}
}
unique_ptr<duckdb_httplib_openssl::Client> S3FileHandle::CreateClient(optional_ptr<ClientContext> client_context) {
auto parsed_url = S3FileSystem::S3UrlParse(path, this->auth_params);
string proto_host_port = parsed_url.http_proto + parsed_url.host;
return HTTPFileSystem::GetClient(this->http_params, proto_host_port.c_str(), this);
}
// Opens the multipart upload and returns the ID
string S3FileSystem::InitializeMultipartUpload(S3FileHandle &file_handle) {
auto &s3fs = (S3FileSystem &)file_handle.file_system;
// AWS response is around 300~ chars in docs so this should be enough to not need a resize
idx_t response_buffer_len = 1000;
auto response_buffer = duckdb::unique_ptr<char[]> {new char[response_buffer_len]};
string query_param = "uploads=";
auto res = s3fs.PostRequest(file_handle, file_handle.path, {}, response_buffer, response_buffer_len, nullptr, 0,
query_param);
if (res->code != 200) {
throw HTTPException(*res, "Unable to connect to URL %s: %s (HTTP code %s)", res->http_url, res->error,
to_string(res->code));
}
string result(response_buffer.get(), response_buffer_len);
auto open_tag_pos = result.find("<UploadId>", 0);
auto close_tag_pos = result.find("</UploadId>", open_tag_pos);
if (open_tag_pos == string::npos || close_tag_pos == string::npos) {
throw HTTPException("Unexpected response while initializing S3 multipart upload");
}
open_tag_pos += 10; // Skip open tag
return result.substr(open_tag_pos, close_tag_pos - open_tag_pos);
}
void S3FileSystem::NotifyUploadsInProgress(S3FileHandle &file_handle) {
{
unique_lock<mutex> lck(file_handle.uploads_in_progress_lock);
file_handle.uploads_in_progress--;
}
// Note that there are 2 cv's because otherwise we might deadlock when the final flushing thread is notified while
// another thread is still waiting for an upload thread
file_handle.uploads_in_progress_cv.notify_one();
file_handle.final_flush_cv.notify_one();
}
void S3FileSystem::UploadBuffer(S3FileHandle &file_handle, shared_ptr<S3WriteBuffer> write_buffer) {
auto &s3fs = (S3FileSystem &)file_handle.file_system;
string query_param = "partNumber=" + to_string(write_buffer->part_no + 1) + "&" +
"uploadId=" + S3FileSystem::UrlEncode(file_handle.multipart_upload_id, true);
unique_ptr<ResponseWrapper> res;
case_insensitive_map_t<string>::iterator etag_lookup;
try {
res = s3fs.PutRequest(file_handle, file_handle.path, {}, (char *)write_buffer->Ptr(), write_buffer->idx,
query_param);
if (res->code != 200) {
throw HTTPException(*res, "Unable to connect to URL %s: %s (HTTP code %s)", res->http_url, res->error,
to_string(res->code));
}
etag_lookup = res->headers.find("ETag");
if (etag_lookup == res->headers.end()) {
throw IOException("Unexpected response when uploading part to S3");
}
} catch (std::exception &ex) {
ErrorData error(ex);
if (error.Type() != ExceptionType::IO && error.Type() != ExceptionType::HTTP) {
throw;
}
// Ensure only one thread sets the exception
bool f = false;
auto exchanged = file_handle.uploader_has_error.compare_exchange_strong(f, true);
if (exchanged) {
file_handle.upload_exception = std::current_exception();
}
NotifyUploadsInProgress(file_handle);
return;
}
// Insert etag
{
unique_lock<mutex> lck(file_handle.part_etags_lock);
file_handle.part_etags.insert(std::pair<uint16_t, string>(write_buffer->part_no, etag_lookup->second));
}
file_handle.parts_uploaded++;
// Free up space for another thread to acquire an S3WriteBuffer
write_buffer.reset();
NotifyUploadsInProgress(file_handle);
}
void S3FileSystem::FlushBuffer(S3FileHandle &file_handle, shared_ptr<S3WriteBuffer> write_buffer) {
if (write_buffer->idx == 0) {
return;
}
auto uploading = write_buffer->uploading.load();
if (uploading) {
return;
}
bool can_upload = write_buffer->uploading.compare_exchange_strong(uploading, true);
if (!can_upload) {
return;
}
file_handle.RethrowIOError();
{
unique_lock<mutex> lck(file_handle.write_buffers_lock);
file_handle.write_buffers.erase(write_buffer->part_no);
}
{
unique_lock<mutex> lck(file_handle.uploads_in_progress_lock);
// check if there are upload threads available
if (file_handle.uploads_in_progress >= file_handle.config_params.max_upload_threads) {
// there are not - wait for one to become available
file_handle.uploads_in_progress_cv.wait(lck, [&file_handle] {
return file_handle.uploads_in_progress < file_handle.config_params.max_upload_threads;
});
}
file_handle.uploads_in_progress++;
}
thread upload_thread(UploadBuffer, std::ref(file_handle), write_buffer);
upload_thread.detach();
}
// Note that FlushAll currently does not allow to continue writing afterwards. Therefore, FinalizeMultipartUpload should
// be called right after it!
// TODO: we can fix this by keeping the last partially written buffer in memory and allow reuploading it with new data.
void S3FileSystem::FlushAllBuffers(S3FileHandle &file_handle) {
// Collect references to all buffers to check
vector<shared_ptr<S3WriteBuffer>> to_flush;
file_handle.write_buffers_lock.lock();
for (auto &item : file_handle.write_buffers) {
to_flush.push_back(item.second);
}
file_handle.write_buffers_lock.unlock();
// Flush all buffers that aren't already uploading
for (auto &write_buffer : to_flush) {
if (!write_buffer->uploading) {
FlushBuffer(file_handle, write_buffer);
}
}
unique_lock<mutex> lck(file_handle.uploads_in_progress_lock);
file_handle.final_flush_cv.wait(lck, [&file_handle] { return file_handle.uploads_in_progress == 0; });
file_handle.RethrowIOError();
}
void S3FileSystem::FinalizeMultipartUpload(S3FileHandle &file_handle) {
auto &s3fs = (S3FileSystem &)file_handle.file_system;
file_handle.upload_finalized = true;
std::stringstream ss;
ss << "<CompleteMultipartUpload xmlns=\"http://s3.amazonaws.com/doc/2006-03-01/\">";
auto parts = file_handle.parts_uploaded.load();
for (auto i = 0; i < parts; i++) {
auto etag_lookup = file_handle.part_etags.find(i);
if (etag_lookup == file_handle.part_etags.end()) {
throw IOException("Unknown part number");
}
ss << "<Part><ETag>" << etag_lookup->second << "</ETag><PartNumber>" << i + 1 << "</PartNumber></Part>";
}
ss << "</CompleteMultipartUpload>";
string body = ss.str();
// Response is around ~400 in AWS docs so this should be enough to not need a resize
idx_t response_buffer_len = 1000;
auto response_buffer = duckdb::unique_ptr<char[]> {new char[response_buffer_len]};
string query_param = "uploadId=" + S3FileSystem::UrlEncode(file_handle.multipart_upload_id, true);
auto res = s3fs.PostRequest(file_handle, file_handle.path, {}, response_buffer, response_buffer_len,
(char *)body.c_str(), body.length(), query_param);
string result(response_buffer.get(), response_buffer_len);
auto open_tag_pos = result.find("<CompleteMultipartUploadResult", 0);
if (open_tag_pos == string::npos) {
throw HTTPException(*res, "Unexpected response during S3 multipart upload finalization: %d\n\n%s", res->code,
result);
}
}
// Wrapper around the BufferManager::Allocate to that allows limiting the number of buffers that will be handed out
BufferHandle S3FileSystem::Allocate(idx_t part_size, uint16_t max_threads) {
return buffer_manager.Allocate(MemoryTag::EXTENSION, part_size);
}
shared_ptr<S3WriteBuffer> S3FileHandle::GetBuffer(uint16_t write_buffer_idx) {
auto &s3fs = (S3FileSystem &)file_system;
// Check if write buffer already exists
{
unique_lock<mutex> lck(write_buffers_lock);
auto lookup_result = write_buffers.find(write_buffer_idx);
if (lookup_result != write_buffers.end()) {
shared_ptr<S3WriteBuffer> buffer = lookup_result->second;
return buffer;
}
}
auto buffer_handle = s3fs.Allocate(part_size, config_params.max_upload_threads);
auto new_write_buffer =
make_shared_ptr<S3WriteBuffer>(write_buffer_idx * part_size, part_size, std::move(buffer_handle));
{
unique_lock<mutex> lck(write_buffers_lock);
auto lookup_result = write_buffers.find(write_buffer_idx);
// Check if other thread has created the same buffer, if so we return theirs and drop ours.
if (lookup_result != write_buffers.end()) {
// write_buffer_idx << std::endl;
shared_ptr<S3WriteBuffer> write_buffer = lookup_result->second;
return write_buffer;
}
write_buffers.insert(pair<uint16_t, shared_ptr<S3WriteBuffer>>(write_buffer_idx, new_write_buffer));
}
return new_write_buffer;
}
void S3FileSystem::GetQueryParam(const string &key, string ¶m, duckdb_httplib_openssl::Params &query_params) {
auto found_param = query_params.find(key);
if (found_param != query_params.end()) {
param = found_param->second;
query_params.erase(found_param);
}
}
void S3FileSystem::ReadQueryParams(const string &url_query_param, S3AuthParams ¶ms) {
if (url_query_param.empty()) {
return;
}
duckdb_httplib_openssl::Params query_params;
duckdb_httplib_openssl::detail::parse_query_text(url_query_param, query_params);
GetQueryParam("s3_region", params.region, query_params);
GetQueryParam("s3_access_key_id", params.access_key_id, query_params);
GetQueryParam("s3_secret_access_key", params.secret_access_key, query_params);
GetQueryParam("s3_session_token", params.session_token, query_params);
GetQueryParam("s3_endpoint", params.endpoint, query_params);
GetQueryParam("s3_url_style", params.url_style, query_params);
auto found_param = query_params.find("s3_use_ssl");
if (found_param != query_params.end()) {
if (found_param->second == "true") {
params.use_ssl = true;
} else if (found_param->second == "false") {
params.use_ssl = false;
} else {
throw IOException("Incorrect setting found for s3_use_ssl, allowed values are: 'true' or 'false'");
}
query_params.erase(found_param);
}
if (!query_params.empty()) {
throw IOException("Invalid query parameters found. Supported parameters are:\n's3_region', 's3_access_key_id', "
"'s3_secret_access_key', 's3_session_token',\n's3_endpoint', 's3_url_style', 's3_use_ssl'");
}
}
static string GetPrefix(string url) {
const string prefixes[] = {"s3://", "s3a://", "s3n://", "gcs://", "gs://", "r2://"};
for (auto &prefix : prefixes) {
if (StringUtil::StartsWith(url, prefix)) {
return prefix;
}
}
throw IOException("URL needs to start with s3://, gcs:// or r2://");
return string();
}
ParsedS3Url S3FileSystem::S3UrlParse(string url, S3AuthParams ¶ms) {
string http_proto, prefix, host, bucket, key, path, query_param, trimmed_s3_url;
prefix = GetPrefix(url);
auto prefix_end_pos = url.find("//") + 2;
auto slash_pos = url.find('/', prefix_end_pos);
if (slash_pos == string::npos) {
throw IOException("URL needs to contain a '/' after the host");
}
bucket = url.substr(prefix_end_pos, slash_pos - prefix_end_pos);
if (bucket.empty()) {
throw IOException("URL needs to contain a bucket name");
}
if (params.s3_url_compatibility_mode) {
// In url compatibility mode, we will ignore any special chars, so query param strings are disabled
trimmed_s3_url = url;
key += url.substr(slash_pos);
} else {
// Parse query parameters
auto question_pos = url.find_first_of('?');
if (question_pos != string::npos) {
query_param = url.substr(question_pos + 1);
trimmed_s3_url = url.substr(0, question_pos);
} else {
trimmed_s3_url = url;
}
if (!query_param.empty()) {
key += url.substr(slash_pos, question_pos - slash_pos);
} else {
key += url.substr(slash_pos);
}
}
if (key.empty()) {
throw IOException("URL needs to contain key");
}
// Derived host and path based on the endpoint
auto sub_path_pos = params.endpoint.find_first_of('/');
if (sub_path_pos != string::npos) {
// Host header should conform to <host>:<port> so not include the path
host = params.endpoint.substr(0, sub_path_pos);
path = params.endpoint.substr(sub_path_pos);
} else {
host = params.endpoint;
path = "";
}
// Update host and path according to the url style
// See https://docs.aws.amazon.com/AmazonS3/latest/userguide/VirtualHosting.html
if (params.url_style == "vhost" || params.url_style == "") {
host = bucket + "." + host;
} else if (params.url_style == "path") {
path += "/" + bucket;
}
// Append key (including leading slash) to the path
path += key;
// Remove leading slash from key
key = key.substr(1);
http_proto = params.use_ssl ? "https://" : "http://";
return {http_proto, prefix, host, bucket, key, path, query_param, trimmed_s3_url};
}
string S3FileSystem::GetPayloadHash(char *buffer, idx_t buffer_len) {
if (buffer_len > 0) {
hash_bytes payload_hash_bytes;
hash_str payload_hash_str;
sha256(buffer, buffer_len, payload_hash_bytes);
hex256(payload_hash_bytes, payload_hash_str);
return string((char *)payload_hash_str, sizeof(payload_hash_str));
} else {
return "";
}
}
string ParsedS3Url::GetHTTPUrl(S3AuthParams &auth_params, const string &http_query_string) {
string full_url = http_proto + host + S3FileSystem::UrlEncode(path);
if (!http_query_string.empty()) {
full_url += "?" + http_query_string;
}
return full_url;
}
unique_ptr<ResponseWrapper> S3FileSystem::PostRequest(FileHandle &handle, string url, HeaderMap header_map,
duckdb::unique_ptr<char[]> &buffer_out, idx_t &buffer_out_len,
char *buffer_in, idx_t buffer_in_len, string http_params) {
auto auth_params = handle.Cast<S3FileHandle>().auth_params;
auto parsed_s3_url = S3UrlParse(url, auth_params);
string http_url = parsed_s3_url.GetHTTPUrl(auth_params, http_params);
auto payload_hash = GetPayloadHash(buffer_in, buffer_in_len);
auto headers = create_s3_header(parsed_s3_url.path, http_params, parsed_s3_url.host, "s3", "POST", auth_params, "",
"", payload_hash, "application/octet-stream");
return HTTPFileSystem::PostRequest(handle, http_url, headers, buffer_out, buffer_out_len, buffer_in, buffer_in_len);
}
unique_ptr<ResponseWrapper> S3FileSystem::PutRequest(FileHandle &handle, string url, HeaderMap header_map,
char *buffer_in, idx_t buffer_in_len, string http_params) {
auto auth_params = handle.Cast<S3FileHandle>().auth_params;
auto parsed_s3_url = S3UrlParse(url, auth_params);
string http_url = parsed_s3_url.GetHTTPUrl(auth_params, http_params);
auto content_type = "application/octet-stream";
auto payload_hash = GetPayloadHash(buffer_in, buffer_in_len);
auto headers = create_s3_header(parsed_s3_url.path, http_params, parsed_s3_url.host, "s3", "PUT", auth_params, "",
"", payload_hash, content_type);
return HTTPFileSystem::PutRequest(handle, http_url, headers, buffer_in, buffer_in_len);
}
unique_ptr<ResponseWrapper> S3FileSystem::HeadRequest(FileHandle &handle, string s3_url, HeaderMap header_map) {
auto auth_params = handle.Cast<S3FileHandle>().auth_params;
auto parsed_s3_url = S3UrlParse(s3_url, auth_params);
string http_url = parsed_s3_url.GetHTTPUrl(auth_params);
auto headers =
create_s3_header(parsed_s3_url.path, "", parsed_s3_url.host, "s3", "HEAD", auth_params, "", "", "", "");
return HTTPFileSystem::HeadRequest(handle, http_url, headers);
}
unique_ptr<ResponseWrapper> S3FileSystem::GetRequest(FileHandle &handle, string s3_url, HeaderMap header_map) {
auto auth_params = handle.Cast<S3FileHandle>().auth_params;
auto parsed_s3_url = S3UrlParse(s3_url, auth_params);
string http_url = parsed_s3_url.GetHTTPUrl(auth_params);
auto headers =
create_s3_header(parsed_s3_url.path, "", parsed_s3_url.host, "s3", "GET", auth_params, "", "", "", "");
return HTTPFileSystem::GetRequest(handle, http_url, headers);
}
unique_ptr<ResponseWrapper> S3FileSystem::GetRangeRequest(FileHandle &handle, string s3_url, HeaderMap header_map,
idx_t file_offset, char *buffer_out, idx_t buffer_out_len) {
auto auth_params = handle.Cast<S3FileHandle>().auth_params;
auto parsed_s3_url = S3UrlParse(s3_url, auth_params);
string http_url = parsed_s3_url.GetHTTPUrl(auth_params);
auto headers =
create_s3_header(parsed_s3_url.path, "", parsed_s3_url.host, "s3", "GET", auth_params, "", "", "", "");
return HTTPFileSystem::GetRangeRequest(handle, http_url, headers, file_offset, buffer_out, buffer_out_len);
}
unique_ptr<ResponseWrapper> S3FileSystem::DeleteRequest(FileHandle &handle, string s3_url, HeaderMap header_map) {
auto auth_params = handle.Cast<S3FileHandle>().auth_params;
auto parsed_s3_url = S3UrlParse(s3_url, auth_params);
string http_url = parsed_s3_url.GetHTTPUrl(auth_params);
auto headers =
create_s3_header(parsed_s3_url.path, "", parsed_s3_url.host, "s3", "DELETE", auth_params, "", "", "", "");
return HTTPFileSystem::DeleteRequest(handle, http_url, headers);
}
unique_ptr<HTTPFileHandle> S3FileSystem::CreateHandle(const OpenFileInfo &file, FileOpenFlags flags,
optional_ptr<FileOpener> opener) {
FileOpenerInfo info = {file.path};
S3AuthParams auth_params = S3AuthParams::ReadFrom(opener, info);
// Scan the query string for any s3 authentication parameters
auto parsed_s3_url = S3UrlParse(file.path, auth_params);
ReadQueryParams(parsed_s3_url.query_param, auth_params);
return duckdb::make_uniq<S3FileHandle>(*this, file, flags, HTTPParams::ReadFrom(opener, info), auth_params,
S3ConfigParams::ReadFrom(opener));
}
// this computes the signature from https://czak.pl/2015/09/15/s3-rest-api-with-curl.html
void S3FileSystem::Verify() {
S3AuthParams auth_params;
auth_params.region = "us-east-1";
auth_params.access_key_id = "AKIAIOSFODNN7EXAMPLE";
auth_params.secret_access_key = "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY";
auto test_header = create_s3_header("/", "", "my-precious-bucket.s3.amazonaws.com", "s3", "GET", auth_params,
"20150915", "20150915T124500Z");
if (test_header["Authorization"] !=
"AWS4-HMAC-SHA256 Credential=AKIAIOSFODNN7EXAMPLE/20150915/us-east-1/s3/aws4_request, "
"SignedHeaders=host;x-amz-content-sha256;x-amz-date, "
"Signature=182072eb53d85c36b2d791a1fa46a12d23454ec1e921b02075c23aee40166d5a") {
throw std::runtime_error("test fail");
}
if (UrlEncode("/category=Books/") != "/category%3DBooks/") {
throw std::runtime_error("test fail");
}
if (UrlEncode("/?category=Books&title=Ducks Retreat/") != "/%3Fcategory%3DBooks%26title%3DDucks%20Retreat/") {
throw std::runtime_error("test fail");
}
if (UrlEncode("/?category=Books&title=Ducks Retreat/", true) !=
"%2F%3Fcategory%3DBooks%26title%3DDucks%20Retreat%2F") {
throw std::runtime_error("test fail");
}
// AWS_SECRET_ACCESS_KEY="vs1BZPxSL2qVARBSg5vCMKJsavCoEPlo/HSHRaVe" AWS_ACCESS_KEY_ID="ASIAYSPIOYDTHTBIITVC"
// AWS_SESSION_TOKEN="IQoJb3JpZ2luX2VjENX//////////wEaCWV1LXdlc3QtMSJHMEUCIQDfjzs9BYHrEXDMU/NR+PHV1uSTr7CSVSQdjKSfiPRLdgIgCCztF0VMbi9+uHHAfBVKhV4t9MlUrQg3VAOIsLxrWyoqlAIIHRAAGgw1ODk0MzQ4OTY2MTQiDOGl2DsYxENcKCbh+irxARe91faI+hwUhT60sMGRFg0GWefKnPclH4uRFzczrDOcJlAAaQRJ7KOsT8BrJlrY1jSgjkO7PkVjPp92vi6lJX77bg99MkUTJActiOKmd84XvAE5bFc/jFbqechtBjXzopAPkKsGuaqAhCenXnFt6cwq+LZikv/NJGVw7TRphLV+Aq9PSL9XwdzIgsW2qXwe1c3rxDNj53yStRZHVggdxJ0OgHx5v040c98gFphzSULHyg0OY6wmCMTYcswpb4kO2IIi6AiD9cY25TlwPKRKPi5CdBsTPnyTeW62u7PvwK0fTSy4ZuJUuGKQnH2cKmCXquEwoOHEiQY6nQH9fzY/EDGHMRxWWhxu0HiqIfsuFqC7GS0p0ToKQE+pzNsvVwMjZc+KILIDDQpdCWRIwu53I5PZy2Cvk+3y4XLvdZKQCsAKqeOc4c94UAS4NmUT7mCDOuRV0cLBVM8F0JYBGrUxyI+YoIvHhQWmnRLuKgTb5PkF7ZWrXBHFWG5/tZDOvBbbaCWTlRCL9b0Vpg5+BM/81xd8jChP4w83"
// aws --region eu-west-1 --debug s3 ls my-precious-bucket 2>&1 | less
string canonical_query_string = "delimiter=%2F&encoding-type=url&list-type=2&prefix="; // aws s3 ls <bucket>
S3AuthParams auth_params2;
auth_params2.region = "eu-west-1";
auth_params2.access_key_id = "ASIAYSPIOYDTHTBIITVC";
auth_params2.secret_access_key = "vs1BZPxSL2qVARBSg5vCMKJsavCoEPlo/HSHRaVe";
auth_params2.session_token =
"IQoJb3JpZ2luX2VjENX//////////wEaCWV1LXdlc3QtMSJHMEUCIQDfjzs9BYHrEXDMU/"
"NR+PHV1uSTr7CSVSQdjKSfiPRLdgIgCCztF0VMbi9+"
"uHHAfBVKhV4t9MlUrQg3VAOIsLxrWyoqlAIIHRAAGgw1ODk0MzQ4OTY2MTQiDOGl2DsYxENcKCbh+irxARe91faI+"
"hwUhT60sMGRFg0GWefKnPclH4uRFzczrDOcJlAAaQRJ7KOsT8BrJlrY1jSgjkO7PkVjPp92vi6lJX77bg99MkUTJA"
"ctiOKmd84XvAE5bFc/jFbqechtBjXzopAPkKsGuaqAhCenXnFt6cwq+LZikv/"
"NJGVw7TRphLV+"
"Aq9PSL9XwdzIgsW2qXwe1c3rxDNj53yStRZHVggdxJ0OgHx5v040c98gFphzSULHyg0OY6wmCMTYcswpb4kO2IIi6"
"AiD9cY25TlwPKRKPi5CdBsTPnyTeW62u7PvwK0fTSy4ZuJUuGKQnH2cKmCXquEwoOHEiQY6nQH9fzY/"
"EDGHMRxWWhxu0HiqIfsuFqC7GS0p0ToKQE+pzNsvVwMjZc+KILIDDQpdCWRIwu53I5PZy2Cvk+"
"3y4XLvdZKQCsAKqeOc4c94UAS4NmUT7mCDOuRV0cLBVM8F0JYBGrUxyI+"
"YoIvHhQWmnRLuKgTb5PkF7ZWrXBHFWG5/tZDOvBbbaCWTlRCL9b0Vpg5+BM/81xd8jChP4w83";
auto test_header2 = create_s3_header("/", canonical_query_string, "my-precious-bucket.s3.eu-west-1.amazonaws.com",
"s3", "GET", auth_params2, "20210904", "20210904T121746Z");
if (test_header2["Authorization"] !=
"AWS4-HMAC-SHA256 Credential=ASIAYSPIOYDTHTBIITVC/20210904/eu-west-1/s3/aws4_request, "
"SignedHeaders=host;x-amz-content-sha256;x-amz-date;x-amz-security-token, "
"Signature=4d9d6b59d7836b6485f6ad822de97be40287da30347d83042ea7fbed530dc4c0") {
throw std::runtime_error("test fail");
}
S3AuthParams auth_params3;
auth_params3.region = "eu-west-1";
auth_params3.access_key_id = "S3RVER";
auth_params3.secret_access_key = "S3RVER";
auto test_header3 =
create_s3_header("/correct_auth_test.csv", "", "test-bucket-ceiveran.s3.amazonaws.com", "s3", "PUT",
auth_params3, "20220121", "20220121T141452Z",
"28a0cf6ac5c4cb73793091fe6ecc6a68bf90855ac9186158748158f50241bb0c", "text/data;charset=utf-8");
if (test_header3["Authorization"] != "AWS4-HMAC-SHA256 Credential=S3RVER/20220121/eu-west-1/s3/aws4_request, "
"SignedHeaders=content-type;host;x-amz-content-sha256;x-amz-date, "
"Signature=5d9a6cbfaa78a6d0f2ab7df0445e2f1cc9c80cd3655ac7de9e7219c036f23f02") {
throw std::runtime_error("test3 fail");
}
// bug #4082
S3AuthParams auth_params4;
auth_params4.region = "auto";
auth_params4.access_key_id = "asdf";
auth_params4.secret_access_key = "asdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdf";
create_s3_header("/", "", "exampple.com", "s3", "GET", auth_params4);
if (UrlEncode("/category=Books/") != "/category%3DBooks/") {
throw std::runtime_error("test fail");
}
if (UrlEncode("/?category=Books&title=Ducks Retreat/") != "/%3Fcategory%3DBooks%26title%3DDucks%20Retreat/") {
throw std::runtime_error("test fail");
}
if (UrlEncode("/?category=Books&title=Ducks Retreat/", true) !=
"%2F%3Fcategory%3DBooks%26title%3DDucks%20Retreat%2F") {
throw std::runtime_error("test fail");
}
// TODO add a test that checks the signing for path-style
}
void S3FileHandle::Initialize(optional_ptr<FileOpener> opener) {
try {
HTTPFileHandle::Initialize(opener);
} catch (std::exception &ex) {
ErrorData error(ex);
if (error.Type() == ExceptionType::IO || error.Type() == ExceptionType::HTTP) {
bool refreshed_secret = false;
auto context = opener->TryGetClientContext();
if (context) {
auto transaction = CatalogTransaction::GetSystemCatalogTransaction(*context);
for (const string type : {"s3", "r2", "gcs", "aws"}) {
auto res = context->db->GetSecretManager().LookupSecret(transaction, path, type);
if (res.HasMatch()) {
refreshed_secret |= CreateS3SecretFunctions::TryRefreshS3Secret(*context, *res.secret_entry);
}
}
}
if (refreshed_secret) {
// We have succesfully refreshed a secret: retry initializing with new credentials
FileOpenerInfo info = {path};
auth_params = S3AuthParams::ReadFrom(opener, info);
HTTPFileHandle::Initialize(opener);
return;
}
}
throw;
}
auto &s3fs = file_system.Cast<S3FileSystem>();
if (flags.OpenForWriting()) {
auto aws_minimum_part_size = 5242880; // 5 MiB https://docs.aws.amazon.com/AmazonS3/latest/userguide/qfacts.html
auto max_part_count = config_params.max_parts_per_file;
auto required_part_size = config_params.max_file_size / max_part_count;
auto minimum_part_size = MaxValue<idx_t>(aws_minimum_part_size, required_part_size);
// Round part size up to multiple of Storage::DEFAULT_BLOCK_SIZE
part_size = ((minimum_part_size + Storage::DEFAULT_BLOCK_SIZE - 1) / Storage::DEFAULT_BLOCK_SIZE) *
Storage::DEFAULT_BLOCK_SIZE;
D_ASSERT(part_size * max_part_count >= config_params.max_file_size);
multipart_upload_id = s3fs.InitializeMultipartUpload(*this);
}
}
bool S3FileSystem::CanHandleFile(const string &fpath) {
return fpath.rfind("s3://", 0) * fpath.rfind("s3a://", 0) * fpath.rfind("s3n://", 0) * fpath.rfind("gcs://", 0) *
fpath.rfind("gs://", 0) * fpath.rfind("r2://", 0) ==
0;
}
void S3FileSystem::RemoveFile(const string &path, optional_ptr<FileOpener> opener) {
auto handle = OpenFile(path, FileFlags::FILE_FLAGS_NULL_IF_NOT_EXISTS, opener);
if (!handle) {
throw IOException("Could not remove file \"%s\": %s", {{"errno", "404"}}, path, "No such file or directory");
}
auto &s3fh = handle->Cast<S3FileHandle>();
auto res = DeleteRequest(*handle, s3fh.path, {});
if (res->code != 200 && res->code != 204) {
throw IOException("Could not remove file \"%s\": %s", {{"errno", to_string(res->code)}}, path, res->error);
}
}
void S3FileSystem::RemoveDirectory(const string &path, optional_ptr<FileOpener> opener) {
ListFiles(
path,
[&](const string &file, bool is_dir) {
try {
this->RemoveFile(file, opener);
} catch (IOException &e) {
string errmsg(e.what());
if (errmsg.find("No such file or directory") != std::string::npos) {
return;
}
throw;
}
},
opener.get());
}
void S3FileSystem::FileSync(FileHandle &handle) {
auto &s3fh = handle.Cast<S3FileHandle>();
if (!s3fh.upload_finalized) {
FlushAllBuffers(s3fh);
FinalizeMultipartUpload(s3fh);
}
}
void S3FileSystem::Write(FileHandle &handle, void *buffer, int64_t nr_bytes, idx_t location) {
auto &s3fh = handle.Cast<S3FileHandle>();
if (!s3fh.flags.OpenForWriting()) {
throw InternalException("Write called on file not opened in write mode");
}
int64_t bytes_written = 0;
while (bytes_written < nr_bytes) {
auto curr_location = location + bytes_written;
if (curr_location != s3fh.file_offset) {
throw InternalException("Non-sequential write not supported!");
}
// Find buffer for writing
auto write_buffer_idx = curr_location / s3fh.part_size;
// Get write buffer, may block until buffer is available
auto write_buffer = s3fh.GetBuffer(write_buffer_idx);
// Writing to buffer
auto idx_to_write = curr_location - write_buffer->buffer_start;
auto bytes_to_write = MinValue<idx_t>(nr_bytes - bytes_written, s3fh.part_size - idx_to_write);
memcpy((char *)write_buffer->Ptr() + idx_to_write, (char *)buffer + bytes_written, bytes_to_write);
write_buffer->idx += bytes_to_write;
// Flush to HTTP if full
if (write_buffer->idx >= s3fh.part_size) {
FlushBuffer(s3fh, write_buffer);
}
s3fh.file_offset += bytes_to_write;
bytes_written += bytes_to_write;
}
}
static bool Match(vector<string>::const_iterator key, vector<string>::const_iterator key_end,
vector<string>::const_iterator pattern, vector<string>::const_iterator pattern_end) {
while (key != key_end && pattern != pattern_end) {
if (*pattern == "**") {
if (std::next(pattern) == pattern_end) {
return true;
}
while (key != key_end) {
if (Match(key, key_end, std::next(pattern), pattern_end)) {
return true;
}
key++;
}
return false;
}
if (!Glob(key->data(), key->length(), pattern->data(), pattern->length())) {
return false;
}
key++;
pattern++;
}
return key == key_end && pattern == pattern_end;
}
vector<OpenFileInfo> S3FileSystem::Glob(const string &glob_pattern, FileOpener *opener) {
if (opener == nullptr) {
throw InternalException("Cannot S3 Glob without FileOpener");
}
FileOpenerInfo info = {glob_pattern};
// Trim any query parameters from the string
S3AuthParams s3_auth_params = S3AuthParams::ReadFrom(opener, info);
// In url compatibility mode, we ignore globs allowing users to query files with the glob chars
if (s3_auth_params.s3_url_compatibility_mode) {
return {glob_pattern};
}
auto parsed_s3_url = S3UrlParse(glob_pattern, s3_auth_params);
auto parsed_glob_url = parsed_s3_url.trimmed_s3_url;
// AWS matches on prefix, not glob pattern, so we take a substring until the first wildcard char for the aws calls
auto first_wildcard_pos = parsed_glob_url.find_first_of("*[\\");
if (first_wildcard_pos == string::npos) {
return {glob_pattern};
}
string shared_path = parsed_glob_url.substr(0, first_wildcard_pos);