-
Notifications
You must be signed in to change notification settings - Fork 89
Expand file tree
/
Copy pathclient.cc
More file actions
1534 lines (1364 loc) · 49.3 KB
/
Copy pathclient.cc
File metadata and controls
1534 lines (1364 loc) · 49.3 KB
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
// MinIO C++ Library for Amazon S3 Compatible Cloud Storage
// Copyright 2022-2024 MinIO, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// SPDX-License-Identifier: Apache-2.0
#include "miniocpp/client.h"
#ifdef _MSC_VER
#include <malloc.h>
#else
#include <unistd.h>
#endif
#include <curlpp/cURLpp.hpp>
#include <deque>
#include <filesystem>
#include <fstream>
#include <future>
#include <iostream>
#include <list>
#include <memory>
#include <string>
#include <system_error>
#include <type_traits>
#include <vector>
#include "miniocpp/args.h"
#include "miniocpp/baseclient.h"
#include "miniocpp/error.h"
#include "miniocpp/http.h"
#include "miniocpp/providers.h"
#include "miniocpp/request.h"
#include "miniocpp/response.h"
#include "miniocpp/sse.h"
#include "miniocpp/types.h"
#include "miniocpp/utils.h"
#ifdef MINIO_CPP_RDMA
#include "miniocpp/nvidia-cuobjclient.h"
#include "miniocpp/rdma.h"
#endif
namespace minio::s3 {
namespace {
#ifdef _MSC_VER
inline size_t GetPageSize() { return 4096; }
inline int AlignedAlloc(void** out, size_t alignment, size_t size) {
*out = _aligned_malloc(size, alignment);
return *out ? 0 : -1;
}
inline void AlignedFree(void* p) { _aligned_free(p); }
#else
inline size_t GetPageSize() { return static_cast<size_t>(getpagesize()); }
inline int AlignedAlloc(void** out, size_t alignment, size_t size) {
return posix_memalign(out, alignment, size);
}
inline void AlignedFree(void* p) { free(p); }
#endif
struct AlignedBuffer {
void* ptr = nullptr;
AlignedBuffer() = default;
explicit AlignedBuffer(void* p) : ptr(p) {}
AlignedBuffer(const AlignedBuffer&) = delete;
AlignedBuffer& operator=(const AlignedBuffer&) = delete;
AlignedBuffer(AlignedBuffer&& o) noexcept : ptr(o.ptr) { o.ptr = nullptr; }
AlignedBuffer& operator=(AlignedBuffer&& o) noexcept {
if (this != &o) {
if (ptr) AlignedFree(ptr);
ptr = o.ptr;
o.ptr = nullptr;
}
return *this;
}
~AlignedBuffer() {
if (ptr) AlignedFree(ptr);
}
};
#ifdef MINIO_CPP_RDMA
// Releases an RDMA buffer registration when it goes out of scope. Declared
// *after* the buffer it covers, so destruction order (reverse of declaration)
// guarantees deregister-before-free regardless of which control path returns.
struct ScopedRDMARegistration {
cuObjClient* client = nullptr;
void* buf = nullptr;
ScopedRDMARegistration() = default;
ScopedRDMARegistration(cuObjClient* c, void* p) : client(c), buf(p) {}
ScopedRDMARegistration(const ScopedRDMARegistration&) = delete;
ScopedRDMARegistration& operator=(const ScopedRDMARegistration&) = delete;
ScopedRDMARegistration(ScopedRDMARegistration&& o) noexcept
: client(o.client), buf(o.buf) {
o.client = nullptr;
o.buf = nullptr;
}
ScopedRDMARegistration& operator=(ScopedRDMARegistration&& o) noexcept {
if (this != &o) {
Release();
client = o.client;
buf = o.buf;
o.client = nullptr;
o.buf = nullptr;
}
return *this;
}
~ScopedRDMARegistration() { Release(); }
private:
void Release() {
if (client && buf && client->cuMemObjPutDescriptor(buf) != 0) {
std::cerr << "warning: cuMemObjPutDescriptor failed during teardown"
<< std::endl;
}
client = nullptr;
buf = nullptr;
}
};
#endif
} // namespace
ListObjectsResult::ListObjectsResult(error::Error err) : failed_(true) {
resp_ = std::make_shared<ListObjectsResponse>();
resp_->contents.push_back(Item(std::move(err)));
itr_ = resp_->contents.begin();
}
ListObjectsResult::ListObjectsResult(Client* const client,
const ListObjectsArgs& args)
: client_(client), args_(args) {
resp_ = std::make_shared<ListObjectsResponse>();
itr_ = resp_->contents.end();
StartPrefetch();
}
ListObjectsResult::ListObjectsResult(Client* const client,
ListObjectsArgs&& args)
: client_(client), args_(std::move(args)) {
resp_ = std::make_shared<ListObjectsResponse>();
itr_ = resp_->contents.end();
StartPrefetch();
}
void ListObjectsResult::UpdatePaginationArgs() {
if (args_.include_versions || !args_.version_id_marker.empty()) {
args_.key_marker = resp_->next_key_marker;
args_.version_id_marker = resp_->next_version_id_marker;
} else if (args_.use_api_v1) {
args_.marker = resp_->next_marker;
} else {
args_.start_after = resp_->start_after;
args_.continuation_token = resp_->next_continuation_token;
}
}
void ListObjectsResult::StartPrefetch() {
ListObjectsArgs next_args = args_;
try {
prefetch_future_ = std::make_shared<
std::shared_future<std::shared_ptr<ListObjectsResponse>>>(std::async(
std::launch::async,
[client = client_, next_args = std::move(next_args)]() mutable
-> std::shared_ptr<ListObjectsResponse> {
try {
GetRegionResponse resp =
client->GetRegion(next_args.bucket, next_args.region);
if (resp) {
next_args.region = resp.region;
if (next_args.recursive) {
next_args.delimiter = "";
} else if (next_args.delimiter.empty()) {
next_args.delimiter = "/";
}
if (next_args.include_versions ||
!next_args.version_id_marker.empty()) {
return std::make_shared<ListObjectsResponse>(
client->ListObjectVersions(
ListObjectVersionsArgs(next_args)));
} else if (next_args.use_api_v1) {
return std::make_shared<ListObjectsResponse>(
client->ListObjectsV1(ListObjectsV1Args(next_args)));
} else {
return std::make_shared<ListObjectsResponse>(
client->ListObjectsV2(ListObjectsV2Args(next_args)));
}
}
return std::make_shared<ListObjectsResponse>(resp);
} catch (const std::exception& e) {
return std::make_shared<ListObjectsResponse>(
error::Error(std::string("prefetch failed: ") + e.what()));
}
}));
} catch (const std::exception& e) {
std::promise<std::shared_ptr<ListObjectsResponse>> p;
p.set_value(std::make_shared<ListObjectsResponse>(
error::Error(std::string("failed to launch prefetch: ") + e.what())));
prefetch_future_ = std::make_shared<
std::shared_future<std::shared_ptr<ListObjectsResponse>>>(
p.get_future());
}
}
void ListObjectsResult::Populate() {
if (!prefetch_future_ || !prefetch_future_->valid()) {
return;
}
try {
resp_ = prefetch_future_->get();
} catch (const std::exception& e) {
resp_ = std::make_shared<ListObjectsResponse>(
error::Error(std::string("prefetch result failed: ") + e.what()));
}
prefetch_future_.reset();
if (!*resp_) {
failed_ = true;
resp_->contents.push_back(Item(*resp_));
}
itr_ = resp_->contents.begin();
if (*resp_ && resp_->is_truncated) {
UpdatePaginationArgs();
StartPrefetch();
}
}
RemoveObjectsResult::RemoveObjectsResult(error::Error err) {
done_ = true;
resp_.errors.push_back(DeleteError(err));
itr_ = resp_.errors.begin();
}
RemoveObjectsResult::RemoveObjectsResult(Client* const client,
const RemoveObjectsArgs& args)
: client_(client), args_(args) {
Populate();
}
RemoveObjectsResult::RemoveObjectsResult(Client* const client,
RemoveObjectsArgs&& args)
: client_(client), args_(args) {
Populate();
}
void RemoveObjectsResult::Populate() {
while (!done_ && resp_.errors.size() == 0) {
RemoveObjectsApiArgs args;
args.extra_headers = args_.extra_headers;
args.extra_query_params = args_.extra_query_params;
args.bucket = args_.bucket;
args.region = args_.region;
args.quiet = true;
args.bypass_governance_mode = args_.bypass_governance_mode;
for (int i = 0; i < 1000; i++) {
DeleteObject object;
if (!args_.func(object)) {
break;
}
args.objects.push_back(object);
}
if (args.objects.size() != 0) {
resp_ = client_->BaseClient::RemoveObjects(args);
if (!resp_) {
resp_.errors.push_back(DeleteError(resp_));
}
itr_ = resp_.errors.begin();
} else {
done_ = true;
}
}
// Caller's func may have returned false on the very first call (empty
// batch). `done_` flips to true above but itr_ was never assigned, so
// operator bool() would compare an uninitialized iterator. Pin it to
// end() so the result evaluates to false cleanly.
if (done_ && resp_.errors.empty()) {
itr_ = resp_.errors.end();
}
}
#ifdef MINIO_CPP_RDMA
// Meyers singleton — thread-safe per C++11 [stmt.dcl]/4 ("If control
// enters the declaration concurrently while the variable is being
// initialized, the concurrent execution shall wait for completion of the
// initialization."). This replaces the previous per-call cuObjClient
// construction, which was racy under concurrency and caused the
// glibc-level "malloc(): invalid size (unsorted)" abort when multiple
// warp workers started up simultaneously.
cuObjClient& Client::SharedRDMAClient() {
static CUObjIOOps ops{};
static cuObjClient client{ops, CUOBJ_PROTO_RDMA_DC_V1};
return client;
}
#endif
Client::Client(BaseUrl& base_url, creds::Provider* const provider)
: BaseClient(base_url, provider) {}
StatObjectResponse Client::CalculatePartCount(
size_t& part_count, std::list<ComposeSource> sources) {
size_t object_size = 0;
size_t i = 0;
for (auto& source : sources) {
if (source.ssec != nullptr && !base_url_.https) {
std::string msg = "source " + source.bucket + "/" + source.object;
if (!source.version_id.empty()) {
msg += "?versionId=" + source.version_id;
}
msg += ": SSE-C operation must be performed over a secure connection";
return error::make<StatObjectResponse>(msg);
}
i++;
std::string etag;
size_t size;
StatObjectResponse resp = StatObject(source);
if (!resp) {
return resp;
}
etag = resp.etag;
size = resp.size;
if (error::Error err = source.BuildHeaders(size, etag)) {
return StatObjectResponse(err);
}
if (source.length.has_value()) {
size = *source.length;
} else if (source.offset.has_value()) {
size -= *source.offset;
}
if (size < utils::kMinPartSize && sources.size() != 1 &&
i != sources.size()) {
std::string msg = "source " + source.bucket + "/" + source.object;
if (!source.version_id.empty()) msg += "?versionId=" + source.version_id;
msg += ": size " + std::to_string(size) + " must be greater than " +
std::to_string(utils::kMinPartSize);
return error::make<StatObjectResponse>(msg);
}
object_size += size;
if (object_size > utils::kMaxObjectSize) {
return error::make<StatObjectResponse>(
"destination object size must be less than " +
std::to_string(utils::kMaxObjectSize));
}
if (size > utils::kMaxPartSize) {
size_t count = size / utils::kMaxPartSize;
size_t last_part_size = size - (count * utils::kMaxPartSize);
if (last_part_size > 0) {
count++;
} else {
last_part_size = utils::kMaxPartSize;
}
if (last_part_size < utils::kMinPartSize && sources.size() != 1 &&
i != sources.size()) {
std::string msg = "source " + source.bucket + "/" + source.object;
if (!source.version_id.empty()) {
msg += "?versionId=" + source.version_id;
}
msg += ": size " + std::to_string(size) +
" for multipart split upload of " + std::to_string(size) +
", last part size is less than " +
std::to_string(utils::kMinPartSize);
return error::make<StatObjectResponse>(msg);
}
part_count += count;
} else {
part_count++;
}
if (part_count > utils::kMaxMultipartCount) {
return error::make<StatObjectResponse>(
"Compose sources create more than allowed multipart count " +
std::to_string(utils::kMaxMultipartCount));
}
}
return StatObjectResponse(error::SUCCESS);
}
ComposeObjectResponse Client::ComposeObject(ComposeObjectArgs args,
std::string& upload_id) {
size_t part_count = 0;
{
StatObjectResponse resp = CalculatePartCount(part_count, args.sources);
if (!resp) {
return ComposeObjectResponse(resp);
}
}
ComposeSource& source = args.sources.front();
if (part_count == 1 && !source.offset.has_value() &&
!source.length.has_value()) {
CopyObjectArgs coargs;
coargs.extra_headers = args.extra_headers;
coargs.extra_query_params = args.extra_query_params;
coargs.bucket = args.bucket;
coargs.region = args.region;
coargs.object = args.object;
coargs.sse = args.sse;
coargs.source = source;
return ComposeObjectResponse(CopyObject(coargs));
}
utils::Multimap headers = args.Headers();
{
CreateMultipartUploadArgs cmu_args;
cmu_args.extra_query_params = args.extra_query_params;
cmu_args.bucket = args.bucket;
cmu_args.region = args.region;
cmu_args.object = args.object;
cmu_args.headers = headers;
if (CreateMultipartUploadResponse resp = CreateMultipartUpload(cmu_args)) {
upload_id = resp.upload_id;
} else {
return ComposeObjectResponse(resp);
}
}
unsigned int part_number = 0;
utils::Multimap ssecheaders;
if (args.sse != nullptr) {
if (SseCustomerKey* ssec = dynamic_cast<SseCustomerKey*>(args.sse)) {
ssecheaders = ssec->Headers();
}
}
std::list<Part> parts;
for (auto& source : args.sources) {
size_t size = source.ObjectSize();
if (source.length.has_value()) {
size = *source.length;
} else if (source.offset.has_value()) {
size -= *source.offset;
}
size_t offset = 0;
if (source.offset.has_value()) offset = *source.offset;
utils::Multimap headers;
headers.AddAll(source.Headers());
headers.AddAll(ssecheaders);
if (size <= utils::kMaxPartSize) {
part_number++;
if (source.length.has_value()) {
headers.Add("x-amz-copy-source-range",
"bytes=" + std::to_string(offset) + "-" +
std::to_string(offset + *source.length - 1));
} else if (source.offset.has_value()) {
headers.Add("x-amz-copy-source-range",
"bytes=" + std::to_string(offset) + "-" +
std::to_string(offset + size - 1));
}
UploadPartCopyArgs upc_args;
upc_args.bucket = args.bucket;
upc_args.region = args.region;
upc_args.object = args.object;
upc_args.headers = headers;
upc_args.upload_id = upload_id;
upc_args.part_number = part_number;
UploadPartCopyResponse resp = UploadPartCopy(upc_args);
if (!resp) {
return ComposeObjectResponse(resp);
}
parts.push_back(Part(part_number, std::move(resp.etag)));
} else {
while (size > 0) {
part_number++;
size_t length = size;
if (length > utils::kMaxPartSize) length = utils::kMaxPartSize;
size_t end_bytes = offset + length - 1;
utils::Multimap headerscopy;
headerscopy.AddAll(headers);
headerscopy.Add("x-amz-copy-source-range",
"bytes=" + std::to_string(offset) + "-" +
std::to_string(end_bytes));
UploadPartCopyArgs upc_args;
upc_args.bucket = args.bucket;
upc_args.region = args.region;
upc_args.object = args.object;
upc_args.headers = headerscopy;
upc_args.upload_id = upload_id;
upc_args.part_number = part_number;
{
UploadPartCopyResponse resp = UploadPartCopy(upc_args);
if (!resp) {
return ComposeObjectResponse(resp);
}
parts.push_back(Part(part_number, std::move(resp.etag)));
}
offset += length;
size -= length;
}
}
}
CompleteMultipartUploadArgs cmu_args;
cmu_args.bucket = args.bucket;
cmu_args.region = args.region;
cmu_args.object = args.object;
cmu_args.upload_id = upload_id;
cmu_args.parts = parts;
return ComposeObjectResponse(CompleteMultipartUpload(cmu_args));
}
GetObjectResponse Client::GetObject(GetObjectArgs args) {
if (error::Error err = args.Validate()) {
return GetObjectResponse(err);
}
#ifdef MINIO_CPP_RDMA
if (args.buf != nullptr) {
std::string region;
if (GetRegionResponse resp = GetRegion(args.bucket, args.region)) {
region = resp.region;
} else {
return GetObjectResponse(resp);
}
const size_t size = *args.size;
// Process-wide cuObjClient — see client.h for the race rationale.
cuObjClient& rdma_client = SharedRDMAClient();
bool use_rdma = (rdma_client.cuMemObjGetDescriptor(args.buf, size) == 0);
if (use_rdma) {
s3_rdma_client_ctx getCtx = {
.provider = provider_,
.bucket = args.bucket,
.object = args.object,
.url = base_url_,
.region = region,
.op = CUOBJ_GET,
};
ssize_t ret = rdmaGetWithRetry(&rdma_client, &getCtx, args.buf, size);
rdma_client.cuMemObjPutDescriptor(args.buf);
if (ret > 0) {
GetObjectResponse resp;
resp.etag = getCtx.etag;
return resp;
}
// ret < 0 (retries exhausted) or kRDMANotSupported (server declined):
// fall through to HTTP-into-buffer path below.
}
// HTTP fallback: stream the body into the caller's buffer.
GetObjectArgs targs;
std::stringstream ss(std::ios_base::in | std::ios_base::out);
ss.rdbuf()->pubsetbuf(args.buf, size);
targs.bucket = args.bucket;
targs.object = args.object;
targs.region = region;
targs.datafunc = [&ss = ss](minio::http::DataFunctionArgs args) -> bool {
ss << args.datachunk;
return true;
};
return BaseClient::GetObject(targs);
}
#endif
return BaseClient::GetObject(args);
}
PutObjectResponse Client::PutObject(PutObjectArgs args, std::string& upload_id,
char* buf) {
utils::Multimap headers = args.Headers();
if (!headers.Contains("Content-Type")) {
if (args.content_type.empty()) {
headers.Add("Content-Type", "application/octet-stream");
} else {
headers.Add("Content-Type", args.content_type);
}
}
std::optional<uint64_t> object_size = args.object_size;
size_t part_size = args.part_size;
size_t uploaded_size = 0;
unsigned int part_number = 0;
std::string one_byte;
bool stop = false;
std::list<Part> parts;
std::optional<size_t> part_count = args.part_count;
double uploaded_bytes = 0; // for progress
std::optional<double> upload_speed; // for progress
auto read_part_data = [&](char* buf, size_t& bytes_read) -> error::Error {
if (part_count.has_value()) {
if (part_number == *part_count) {
part_size = *object_size - uploaded_size;
stop = true;
}
if (error::Error err =
utils::ReadPart(*args.stream, buf, part_size, bytes_read)) {
return err;
}
if (bytes_read != part_size) {
return error::Error("not enough data in the stream; expected: " +
std::to_string(part_size) +
", got: " + std::to_string(bytes_read) + " bytes");
}
} else {
char* b = buf;
size_t size = part_size + 1;
if (!one_byte.empty()) {
buf[0] = one_byte.front();
b = buf + 1;
size--;
bytes_read = 1;
one_byte = "";
}
size_t n = 0;
if (error::Error err = utils::ReadPart(*args.stream, b, size, n)) {
return err;
}
bytes_read += n;
// If bytes read is less than or equals to part size, then we have reached
// last part.
if (bytes_read <= part_size) {
part_count = std::optional<size_t>(part_number);
part_size = bytes_read;
stop = true;
} else {
one_byte = buf[part_size];
}
}
return error::Error();
};
while (!stop) {
part_number++;
size_t bytes_read = 0;
if (error::Error err = read_part_data(buf, bytes_read)) {
return PutObjectResponse(err);
}
std::string_view data(buf, part_size);
uploaded_size += part_size;
if (part_count.has_value() && *part_count == 1) {
PutObjectApiArgs api_args;
api_args.extra_query_params = args.extra_query_params;
api_args.bucket = args.bucket;
api_args.region = args.region;
api_args.object = args.object;
api_args.data = data;
api_args.buf = buf;
api_args.size = part_size;
api_args.progressfunc = args.progressfunc;
api_args.progress_userdata = args.progress_userdata;
api_args.headers = headers;
return BaseClient::PutObject(api_args);
}
if (upload_id.empty()) {
CreateMultipartUploadArgs cmu_args;
cmu_args.extra_query_params = args.extra_query_params;
cmu_args.bucket = args.bucket;
cmu_args.region = args.region;
cmu_args.object = args.object;
cmu_args.headers = headers;
#ifdef MINIO_CPP_RDMA
// Declare CRC64NVME so the server enforces per-part integrity on the
// RDMA UploadPart path (server returns 501 if checksum is missing when
// an algorithm was declared on Create).
cmu_args.headers.Add("x-amz-checksum-algorithm", "CRC64NVME");
#endif
if (CreateMultipartUploadResponse resp =
CreateMultipartUpload(cmu_args)) {
upload_id = resp.upload_id;
} else {
return PutObjectResponse(resp);
}
}
UploadPartArgs up_args;
up_args.bucket = args.bucket;
up_args.region = args.region;
up_args.object = args.object;
up_args.upload_id = upload_id;
up_args.part_number = part_number;
up_args.data = data;
up_args.buf = buf;
up_args.part_size = part_size;
#ifdef MINIO_CPP_RDMA
up_args.rdmaclient = args.rdmaclient;
if (buf != nullptr &&
cuObjClient::getMemoryType(buf) == CUOBJ_MEMORY_SYSTEM) {
const std::string crc = utils::Crc64NvmeBase64(buf, part_size);
up_args.checksum_crc64nvme = crc;
up_args.headers.Add("x-amz-checksum-crc64nvme", crc);
}
#endif
if (args.progressfunc != nullptr) {
up_args.progressfunc =
[&object_size = object_size, &uploaded_bytes = uploaded_bytes,
&upload_speed = upload_speed, &progressfunc = args.progressfunc,
&progress_userdata = args.progress_userdata](
http::ProgressFunctionArgs args) -> bool {
if (args.upload_speed > 0) {
if (!upload_speed.has_value()) {
upload_speed = args.upload_speed;
} else {
upload_speed = (*upload_speed + args.upload_speed) / 2.0;
}
return true;
}
http::ProgressFunctionArgs actual_args;
actual_args.upload_total_bytes =
object_size ? static_cast<double>(*object_size) : -1.0;
actual_args.uploaded_bytes = uploaded_bytes + args.uploaded_bytes;
actual_args.userdata = progress_userdata;
return progressfunc(actual_args);
};
}
// Propagate caller-supplied x-amz-content-sha256 (e.g. UNSIGNED-PAYLOAD
// for GPU-resident buffers) into each UploadPart so the per-part signing
// path also skips hashing the body.
if (headers.Contains("x-amz-content-sha256")) {
up_args.headers.Add("x-amz-content-sha256",
headers.GetFront("x-amz-content-sha256"));
}
if (args.sse != nullptr) {
if (SseCustomerKey* ssec = dynamic_cast<SseCustomerKey*>(args.sse)) {
up_args.headers.AddAll(ssec->Headers());
}
}
if (UploadPartResponse resp = UploadPart(up_args)) {
if (args.progressfunc != nullptr) {
uploaded_bytes += static_cast<double>(data.length());
http::ProgressFunctionArgs actual_args;
actual_args.upload_total_bytes =
object_size ? static_cast<double>(*object_size) : -1.0;
actual_args.uploaded_bytes = uploaded_bytes;
actual_args.userdata = args.progress_userdata;
if (!args.progressfunc(actual_args)) {
return UploadPartResponse(
error::Error("aborted by progress function"));
}
}
// HTTP fallback leaves resp.checksum_crc64nvme empty; use the local CRC.
parts.push_back(Part(part_number, std::move(resp.etag),
std::move(up_args.checksum_crc64nvme)));
} else {
return resp;
}
}
CompleteMultipartUploadArgs cmu_args;
cmu_args.bucket = args.bucket;
cmu_args.region = args.region;
cmu_args.object = args.object;
cmu_args.upload_id = upload_id;
cmu_args.parts = parts;
CompleteMultipartUploadResponse resp = CompleteMultipartUpload(cmu_args);
if (resp && args.progressfunc != nullptr) {
http::ProgressFunctionArgs actual_args;
actual_args.upload_speed = upload_speed.value_or(-1.0);
actual_args.userdata = args.progress_userdata;
// ignore the return value as we completed the upload
args.progressfunc(actual_args);
}
return PutObjectResponse(resp);
}
ComposeObjectResponse Client::ComposeObject(ComposeObjectArgs args) {
if (error::Error err = args.Validate()) {
return ComposeObjectResponse(err);
}
if (args.sse != nullptr && args.sse->TlsRequired() && !base_url_.https) {
return error::make<ComposeObjectResponse>(
"SSE operation must be performed over a secure connection");
}
std::string upload_id;
ComposeObjectResponse resp = ComposeObject(args, upload_id);
if (!resp && !upload_id.empty()) {
AbortMultipartUploadArgs amu_args;
amu_args.bucket = args.bucket;
amu_args.region = args.region;
amu_args.object = args.object;
amu_args.upload_id = upload_id;
AbortMultipartUpload(amu_args);
}
return resp;
}
CopyObjectResponse Client::CopyObject(CopyObjectArgs args) {
if (error::Error err = args.Validate()) {
return CopyObjectResponse(err);
}
if (args.sse != nullptr && args.sse->TlsRequired() && !base_url_.https) {
return error::make<CopyObjectResponse>(
"SSE operation must be performed over a secure connection");
}
if (args.source.ssec != nullptr && !base_url_.https) {
return error::make<CopyObjectResponse>(
"SSE-C operation must be performed over a secure connection");
}
std::string etag;
size_t size;
{
StatObjectResponse resp = StatObject(args.source);
if (!resp) {
return CopyObjectResponse(resp);
}
etag = resp.etag;
size = resp.size;
}
if (args.source.offset.has_value() || args.source.length.has_value() ||
size > utils::kMaxPartSize) {
if (args.metadata_directive != nullptr &&
*args.metadata_directive == Directive::kCopy) {
return error::make<CopyObjectResponse>(
"COPY metadata directive is not applicable to source object size "
"greater than 5 GiB");
}
if (args.tagging_directive != nullptr &&
*args.tagging_directive == Directive::kCopy) {
return error::make<CopyObjectResponse>(
"COPY tagging directive is not applicable to source object size "
"greater than 5 GiB");
}
ComposeSource src;
src.extra_headers = args.source.extra_headers;
src.extra_query_params = args.source.extra_query_params;
src.bucket = args.source.bucket;
src.region = args.source.region;
src.object = args.source.object;
src.ssec = args.source.ssec;
src.offset = args.source.offset;
src.length = args.source.length;
src.match_etag = args.source.match_etag;
src.not_match_etag = args.source.not_match_etag;
src.modified_since = args.source.modified_since;
src.unmodified_since = args.source.unmodified_since;
ComposeObjectArgs coargs;
coargs.extra_headers = args.extra_headers;
coargs.extra_query_params = args.extra_query_params;
coargs.bucket = args.bucket;
coargs.region = args.region;
coargs.object = args.object;
coargs.sse = args.sse;
coargs.sources.push_back(src);
return CopyObjectResponse(ComposeObject(coargs));
}
utils::Multimap headers;
headers.AddAll(args.extra_headers);
headers.AddAll(args.Headers());
if (args.metadata_directive != nullptr) {
headers.Add("x-amz-metadata-directive",
DirectiveToString(*args.metadata_directive));
}
if (args.tagging_directive != nullptr) {
headers.Add("x-amz-tagging-directive",
DirectiveToString(*args.tagging_directive));
}
headers.AddAll(args.source.CopyHeaders());
std::string region;
if (GetRegionResponse resp = GetRegion(args.bucket, args.region)) {
region = resp.region;
} else {
return CopyObjectResponse(resp);
}
Request req(http::Method::kPut, region, base_url_, args.extra_headers,
args.extra_query_params);
req.bucket_name = args.bucket;
req.object_name = args.object;
req.headers.AddAll(headers);
Response response = Execute(req);
if (!response) {
return CopyObjectResponse(response);
}
CopyObjectResponse resp;
resp.etag = utils::Trim(response.headers.GetFront("etag"), '"');
resp.version_id = response.headers.GetFront("x-amz-version-id");
return resp;
}
DownloadObjectResponse Client::DownloadObject(DownloadObjectArgs args) {
if (error::Error err = args.Validate()) {
return DownloadObjectResponse(err);
}
if (args.ssec != nullptr && !base_url_.https) {
return error::make<DownloadObjectResponse>(
"SSE-C operation must be performed over a secure connection");
}
std::string etag;
{
StatObjectArgs soargs;
soargs.bucket = args.bucket;
soargs.region = args.region;
soargs.object = args.object;
soargs.version_id = args.version_id;
soargs.ssec = args.ssec;
StatObjectResponse resp = StatObject(soargs);
if (!resp) {
return DownloadObjectResponse(resp);
}
etag = resp.etag;
}
std::string temp_filename =
args.filename + "." + curlpp::escape(etag) + ".part.minio";
std::ofstream fout(temp_filename, std::ios::trunc | std::ios::out);
if (!fout.is_open()) {
return error::make<DownloadObjectResponse>("unable to open file " +
temp_filename);
}
std::string region;
if (GetRegionResponse resp = GetRegion(args.bucket, args.region)) {
region = resp.region;
} else {
return DownloadObjectResponse(resp);
}
Request req(http::Method::kGet, region, base_url_, args.extra_headers,
args.extra_query_params);
req.bucket_name = args.bucket;
req.object_name = args.object;
if (!args.version_id.empty()) {
req.query_params.Add("versionId", args.version_id);
}
req.datafunc = [&fout = fout](http::DataFunctionArgs args) -> bool {
fout << args.datachunk;
return true;
};
req.progressfunc = args.progressfunc;
req.progress_userdata = args.progress_userdata;
Response response = Execute(req);
fout.close();
if (response) {
std::filesystem::rename(temp_filename, args.filename);
}
return DownloadObjectResponse(response);
}
ListObjectsResult Client::ListObjects(ListObjectsArgs args) {
if (error::Error err = args.Validate()) {