forked from minio/minio-dotnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ObjectOperations.cs
1166 lines (1022 loc) · 57 KB
/
ObjectOperations.cs
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 .NET Library for Amazon S3 Compatible Cloud Storage, (C) 2017 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.
*/
using System;
using System.Collections.Generic;
using RestSharp;
using System.Threading.Tasks;
using System.Linq;
using System.Reactive.Linq;
using Minio.DataModel;
using System.IO;
using System.Xml.Linq;
using System.Xml.Serialization;
using Minio.Exceptions;
using System.Globalization;
using Minio.Helper;
using System.Threading;
using System.Text;
using System.Xml;
namespace Minio
{
public partial class MinioClient : IObjectOperations
{
/// <summary>
/// Get an object. The object will be streamed to the callback given by the user.
/// </summary>
/// <param name="bucketName">Bucket to retrieve object from</param>
/// <param name="objectName">Name of object to retrieve</param>
/// <param name="cb">A stream will be passed to the callback</param>
/// <param name="sse">Server-side encryption option. Defaults to null.</param>
/// <param name="cancellationToken">Optional cancellation token to cancel the operation</param>
public async Task GetObjectAsync(string bucketName, string objectName, Action<Stream> cb, ServerSideEncryption sse = null, CancellationToken cancellationToken = default(CancellationToken))
{
await StatObjectAsync(bucketName, objectName, sse:sse, cancellationToken:cancellationToken).ConfigureAwait(false);
var headers = new Dictionary<string,string>();
if (sse != null && sse.GetType().Equals(EncryptionType.SSE_C))
{
sse.Marshal(headers);
}
var request = await this.CreateRequest(Method.GET,
bucketName,
objectName: objectName,
headerMap: headers)
.ConfigureAwait(false);
request.ResponseWriter = cb;
var response = await this.ExecuteTaskAsync(this.NoErrorHandlers, request, cancellationToken).ConfigureAwait(false);
}
/// <summary>
/// Get an object. The object will be streamed to the callback given by the user.
/// </summary>
/// <param name="bucketName">Bucket to retrieve object from</param>
/// <param name="objectName">Name of object to retrieve</param>
/// <param name="offset"> Offset of the object from where stream will start</param>
/// <param name="length">length of the object that will be read in the stream </param>
/// <param name="cb">A stream will be passed to the callback</param>
/// <param name="sse">Server-side encryption option. Defaults to null.</param>
/// <param name="cancellationToken">Optional cancellation token to cancel the operation</param>
public async Task GetObjectAsync(string bucketName, string objectName, long offset, long length, Action<Stream> cb, ServerSideEncryption sse = null, CancellationToken cancellationToken = default(CancellationToken))
{
if (offset < 0)
throw new ArgumentException("Offset should be zero or greater");
if (length < 0)
throw new ArgumentException("Length should be greater than zero");
await StatObjectAsync(bucketName, objectName, cancellationToken:cancellationToken).ConfigureAwait(false);
Dictionary<string, string> headerMap = new Dictionary<string, string>();
if (length > 0)
headerMap.Add("Range", "bytes=" + offset.ToString() + "-" + (offset + length - 1).ToString());
if (sse != null && sse.GetType().Equals(EncryptionType.SSE_C))
{
sse.Marshal(headerMap);
}
var request = await this.CreateRequest(Method.GET,
bucketName,
objectName: objectName,
headerMap: headerMap)
.ConfigureAwait(false);
request.ResponseWriter = cb;
var response = await this.ExecuteTaskAsync(this.NoErrorHandlers, request, cancellationToken).ConfigureAwait(false);
}
/// <summary>
/// Get an object. The object will be streamed to the callback given by the user.
/// </summary>
/// <param name="bucketName">Bucket to retrieve object from</param>
/// <param name="objectName">Name of object to retrieve</param>
/// <param name="fileName">string with file path</param>
/// <param name="sse">Server-side encryption option. Defaults to null.</param>
/// <param name="cancellationToken">Optional cancellation token to cancel the operation</param>
/// <returns></returns>
public async Task GetObjectAsync(string bucketName, string objectName, string fileName, ServerSideEncryption sse = null, CancellationToken cancellationToken = default(CancellationToken))
{
bool fileExists = File.Exists(fileName);
utils.ValidateFile(fileName);
ObjectStat objectStat = await StatObjectAsync(bucketName, objectName, sse:sse,cancellationToken:cancellationToken).ConfigureAwait(false);
long length = objectStat.Size;
string etag = objectStat.ETag;
string tempFileName = fileName + "." + etag + ".part.minio";
bool tempFileExists = File.Exists(tempFileName);
utils.ValidateFile(tempFileName);
FileInfo tempFileInfo = new FileInfo(tempFileName);
long tempFileSize = 0;
if (tempFileExists)
{
tempFileSize = tempFileInfo.Length;
if (tempFileSize > length)
{
File.Delete(tempFileName);
tempFileExists = false;
tempFileSize = 0;
}
}
if (fileExists)
{
FileInfo fileInfo = new FileInfo(fileName);
long fileSize = fileInfo.Length;
if (fileSize == length)
{
// already downloaded. nothing to do
return;
}
else if (fileSize > length)
{
throw new ArgumentException("'" + fileName + "': object size " + length + " is smaller than file size "
+ fileSize);
}
else if (!tempFileExists)
{
// before resuming the download, copy filename to tempfilename
File.Copy(fileName, tempFileName);
tempFileSize = fileSize;
tempFileExists = true;
}
}
await GetObjectAsync(bucketName, objectName, (stream) =>
{
var fileStream = File.Create(tempFileName);
stream.CopyTo(fileStream);
fileStream.Dispose();
FileInfo writtenInfo = new FileInfo(tempFileName);
long writtenSize = writtenInfo.Length;
if (writtenSize != length - tempFileSize)
{
throw new IOException(tempFileName + ": unexpected data written. expected = " + (length - tempFileSize)
+ ", written = " + writtenSize);
}
utils.MoveWithReplace(tempFileName, fileName);
}, sse, cancellationToken).ConfigureAwait(false);
}
/// <summary>
/// Creates an object from file
/// </summary>
/// <param name="bucketName">Bucket to create object in</param>
/// <param name="objectName">Key of the new object</param>
/// <param name="fileName">Path of file to upload</param>
/// <param name="contentType">Content type of the new object, null defaults to "application/octet-stream"</param>
/// <param name="metaData">Object metadata to be stored. Defaults to null.</param>
/// <param name="sse">Server-side encryption option. Defaults to null.</param>
/// <param name="cancellationToken">Optional cancellation token to cancel the operation</param>
public async Task PutObjectAsync(string bucketName, string objectName, string fileName, string contentType = null, Dictionary<string, string> metaData = null, ServerSideEncryption sse = null, CancellationToken cancellationToken = default(CancellationToken))
{
utils.ValidateFile(fileName, contentType);
FileInfo fileInfo = new FileInfo(fileName);
long size = fileInfo.Length;
using (FileStream file = new FileStream(fileName, FileMode.Open, FileAccess.Read))
{
await PutObjectAsync(bucketName, objectName, file, size, contentType, metaData,sse,cancellationToken).ConfigureAwait(false);
}
}
/// <summary>
/// Creates an object from inputstream
/// </summary>
/// <param name="bucketName">Bucket to create object in</param>
/// <param name="objectName">Key of the new object</param>
/// <param name="size">Total size of bytes to be written, must match with data's length</param>
/// <param name="contentType">Content type of the new object, null defaults to "application/octet-stream"</param>
/// <param name="data">Stream of bytes to send</param>
/// <param name="metaData">Object metadata to be stored. Defaults to null.</param>
/// <param name="sse">Server-side encryption option. Defaults to null.</param>
/// <param name="cancellationToken">Optional cancellation token to cancel the operation</param>
public async Task PutObjectAsync(string bucketName, string objectName, Stream data, long size, string contentType = null, Dictionary<string, string> metaData = null, ServerSideEncryption sse = null, CancellationToken cancellationToken = default(CancellationToken))
{
utils.validateBucketName(bucketName);
utils.validateObjectName(objectName);
var sseHeaders = new Dictionary<string,string>();
if (metaData == null)
{
metaData = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
}
else
{
metaData = new Dictionary<string, string>(metaData, StringComparer.OrdinalIgnoreCase);
}
if (sse != null)
{
sse.Marshal(sseHeaders);
}
if (string.IsNullOrWhiteSpace(contentType))
{
contentType = "application/octet-stream";
}
if (! metaData.ContainsKey("Content-Type"))
{
metaData["Content-Type"] = contentType;
}
if (data == null)
{
throw new ArgumentNullException("Invalid input stream,cannot be null");
}
// for sizes less than 5Mb , put a single object
if (size < Constants.MinimumPartSize && size >= 0)
{
var bytes = ReadFull(data, (int)size);
if (bytes != null && bytes.Length != (int)size)
{
throw new UnexpectedShortReadException("Data read " + bytes.Length + " is shorter than the size " + size + " of input buffer.");
}
await this.PutObjectAsync(bucketName, objectName, null, 0, bytes, metaData, sseHeaders, cancellationToken).ConfigureAwait(false);
return;
}
// For all sizes greater than 5MiB do multipart.
dynamic multiPartInfo = utils.CalculateMultiPartSize(size);
double partSize = multiPartInfo.partSize;
double partCount = multiPartInfo.partCount;
double lastPartSize = multiPartInfo.lastPartSize;
Part[] totalParts = new Part[(int)partCount];
string uploadId = await this.NewMultipartUploadAsync(bucketName, objectName, metaData, sseHeaders, cancellationToken).ConfigureAwait(false);
// Remove SSE-S3 and KMS headers during PutObjectPart operations.
if (sse != null &&
(sse.GetType().Equals(EncryptionType.SSE_S3) ||
sse.GetType().Equals(EncryptionType.SSE_KMS)))
{
sseHeaders.Remove(Constants.SSEGenericHeader);
sseHeaders.Remove(Constants.SSEKMSContext);
sseHeaders.Remove(Constants.SSEKMSKeyId);
}
double expectedReadSize = partSize;
int partNumber;
int numPartsUploaded = 0;
for (partNumber = 1; partNumber <= partCount; partNumber++)
{
byte[] dataToCopy = ReadFull(data, (int)partSize);
if (dataToCopy == null && numPartsUploaded > 0)
{
break;
}
if (partNumber == partCount)
{
expectedReadSize = lastPartSize;
}
numPartsUploaded += 1;
string etag = await this.PutObjectAsync(bucketName, objectName, uploadId, partNumber, dataToCopy, metaData, sseHeaders, cancellationToken).ConfigureAwait(false);
totalParts[partNumber - 1] = new Part() { PartNumber = partNumber, ETag = etag, size = (long)expectedReadSize };
}
// This shouldn't happen where stream size is known.
if (partCount != numPartsUploaded && size != -1)
{
await this.RemoveUploadAsync(bucketName, objectName, uploadId, cancellationToken).ConfigureAwait(false);
return;
}
Dictionary<int, string> etags = new Dictionary<int, string>();
for (partNumber = 1; partNumber <= numPartsUploaded; partNumber++)
{
etags[partNumber] = totalParts[partNumber - 1].ETag;
}
await this.CompleteMultipartUploadAsync(bucketName, objectName, uploadId, etags, cancellationToken).ConfigureAwait(false);
}
/// <summary>
/// Internal method to complete multi part upload of object to server.
/// </summary>
/// <param name="bucketName">Bucket Name</param>
/// <param name="objectName">Object to be uploaded</param>
/// <param name="uploadId">Upload Id</param>
/// <param name="etags">Etags</param>
/// <param name="cancellationToken">Optional cancellation token to cancel the operation</param>
/// <returns></returns>
private async Task CompleteMultipartUploadAsync(string bucketName, string objectName, string uploadId, Dictionary<int, string> etags, CancellationToken cancellationToken)
{
string resourcePath = "?uploadId=" + uploadId;
var request = await this.CreateRequest(Method.POST, bucketName,
objectName: objectName,
resourcePath: resourcePath)
.ConfigureAwait(false);
List<XElement> parts = new List<XElement>();
for (int i = 1; i <= etags.Count; i++)
{
parts.Add(new XElement("Part",
new XElement("PartNumber", i),
new XElement("ETag", etags[i])));
}
var completeMultipartUploadXml = new XElement("CompleteMultipartUpload", parts);
var bodyString = completeMultipartUploadXml.ToString();
var body = System.Text.Encoding.UTF8.GetBytes(bodyString);
request.AddParameter("application/xml", body, RestSharp.ParameterType.RequestBody);
var response = await this.ExecuteTaskAsync(this.NoErrorHandlers, request, cancellationToken).ConfigureAwait(false);
}
/// <summary>
/// Returns an async observable of parts corresponding to a uploadId for a specific bucket and objectName
/// </summary>
/// <param name="bucketName"></param>
/// <param name="objectName"></param>
/// <param name="uploadId"></param>
/// <param name="cancellationToken">Optional cancellation token to cancel the operation</param>
/// <returns></returns>
private IObservable<Part> ListParts(string bucketName, string objectName, string uploadId, CancellationToken cancellationToken)
{
return Observable.Create<Part>(
async obs =>
{
int nextPartNumberMarker = 0;
bool isRunning = true;
while (isRunning)
{
var uploads = await this.GetListPartsAsync(bucketName, objectName, uploadId, nextPartNumberMarker, cancellationToken).ConfigureAwait(false);
foreach (Part part in uploads.Item2)
{
obs.OnNext(part);
}
nextPartNumberMarker = uploads.Item1.NextPartNumberMarker;
isRunning = uploads.Item1.IsTruncated;
}
});
}
/// <summary>
/// Gets the list of parts corresponding to a uploadId for given bucket and object
/// </summary>
/// <param name="bucketName"></param>
/// <param name="objectName"></param>
/// <param name="uploadId"></param>
/// <param name="partNumberMarker"></param>
/// <param name="cancellationToken">Optional cancellation token to cancel the operation</param>
/// <returns></returns>
private async Task<Tuple<ListPartsResult, List<Part>>> GetListPartsAsync(string bucketName, string objectName, string uploadId, int partNumberMarker, CancellationToken cancellationToken)
{
var resourcePath = "?uploadId=" + uploadId;
if (partNumberMarker > 0)
{
resourcePath += "&part-number-marker=" + partNumberMarker;
}
resourcePath += "&max-parts=1000";
var request = await this.CreateRequest(Method.GET, bucketName,
objectName: objectName,
resourcePath: resourcePath)
.ConfigureAwait(false);
var response = await this.ExecuteTaskAsync(this.NoErrorHandlers, request, cancellationToken).ConfigureAwait(false);
var contentBytes = System.Text.Encoding.UTF8.GetBytes(response.Content);
ListPartsResult listPartsResult = null;
using (var stream = new MemoryStream(contentBytes))
listPartsResult = (ListPartsResult)(new XmlSerializer(typeof(ListPartsResult)).Deserialize(stream));
XDocument root = XDocument.Parse(response.Content);
var uploads = (from c in root.Root.Descendants("{http://s3.amazonaws.com/doc/2006-03-01/}Part")
select new Part()
{
PartNumber = int.Parse(c.Element("{http://s3.amazonaws.com/doc/2006-03-01/}PartNumber").Value, CultureInfo.CurrentCulture),
ETag = c.Element("{http://s3.amazonaws.com/doc/2006-03-01/}ETag").Value.Replace("\"", ""),
size = long.Parse(c.Element("{http://s3.amazonaws.com/doc/2006-03-01/}Size").Value, CultureInfo.CurrentCulture)
});
return new Tuple<ListPartsResult, List<Part>>(listPartsResult, uploads.ToList());
}
/// <summary>
/// Start a new multi-part upload request
/// </summary>
/// <param name="bucketName"></param>
/// <param name="objectName"></param>
/// <param name="metaData"></param>
/// <param name="sseHeaders"> Server-side encryption options</param>
/// <param name="cancellationToken">Optional cancellation token to cancel the operation</param>
/// <returns></returns>
private async Task<string> NewMultipartUploadAsync(string bucketName, string objectName, Dictionary<string,string> metaData ,Dictionary<string,string> sseHeaders, CancellationToken cancellationToken = default(CancellationToken))
{
var resource = "?uploads";
foreach(KeyValuePair<string,string> kv in sseHeaders)
{
metaData.Add(kv.Key, kv.Value);
}
var request = await this.CreateRequest(Method.POST, bucketName, objectName: objectName,
headerMap: metaData, resourcePath: resource).ConfigureAwait(false);
var response = await this.ExecuteTaskAsync(this.NoErrorHandlers, request, cancellationToken).ConfigureAwait(false);
var contentBytes = System.Text.Encoding.UTF8.GetBytes(response.Content);
InitiateMultipartUploadResult newUpload = null;
using (var stream = new MemoryStream(contentBytes))
newUpload = (InitiateMultipartUploadResult)(new XmlSerializer(typeof(InitiateMultipartUploadResult)).Deserialize(stream));
return newUpload.UploadId;
}
/// <summary>
/// Upload object part to bucket for particular uploadId
/// </summary>
/// <param name="bucketName"></param>
/// <param name="objectName"></param>
/// <param name="uploadId"></param>
/// <param name="partNumber"></param>
/// <param name="data"></param>
/// <param name="metaData"></param>
/// <param name="sseHeaders">Server-side encryption headers if any </param>
/// <param name="cancellationToken">Optional cancellation token to cancel the operation</param>
/// <returns></returns>
private async Task<string> PutObjectAsync(string bucketName, string objectName, string uploadId, int partNumber, byte[] data, Dictionary<string,string> metaData, Dictionary<string,string> sseHeaders, CancellationToken cancellationToken)
{
var resource = "";
if (!string.IsNullOrEmpty(uploadId) && partNumber > 0)
{
resource += "?uploadId=" + uploadId + "&partNumber=" + partNumber;
}
// For multi-part upload requests, metadata needs to be passed in the NewMultiPartUpload request
string contentType = metaData["Content-Type"];
if (uploadId != null)
{
metaData = new Dictionary<string,string>();
}
foreach (KeyValuePair<string,string> kv in sseHeaders)
{
metaData.Add(kv.Key,kv.Value);
}
var request = await this.CreateRequest(Method.PUT, bucketName,
objectName: objectName,
contentType: contentType,
headerMap: metaData,
body: data,
resourcePath: resource)
.ConfigureAwait(false);
var response = await this.ExecuteTaskAsync(this.NoErrorHandlers, request, cancellationToken).ConfigureAwait(false);
string etag = null;
foreach (Parameter parameter in response.Headers)
{
if (parameter.Name.Equals("ETag",StringComparison.OrdinalIgnoreCase))
{
etag = parameter.Value.ToString();
}
}
return etag;
}
/// <summary>
/// Get list of multi-part uploads matching particular uploadIdMarker
/// </summary>
/// <param name="bucketName">bucketName</param>
/// <param name="prefix">prefix</param>
/// <param name="keyMarker"></param>
/// <param name="uploadIdMarker"></param>
/// <param name="delimiter"></param>
/// <param name="cancellationToken">Optional cancellation token to cancel the operation</param>
/// <returns></returns>
private async Task<Tuple<ListMultipartUploadsResult, List<Upload>>> GetMultipartUploadsListAsync(string bucketName,
string prefix,
string keyMarker,
string uploadIdMarker,
string delimiter,
CancellationToken cancellationToken)
{
var queries = new List<string>();
queries.Add("uploads");
queries.Add("prefix=" + Uri.EscapeDataString(prefix));
queries.Add("delimiter=" + Uri.EscapeDataString(delimiter));
queries.Add("max-uploads=1000");
if (keyMarker != null)
{
queries.Add("key-marker=" + Uri.EscapeDataString(keyMarker));
}
if (uploadIdMarker != null)
{
queries.Add("upload-id-marker=" + uploadIdMarker);
}
string query = string.Join("&", queries);
var request = await this.CreateRequest(Method.GET, bucketName, resourcePath: "?" + query).ConfigureAwait(false);
var response = await this.ExecuteTaskAsync(this.NoErrorHandlers, request, cancellationToken).ConfigureAwait(false);
var contentBytes = System.Text.Encoding.UTF8.GetBytes(response.Content);
ListMultipartUploadsResult listBucketResult = null;
using (var stream = new MemoryStream(contentBytes))
listBucketResult = (ListMultipartUploadsResult)(new XmlSerializer(typeof(ListMultipartUploadsResult)).Deserialize(stream));
XDocument root = XDocument.Parse(response.Content);
var uploads = (from c in root.Root.Descendants("{http://s3.amazonaws.com/doc/2006-03-01/}Upload")
select new Upload()
{
Key = c.Element("{http://s3.amazonaws.com/doc/2006-03-01/}Key").Value,
UploadId = c.Element("{http://s3.amazonaws.com/doc/2006-03-01/}UploadId").Value,
Initiated = c.Element("{http://s3.amazonaws.com/doc/2006-03-01/}Initiated").Value
});
return new Tuple<ListMultipartUploadsResult, List<Upload>>(listBucketResult, uploads.ToList());
}
/// <summary>
/// Lists all incomplete uploads in a given bucket and prefix recursively
/// </summary>
/// <param name="bucketName">Bucket to list all incomplepte uploads from</param>
/// <param name="prefix">Filter all incomplete uploads starting with this prefix</param>
/// <param name="recursive">Set to true to recursively list all incomplete uploads</param>
/// <param name="cancellationToken">Optional cancellation token to cancel the operation</param>
/// <returns>A lazily populated list of incomplete uploads</returns>
public IObservable<Upload> ListIncompleteUploads(string bucketName, string prefix = "", bool recursive = true, CancellationToken cancellationToken = default(CancellationToken))
{
if (recursive)
{
return this.listIncompleteUploads(bucketName, prefix, "", cancellationToken);
}
return this.listIncompleteUploads(bucketName, prefix, "/", cancellationToken);
}
/// <summary>
/// Lists all or delimited incomplete uploads in a given bucket with a given objectName
/// </summary>
/// <param name="bucketName">Bucket to list incomplete uploads from</param>
/// <param name="prefix">Key of object to list incomplete uploads from</param>
/// <param name="delimiter">delimiter of object to list incomplete uploads</param>
/// <param name="cancellationToken">Optional cancellation token to cancel the operation</param>
/// <returns>Observable that notifies when next next upload becomes available</returns>
private IObservable<Upload> listIncompleteUploads(string bucketName, string prefix, string delimiter, CancellationToken cancellationToken)
{
return Observable.Create<Upload>(
async obs =>
{
string nextKeyMarker = null;
string nextUploadIdMarker = null;
bool isRunning = true;
while (isRunning)
{
var uploads = await this.GetMultipartUploadsListAsync(bucketName, prefix, nextKeyMarker, nextUploadIdMarker, delimiter, cancellationToken).ConfigureAwait(false);
foreach (Upload upload in uploads.Item2)
{
obs.OnNext(upload);
}
nextKeyMarker = uploads.Item1.NextKeyMarker;
nextUploadIdMarker = uploads.Item1.NextUploadIdMarker;
isRunning = uploads.Item1.IsTruncated;
}
});
}
/// <summary>
/// Remove incomplete uploads from a given bucket and objectName
/// </summary>
/// <param name="bucketName">Bucket to remove incomplete uploads from</param>
/// <param name="objectName">Key to remove incomplete uploads from</param>
/// <param name="cancellationToken">Optional cancellation token to cancel the operation</param>
/// <returns></returns>
public async Task RemoveIncompleteUploadAsync(string bucketName, string objectName, CancellationToken cancellationToken = default(CancellationToken))
{
var uploads = await this.ListIncompleteUploads(bucketName, objectName, cancellationToken: cancellationToken).ToArray();
foreach (Upload upload in uploads)
{
if (objectName == upload.Key)
{
await this.RemoveUploadAsync(bucketName, objectName, upload.UploadId, cancellationToken).ConfigureAwait(false);
}
}
}
/// <summary>
/// Remove object with matching uploadId from bucket
/// </summary>
/// <param name="bucketName"></param>
/// <param name="objectName"></param>
/// <param name="uploadId"></param>
/// <param name="cancellationToken">Optional cancellation token to cancel the operation</param>
/// <returns></returns>
private async Task RemoveUploadAsync(string bucketName, string objectName, string uploadId, CancellationToken cancellationToken)
{
// var resourcePath = "/" + utils.UrlEncode(objectName) + "?uploadId=" + uploadId;
var resourcePath = "?uploadId=" + uploadId;
var request = await this.CreateRequest(Method.DELETE, bucketName,
objectName: objectName,
resourcePath: resourcePath)
.ConfigureAwait(false);
var response = await this.ExecuteTaskAsync(this.NoErrorHandlers, request, cancellationToken).ConfigureAwait(false);
}
/// <summary>
/// Removes an object with given name in specific bucket
/// </summary>
/// <param name="bucketName">Bucket to remove object from</param>
/// <param name="objectName">Key of object to remove</param>
/// <param name="cancellationToken">Optional cancellation token to cancel the operation</param>
/// <returns></returns>
public async Task RemoveObjectAsync(string bucketName, string objectName, CancellationToken cancellationToken = default(CancellationToken))
{
var request = await this.CreateRequest(Method.DELETE, bucketName, objectName: objectName).ConfigureAwait(false);
var response = await this.ExecuteTaskAsync(this.NoErrorHandlers, request, cancellationToken).ConfigureAwait(false);
}
/// <summary>
/// private helper method to remove list of objects from bucket
/// </summary>
/// <param name="bucketName"></param>
/// <param name="objectsList"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
private async Task<List<DeleteError>> removeObjectsAsync(string bucketName, List<DeleteObject> objectsList, CancellationToken cancellationToken)
{
string resource = "?delete";
var request = await this.CreateRequest(Method.POST, bucketName, resourcePath: resource).ConfigureAwait(false);
List<XElement> objects = new List<XElement>();
foreach (var obj in objectsList)
{
objects.Add(new XElement("Object",
new XElement("Key", obj.Key)));
}
var deleteObjectsRequest = new XElement("DeleteObject", objects,
new XElement("Quiet",true));
var bodyString = deleteObjectsRequest.ToString();
var body = System.Text.Encoding.UTF8.GetBytes(bodyString);
request.AddParameter("application/xml", body, RestSharp.ParameterType.RequestBody);
var response = await this.ExecuteTaskAsync(this.NoErrorHandlers, request, cancellationToken).ConfigureAwait(false);
var contentBytes = System.Text.Encoding.UTF8.GetBytes(response.Content);
DeleteObjectsResult deleteResult = null;
using (var stream = new MemoryStream(contentBytes))
deleteResult = (DeleteObjectsResult)(new XmlSerializer(typeof(DeleteObjectsResult)).Deserialize(stream));
if (deleteResult == null )
{
return new List<DeleteError>();
}
return deleteResult.ErrorList();
}
/// <summary>
/// Removes multiple objects from a specific bucket
/// </summary>
/// <param name="bucketName">Bucket to remove objects from</param>
/// <param name="objectNames">List of object keys to remove.</param>
/// <param name="cancellationToken">Optional cancellation token to cancel the operation</param>
/// <returns></returns>
public async Task<IObservable<DeleteError>> RemoveObjectAsync(string bucketName, IEnumerable<string> objectNames, CancellationToken cancellationToken = default(CancellationToken))
{
if (objectNames == null)
{
return null;
}
utils.validateBucketName(bucketName);
List<DeleteObject> objectList;
return Observable.Create<DeleteError>(
async obs =>
{
bool process = true;
int count = objectNames.Count();
int i = 0;
while (process)
{
objectList = new List<DeleteObject>();
while (i < count)
{
string objectName = objectNames.ElementAt(i);
utils.validateObjectName(objectName);
objectList.Add(new DeleteObject(objectName));
i++;
if (i % 1000 == 0)
break;
}
if (objectList.Count() > 0)
{
var errorsList = await removeObjectsAsync(bucketName, objectList, cancellationToken).ConfigureAwait(false);
foreach (DeleteError error in errorsList)
{
obs.OnNext(error);
}
}
if (i >= objectNames.Count())
process = !process;
}
});
}
/// <summary>
/// Tests the object's existence and returns metadata about existing objects.
/// </summary>
/// <param name="bucketName">Bucket to test object in</param>
/// <param name="objectName">Name of the object to stat</param>
/// <param name="sse"> Server-side encryption option.Defaults to null</param>
/// <param name="cancellationToken">Optional cancellation token to cancel the operation</param>
/// <returns>Facts about the object</returns>
public async Task<ObjectStat> StatObjectAsync(string bucketName, string objectName, ServerSideEncryption sse = null, CancellationToken cancellationToken = default(CancellationToken))
{
var headerMap = new Dictionary<string,string>();
if (sse != null && sse.GetType().Equals(EncryptionType.SSE_C))
{
sse.Marshal(headerMap);
}
var request = await this.CreateRequest(Method.HEAD, bucketName, objectName: objectName, headerMap: headerMap).ConfigureAwait(false);
var response = await this.ExecuteTaskAsync(this.NoErrorHandlers, request, cancellationToken).ConfigureAwait(false);
// Extract stats from response
long size = 0;
DateTime lastModified = new DateTime();
string etag = "";
string contentType = null;
Dictionary<string,string> metaData = new Dictionary<string, string>();
//Supported headers for object.
List<string> supportedHeaders = new List<string> { "cache-control", "content-encoding", "content-type" };
foreach (Parameter parameter in response.Headers)
{
if (parameter.Name.Equals("Content-Length", StringComparison.OrdinalIgnoreCase))
{
size = long.Parse(parameter.Value.ToString());
}
else if (parameter.Name.Equals("Last-Modified", StringComparison.OrdinalIgnoreCase))
{
lastModified = DateTime.Parse(parameter.Value.ToString());
}
else if (parameter.Name.Equals("ETag", StringComparison.OrdinalIgnoreCase))
{
etag = parameter.Value.ToString().Replace("\"", "");
}
else if (parameter.Name.Equals("Content-Type", StringComparison.OrdinalIgnoreCase))
{
contentType = parameter.Value.ToString();
metaData["Content-Type"] = contentType;
}
else if (supportedHeaders.Contains(parameter.Name.ToLower()) || parameter.Name.ToLower().StartsWith("x-amz-meta-"))
{
metaData[parameter.Name] = parameter.Value.ToString();
}
}
return new ObjectStat(objectName, size, lastModified, etag, contentType, metaData);
}
/// <summary>
/// Advances in the stream upto currentPartSize or End of Stream
/// </summary>
/// <param name="data"></param>
/// <param name="currentPartSize"></param>
/// <returns>bytes read in a byte array</returns>
internal byte[] ReadFull(Stream data, int currentPartSize)
{
byte[] result = new byte[currentPartSize];
int totalRead = 0;
while (totalRead < currentPartSize)
{
byte[] curData = new byte[currentPartSize - totalRead];
int curRead = data.Read(curData, 0, currentPartSize - totalRead);
if (curRead == 0)
{
break;
}
for (int i = 0; i < curRead; i++)
{
result[totalRead + i] = curData[i];
}
totalRead += curRead;
}
if (totalRead == 0) return null;
if (totalRead == currentPartSize) return result;
byte[] truncatedResult = new byte[totalRead];
for (int i = 0; i < totalRead; i++)
{
truncatedResult[i] = result[i];
}
return truncatedResult;
}
/// <summary>
/// Copy a source object into a new destination object.
/// </summary>
/// <param name="bucketName"> Bucket name where the object to be copied exists.</param>
/// <param name="objectName">Object name source to be copied.</param>
/// <param name="destBucketName">Bucket name where the object will be copied to.</param>
/// <param name="destObjectName">Object name to be created, if not provided uses source object name as destination object name.</param>
/// <param name="copyConditions">optionally can take a key value CopyConditions as well for conditionally attempting copyObject.</param>
/// <param name="metadata">Optional Object metadata to be stored. Defaults to null.</param>
/// <param name="sseSrc"> Optional source encryption options.Defaults to null. </param>
/// <param name="sseDest"> Optional destination encryption options.Defaults to null. </param>
/// <param name="cancellationToken">Optional cancellation token to cancel the operation</param>
/// <returns></returns>
public async Task CopyObjectAsync(string bucketName, string objectName, string destBucketName, string destObjectName = null, CopyConditions copyConditions = null, Dictionary<string, string> metadata = null, ServerSideEncryption sseSrc = null, ServerSideEncryption sseDest = null, CancellationToken cancellationToken = default(CancellationToken))
{
if (bucketName == null)
{
throw new ArgumentException("Source bucket name cannot be empty");
}
if (objectName == null)
{
throw new ArgumentException("Source object name cannot be empty");
}
if (destBucketName == null)
{
throw new ArgumentException("Destination bucket name cannot be empty");
}
// Escape source object path.
string sourceObjectPath = bucketName + "/" + utils.UrlEncode(objectName);
// Destination object name is optional, if empty default to source object name.
if (destObjectName == null)
{
destObjectName = objectName;
}
ServerSideEncryption sseGet = sseSrc;
if (sseSrc is SSECopy)
{
SSECopy sseCpy = (SSECopy)sseSrc;
sseGet = sseCpy.CloneToSSEC();
}
// Get Stats on the source object
ObjectStat srcStats = await this.StatObjectAsync(bucketName, objectName, sse:sseGet, cancellationToken:cancellationToken).ConfigureAwait(false);
// Copy metadata from the source object if no metadata replace directive
Dictionary<string,string> meta = new Dictionary<string,string>();
Dictionary<string,string> m = metadata;
if (copyConditions != null && !copyConditions.HasReplaceMetadataDirective())
{
m = srcStats.metaData;
}
if (m != null)
{
foreach (var item in m)
{
meta[item.Key] = item.Value;
}
}
long srcByteRangeSize = 0L;
if (copyConditions != null)
{
srcByteRangeSize = copyConditions.GetByteRange();
}
long copySize = (srcByteRangeSize == 0) ? srcStats.Size : srcByteRangeSize;
if ((srcByteRangeSize > srcStats.Size) ||
((srcByteRangeSize > 0) && (copyConditions.byteRangeEnd >= srcStats.Size)))
throw new ArgumentException("Specified byte range (" + copyConditions.byteRangeStart.ToString() + "-" + copyConditions.byteRangeEnd.ToString() + ") does not fit within source object (size=" + srcStats.Size.ToString() + ")");
if ((copySize > Constants.MaxSingleCopyObjectSize) ||
(srcByteRangeSize > 0 && (srcByteRangeSize != srcStats.Size)))
await MultipartCopyUploadAsync(bucketName, objectName, destBucketName, destObjectName, copyConditions, copySize, meta,sseSrc, sseDest, cancellationToken).ConfigureAwait(false);
else
{
if (sseSrc != null && sseSrc is SSECopy)
{
sseSrc.Marshal(meta);
}
if (sseDest != null)
{
sseDest.Marshal(meta);
}
await this.CopyObjectRequestAsync(bucketName, objectName, destBucketName, destObjectName, copyConditions, meta, null, cancellationToken, typeof(CopyObjectResult)).ConfigureAwait(false);
}
}
/// <summary>
/// Create the copy request,execute it and
/// </summary>
/// <param name="bucketName"> Bucket name where the object to be copied exists.</param>
/// <param name="objectName">Object name source to be copied.</param>
/// <param name="destBucketName">Bucket name where the object will be copied to.</param>
/// <param name="destObjectName">Object name to be created, if not provided uses source object name as destination object name.</param>
/// <param name="copyConditions">optionally can take a key value CopyConditions as well for conditionally attempting copyObject.</param>
/// <param name="customHeaders">optional custom header to specify byte range</param>
/// <param name="resource"> optional string to specify upload id and part number </param>
/// <param name="type"> type of XML serialization to be applied on the server response</param>
/// <param name="cancellationToken">Optional cancellation token to cancel the operation</param>
/// <returns></returns>
private async Task<object> CopyObjectRequestAsync(string bucketName, string objectName, string destBucketName, string destObjectName, CopyConditions copyConditions, Dictionary<string, string> customHeaders, string resource, CancellationToken cancellationToken, Type type)
{
// Escape source object path.
string sourceObjectPath = bucketName + "/" + utils.UrlEncode(objectName);
// Destination object name is optional, if empty default to source object name.
if (destObjectName == null)
{
destObjectName = objectName;
}
var request = await this.CreateRequest(Method.PUT, destBucketName,
objectName: destObjectName,
resourcePath: resource,
headerMap: customHeaders)
.ConfigureAwait(false);
// Set the object source
request.AddHeader("x-amz-copy-source", sourceObjectPath);
// If no conditions available, skip addition else add the conditions to the header
if (copyConditions != null)
{
foreach (var item in copyConditions.GetConditions())
{
request.AddHeader(item.Key, item.Value);
}
}
var response = await this.ExecuteTaskAsync(this.NoErrorHandlers, request, cancellationToken).ConfigureAwait(false);
// Just read the result and parse content.
var contentBytes = System.Text.Encoding.UTF8.GetBytes(response.Content);
object copyResult = null;
using (var stream = new MemoryStream(contentBytes))
{
if (type == typeof(CopyObjectResult))
copyResult = (CopyObjectResult)(new XmlSerializer(typeof(CopyObjectResult)).Deserialize(stream));
if (type == typeof(CopyPartResult))
copyResult = (CopyPartResult)(new XmlSerializer(typeof(CopyPartResult)).Deserialize(stream));
}
return copyResult;
}
/// <summary>