-
Notifications
You must be signed in to change notification settings - Fork 186
/
Quick.Azure.pas
941 lines (875 loc) · 28.9 KB
/
Quick.Azure.pas
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
{ ***************************************************************************
Copyright (c) 2015-2023 Kike Pérez
Unit : Quick.Azure
Description : Azure blobs operations
Author : Kike Pérez
Version : 1.4
Created : 27/08/2015
Modified : 14/07/2023
This file is part of QuickLib: https://github.com/exilon/QuickLib
***************************************************************************
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.
*************************************************************************** }
unit Quick.Azure;
{$i QuickLib.inc}
interface
uses
Classes,
System.SysUtils,
System.Generics.Collections,
IPPeerClient,
IdURI,
Data.Cloud.CloudAPI,
Data.Cloud.AzureAPI,
Quick.Commons;
type
TAzureProtocol = (azHTTP,azHTTPS);
//TAzureBlob = Data.Cloud.AzureAPI.TAzureBlob;
TBlobPublicAccess = Data.Cloud.AzureAPI.TBlobPublicAccess;
TAzureResponseInfo = record
StatusCode : Integer;
StatusMsg : string;
end;
TAzureBlobObject = class
Name : string;
Size : Int64;
IsDir : Boolean;
LastModified : TDateTime;
end;
TBlobList = class (TObjectList<TAzureBlobObject>);
TQuickAzure = class
private
fconAzure : TAzureConnectionInfo;
fAccountName : string;
fAccountKey : string;
fAzureProtocol : TAzureProtocol;
fTimeOut : Integer;
procedure SetAccountName(azAccountName : string);
procedure SetAccountKey(azAccountKey : string);
procedure SetAzureProtocol(azProtocol : TAzureProtocol);
function FileToArray(cFilename : string) : TArray<Byte>;
function StreamToArray(cStream : TStream) : TArray<Byte>;
function ByteContent(DataStream: TStream): TBytes;
function GMT2DateTime(const gmtdate : string):TDateTime;
function CheckContainer(const aContainer : string) : string;
function RemoveFirstSlash(const aValue : string) : string;
public
constructor Create; overload;
constructor Create(azAccountName, azAccountKey : string); overload;
destructor Destroy; override;
property AccountName : string read fAccountName write SetAccountName;
property AccountKey : string read fAccountKey write SetAccountKey;
property AzureProtocol : TAzureProtocol read fAzureProtocol write SetAzureProtocol;
property TimeOut : Integer read fTimeOut write fTimeOut;
function PutBlob(const azContainer, cFilename, azBlobName : string; out azResponseInfo : TAzureResponseInfo) : Boolean; overload;
function PutBlob(const azContainer : string; cStream : TStream; const azBlobName : string; out azResponseInfo : TAzureResponseInfo) : Boolean; overload;
function GetBlob(const azContainer, azBlobName, cFilenameTo : string; out azResponseInfo : TAzureResponseInfo) : Boolean; overload;
function GetBlob(const azContainer, azBlobName : string; out azResponseInfo : TAzureResponseInfo; out Stream : TMemoryStream) : Boolean; overload;
function GetBlob(const azContainer, azBlobName: string; out azResponseInfo: TAzureResponseInfo; var Stream: TStream): Boolean; overload;
function GetBlob(const azContainer, azBlobName : string; out azResponseInfo : TAzureResponseInfo) : TMemoryStream; overload;
function CopyBlob(const azSourceContainer, azSourceBlobName : string; azTargetContainer, azTargetBlobName : string; out azResponseInfo : TAzureResponseInfo) : Boolean;
function RenameBlob(const azContainer, azSourceBlobName, azTargetBlobName : string; out azResponseInfo : TAzureResponseInfo) : Boolean;
function ExistsObject(const azContainer, azBlobName : string) : Boolean;
function ExistsFolder(const azContainer, azFolderName : string) : Boolean;
function DeleteBlob(const azContainer,azBlobName : string; out azResponseInfo : TAzureResponseInfo) : Boolean;
function ListBlobs(const azContainer, azBlobsStartWith : string; Recursive : Boolean; out azResponseInfo : TAzureResponseInfo) : TBlobList;
function ListBlobsNames(const azContainer, azBlobsStartWith : string; Recursive : Boolean; out azResponseInfo : TAzureResponseInfo) : TStrings;
function ExistsContainer(const azContainer : string) : Boolean;
function ListContainers(const azContainersStartWith : string; out azResponseInfo : TAzureResponseInfo) : TStrings;
function CreateContainer(const azContainer : string; azPublicAccess : TBlobPublicAccess; out azResponseInfo : TAzureResponseInfo) : Boolean;
function DeleteContainer(const azContainer : string; out azResponseInfo : TAzureResponseInfo) : Boolean;
end;
implementation
constructor TQuickAzure.Create;
begin
inherited;
fconAzure := TAzureConnectionInfo.Create(nil);
fAzureProtocol := azHTTP;
fTimeOut := 30;
end;
constructor TQuickAzure.Create(azAccountName, azAccountKey : string);
begin
Create;
SetAccountName(azAccountName);
SetAccountKey(azAccountKey);
end;
destructor TQuickAzure.Destroy;
begin
if Assigned(fconAzure) then fconAzure.Free;
inherited;
end;
procedure TQuickAzure.SetAccountName(azAccountName : string);
begin
if fAccountName <> azAccountName then
begin
fAccountName := azAccountName;
fconAzure.AccountName := azAccountName;
end;
end;
procedure TQuickAzure.SetAccountKey(azAccountKey : string);
begin
if fAccountKey <> azAccountKey then
begin
fAccountKey := azAccountKey ;
fconAzure.AccountKey := azAccountKey;
end;
end;
procedure TQuickAzure.SetAzureProtocol(azProtocol: TAzureProtocol);
begin
if fAzureProtocol <> azProtocol then
begin
fAzureProtocol := azProtocol;
if azProtocol = azHTTP then fconAzure.Protocol := 'HTTP'
else fconAzure.Protocol := 'HTTPS';
end;
end;
function TQuickAzure.FileToArray(cFilename : string) : TArray<Byte>;
var
fs : TFileStream;
begin
fs := TFileStream.Create(cFilename, fmOpenRead);
try
Result := ByteContent(fs);
finally
fs.Free;
end;
end;
function TQuickAzure.StreamToArray(cStream : TStream) : TArray<Byte>;
var
bs : TBytesStream;
begin
bs := TBytesStream.Create(Result);
try
bs.LoadFromStream(cStream);
Result := bs.Bytes;
finally
bs.Free
end;
end;
function TQuickAzure.ByteContent(DataStream: TStream): TBytes;
var
Buffer: TBytes;
begin
if not Assigned(DataStream) then Exit(nil);
SetLength(Buffer, DataStream.Size);
// the content may have been read
DataStream.Position := 0;
if DataStream.Size > 0 then
DataStream.Read(Buffer[0], DataStream.Size);
Result := Buffer;
end;
function TQuickAzure.GMT2DateTime(const gmtdate : string):TDateTime;
function GetMonthDig(Value : string):Integer;
const
aMonth : array[1..12] of string = ('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');
var
idx : Integer;
begin
Result := 0;
for idx := 1 to 12 do
begin
if CompareText(Value,aMonth[idx]) = 0 then
begin
Result := idx;
Break;
end;
end;
end;
var
i : Integer;
Len : Integer;
wDay, wMonth, wYear,
wHour, wMinute, wSec : Word;
begin
//GMT Format: 'Mon, 12 Jan 2014 16:20:35 GMT'
Result := 0;
Len := 0;
if gmtdate = '' then Exit;
try
for i := 0 to Length(gmtdate) do
begin
if gmtdate[i] in ['0'..'9'] then
begin
Len := i;
Break;
end;
end;
//Day
wDay := StrToIntDef(Copy(gmtdate,Len,2),0);
if wDay = 0 then Exit;
Inc(Len,3);
//Month
wMonth := GetMonthDig(Copy(gmtdate,Len,3));
if wMonth = 0 then Exit;
Inc(Len,4);
//Year
wYear := StrToIntDef(Copy(gmtdate,Len,4),0);
if wYear = 0 then Exit;
Inc(Len,5);
//Hour
wHour := StrToIntDef(Copy(gmtdate,Len,2),99);
if wHour = 99 then Exit;
Inc(Len,3);
//Min
wMinute := StrToIntDef(Copy(gmtdate,Len,2),99);
if wMinute = 99 then Exit;
Inc(Len,3);
//Sec
wSec := StrToIntDef(Copy(gmtdate,Len,2),99);
if wSec = 99 then Exit;
Result := EncodeDate(wYear,wMonth,wDay) + EncodeTime(wHour,wMinute,wSec,0);
except
Result := 0;
end;
end;
function GetResponseInfo(ResponseInfo : TCloudResponseInfo) : TAzureResponseInfo;
begin
Result.StatusCode := ResponseInfo.StatusCode;
Result.StatusMsg := ResponseInfo.StatusMessage;
end;
function TQuickAzure.PutBlob(const azContainer, cFilename, azBlobName : string; out azResponseInfo : TAzureResponseInfo) : Boolean;
var
BlobService : TAzureBlobService;
Content : TArray<Byte>;
CloudResponseInfo : TCloudResponseInfo;
container : string;
blobname : string;
begin
BlobService := TAzureBlobService.Create(fconAzure);
try
container := CheckContainer(azContainer);
CloudResponseInfo := TCloudResponseInfo.Create;
try
BlobService.Timeout := fTimeout;
Content := FileToArray(cFilename);
if azBlobName = '' then blobname := cFilename
else blobname := azBlobName;
if blobname.StartsWith('/') then blobname := Copy(blobname,2,Length(blobname));
Result := BlobService.PutBlockBlob(container,blobname,Content,EmptyStr,nil,nil,CloudResponseInfo);
azResponseInfo := GetResponseInfo(CloudResponseInfo);
finally
CloudResponseInfo.Free;
end;
finally
BlobService.Free;
end;
end;
function TQuickAzure.PutBlob(const azContainer : string; cStream : TStream; const azBlobName : string; out azResponseInfo : TAzureResponseInfo) : Boolean;
var
BlobService : TAzureBlobService;
Content : TBytes;
CloudResponseInfo : TCloudResponseInfo;
container : string;
blobname : string;
begin
Result := False;
azResponseInfo.StatusCode := 500;
if cStream.Size = 0 then
begin
azResponseInfo.StatusMsg := 'Stream is empty';
Exit;
end;
container := CheckContainer(azContainer);
blobname := RemoveFirstSlash(azBlobName);
try
BlobService := TAzureBlobService.Create(fconAzure);
try
BlobService.Timeout := fTimeout;
CloudResponseInfo := TCloudResponseInfo.Create;
try
Content := ByteContent(cStream);
Result := BlobService.PutBlockBlob(container,blobname,Content,EmptyStr,nil,nil,CloudResponseInfo);
azResponseInfo := GetResponseInfo(CloudResponseInfo);
finally
CloudResponseInfo.Free;
end;
finally
BlobService.Free;
end;
except
on E : Exception do
begin
azResponseInfo.StatusCode := 500;
azResponseInfo.StatusMsg := e.message;
Result := False;
end;
end;
end;
function TQuickAzure.GetBlob(const azContainer, azBlobName, cFilenameTo : string; out azResponseInfo : TAzureResponseInfo) : Boolean;
var
BlobService : TAzureBlobService;
fs : TFileStream;
CloudResponseInfo : TCloudResponseInfo;
container : string;
blobname : string;
begin
container := CheckContainer(azContainer);
blobname := RemoveFirstSlash(azBlobName);
BlobService := TAzureBlobService.Create(fconAzure);
try
BlobService.Timeout := fTimeout;
fs := TFileStream.Create(cFilenameTo,fmCreate);
try
try
CloudResponseInfo := TCloudResponseInfo.Create;
try
Result := BlobService.GetBlob(container,blobname,fs,EmptyStr,CloudResponseInfo);
azResponseInfo := GetResponseInfo(CloudResponseInfo);
finally
CloudResponseInfo.Free;
end;
except
Result := False;
end;
finally
fs.Free;
end;
finally
BlobService.Free;
end;
end;
function TQuickAzure.GetBlob(const azContainer, azBlobName : string; out azResponseInfo : TAzureResponseInfo; out Stream : TMemoryStream) : Boolean;
begin
Stream := TMemoryStream.Create;
try
Result := GetBlob(azContainer,azBlobName,azResponseInfo,TStream(Stream));
except
Stream.Free;
end;
end;
function TQuickAzure.GetBlob(const azContainer, azBlobName : string; out azResponseInfo : TAzureResponseInfo) : TMemoryStream;
begin
GetBlob(azContainer,azBlobName,azResponseInfo,Result);
end;
function TQuickAzure.GetBlob(const azContainer, azBlobName: string; out azResponseInfo: TAzureResponseInfo; var Stream: TStream): Boolean;
var
BlobService : TAzureBlobService;
CloudResponseInfo : TCloudResponseInfo;
container : string;
blobname : string;
begin
container := CheckContainer(azContainer);
blobname := RemoveFirstSlash(azBlobName);
BlobService := TAzureBlobService.Create(fconAzure);
try
BlobService.Timeout := fTimeout;
CloudResponseInfo := TCloudResponseInfo.Create;
try
Result := BlobService.GetBlob(container,blobname,Stream,EmptyStr,CloudResponseInfo);
azResponseInfo := GetResponseInfo(CloudResponseInfo);
finally
CloudResponseInfo.Free;
end;
finally
BlobService.Free;
end;
end;
function TQuickAzure.CheckContainer(const aContainer: string): string;
begin
if aContainer = '' then Result := '$root'
else Result := aContainer;
end;
function TQuickAzure.CopyBlob(const azSourceContainer, azSourceBlobName : string; azTargetContainer, azTargetBlobName : string; out azResponseInfo : TAzureResponseInfo) : Boolean;
var
BlobService : TAzureBlobService;
CloudResponseInfo : TCloudResponseInfo;
sourcecontainer : string;
targetcontainer : string;
sourceblobname : string;
targetblobname : string;
begin
sourcecontainer := CheckContainer(azSourceContainer);
targetcontainer := CheckContainer(azTargetContainer);
sourceblobname := RemoveFirstSlash(azSourceBlobName);
targetblobname := RemoveFirstSlash(azTargetBlobName);
BlobService := TAzureBlobService.Create(fconAzure);
try
BlobService.Timeout := fTimeout;
try
CloudResponseInfo := TCloudResponseInfo.Create;
try
Result := BlobService.CopyBlob(targetcontainer,targetblobname,sourcecontainer,sourceblobname,'',nil,CloudResponseInfo);
azResponseInfo := GetResponseInfo(CloudResponseInfo);
finally
CloudResponseInfo.Free;
end;
except
on E : Exception do
begin
Result := False;
azResponseInfo.StatusCode := 500;
azResponseInfo.StatusMsg := e.message;
end;
end;
finally
BlobService.Free;
end;
end;
function TQuickAzure.RemoveFirstSlash(const aValue: string): string;
begin
if aValue.StartsWith('/') then Result := Copy(aValue,2,Length(aValue))
else Result := aValue;
end;
function TQuickAzure.RenameBlob(const azContainer, azSourceBlobName, azTargetBlobName : string; out azResponseInfo : TAzureResponseInfo) : Boolean;
var
sourceblobname : string;
begin
Result := False;
if sourceblobname.Contains('%') then sourceblobname := azSourceBlobName
else sourceblobname := TIdURI.PathEncode(azSourceBlobName);
if CopyBlob(azContainer,sourceblobname,azContainer,azTargetBlobName,azResponseInfo) then
begin
Result := DeleteBlob(azContainer,azSourceBlobName,azResponseInfo);
end;
end;
function TQuickAzure.ExistsObject(const azContainer, azBlobName : string) : Boolean;
var
azBlob : string;
azBlobs : TStrings;
ResponseInfo : TAzureResponseInfo;
begin
Result := False;
azBlobs := ListBlobsNames(azContainer,azBlobName,False,ResponseInfo);
try
if (ResponseInfo.StatusCode = 200) and (Assigned(azBlobs)) then
begin
for azBlob in azBlobs do
begin
if azBlob = azBlobName then
begin
Result := True;
Break;
end;
end;
end;
finally
azBlobs.Free;
end;
end;
function TQuickAzure.ExistsFolder(const azContainer, azFolderName : string) : Boolean;
var
BlobService : TAzureBlobService;
azBlob : TAzureBlob;
azBlobList : TList<TAzureBlob>;
CloudResponseInfo : TCloudResponseInfo;
AzParams : TStrings;
cNextMarker : string;
container : string;
foldername : string;
begin
Result := False;
container := CheckContainer(azContainer);
BlobService := TAzureBlobService.Create(fconAzure);
try
BlobService.Timeout := fTimeout;
AzParams := TStringList.Create;
try
if not azFolderName.EndsWith('/') then foldername := azFolderName + '/'
else foldername := azFolderName;
AzParams.Values['prefix'] := foldername;
AzParams.Values['delimiter'] := '/';
AzParams.Values['maxresults'] := '1';
cNextMarker := '';
CloudResponseInfo := TCloudResponseInfo.Create;
try
azBlobList := BlobService.ListBlobs(container,cNextMarker,AzParams,CloudResponseInfo);
try
if (Assigned(azBlobList)) and (azBlobList.Count > 0) and (CloudResponseInfo.StatusCode = 200) then Result := True;
finally
//frees azbloblist objects
for azBlob in azBlobList do azBlob.Free;
azBlobList.Free;
end;
finally
CloudResponseInfo.Free;
end;
finally
AzParams.Free;
end;
finally
BlobService.Free;
end;
end;
function TQuickAzure.DeleteBlob(const azContainer,azBlobName : string; out azResponseInfo : TAzureResponseInfo) : Boolean;
var
BlobService : TAzureBlobService;
CloudResponseInfo : TCloudResponseInfo;
container : string;
blobname : string;
begin
container := CheckContainer(azContainer);
blobname := RemoveFirstSlash(azBlobName);
BlobService := TAzureBlobService.Create(fconAzure);
try
BlobService.Timeout := fTimeout;
CloudResponseInfo := TCloudResponseInfo.Create;
try
Result := BlobService.DeleteBlob(container,blobname,False,EmptyStr,CloudResponseInfo);
azResponseInfo := GetResponseInfo(CloudResponseInfo);
finally
CloudResponseInfo.Free;
end;
finally
BlobService.Free;
end;
end;
{$IFDEF DELPHITOKYO_UP}
function TQuickAzure.ListBlobs(const azContainer, azBlobsStartWith : string; Recursive : Boolean; out azResponseInfo : TAzureResponseInfo) : TBlobList;
var
BlobService : TAzureBlobService;
azBlob : TAzureBlobItem;
azBlobList : TArray<TAzureBlobItem>;
Blob : TAzureBlobObject;
CloudResponseInfo : TCloudResponseInfo;
cNextMarker : string;
container : string;
prefix : string;
blobprefix : TArray<string>;
xmlresp : string;
folder : string;
prop : TPair<string,string>;
previousMaker : string;
begin
Result := TBlobList.Create(True);
cNextMarker := '';
container := CheckContainer(azContainer);
BlobService := TAzureBlobService.Create(fconAzure);
try
if Recursive then prefix := ''
else prefix := '/';
BlobService.Timeout := fTimeout;
repeat
CloudResponseInfo := TCloudResponseInfo.Create;
try
previousMaker := cNextMarker;
cNextMarker := '';
azBlobList := BlobService.ListBlobs(azContainer,azBlobsStartWith,'/',previousMaker,100,[],cNextMarker,blobprefix,xmlresp,CloudResponseInfo);
azResponseInfo := GetResponseInfo(CloudResponseInfo);
if Assigned(azBlobList) then
begin
if Result.Capacity < High(azBlobList) then Result.Capacity := High(azBlobList);
end;
//get folders (prefix)
for folder in blobprefix do
begin
Blob := TAzureBlobObject.Create;
if folder.EndsWith('/') then Blob.Name := RemoveLastChar(folder)
else Blob.Name := folder;
Blob.Name := Copy(Blob.Name,Blob.Name.LastDelimiter('/')+2,Blob.Name.Length);
Blob.IsDir := True;
Result.Add(Blob);
end;
//get files (blobs)
if Assigned(azBlobList) then
begin
for azBlob in azBlobList do
begin
Blob := TAzureBlobObject.Create;
Blob.Name := azBlob.Name;
for prop in azBlob.Properties do
begin
if prop.Key = 'Content-Length' then Blob.Size := StrToInt64Def(prop.Value,0)
else if prop.Key = 'Last-Modified' then Blob.LastModified := GMT2DateTime(prop.Value);
end;
Blob.IsDir := False;
Result.Add(Blob);
end;
end;
azBlobList := nil;
finally
CloudResponseInfo.Free;
end;
until (cNextMarker = '') or (azResponseInfo.StatusCode <> 200);
finally
BlobService.Free;
end;
end;
{$ELSE}
function TQuickAzure.ListBlobs(const azContainer, azBlobsStartWith : string; Recursive : Boolean; out azResponseInfo : TAzureResponseInfo) : TBlobList;
var
BlobService : TAzureBlobService;
azBlob : TAzureBlob;
azBlobList : TList<TAzureBlob>;
Blob : TAzureBlobObject;
CloudResponseInfo : TCloudResponseInfo;
cNextMarker : string;
AzParams : TStrings;
container : string;
xmlresp : string;
previousMarker : string;
begin
Result := TBlobList.Create(True);
cNextMarker := '';
container := CheckContainer(azContainer);
BlobService := TAzureBlobService.Create(fconAzure);
try
BlobService.Timeout := fTimeout;
repeat
AzParams := TStringList.Create;
try
AzParams.Values['prefix'] := azBlobsStartWith;
if not Recursive then AzParams.Values['delimiter'] := '/';
if cNextMarker <> '' then AzParams.Values['marker'] := cNextMarker;
CloudResponseInfo := TCloudResponseInfo.Create;
try
azBlobList := BlobService.ListBlobs(container,cNextMarker,AzParams,CloudResponseInfo);
azResponseInfo := GetResponseInfo(CloudResponseInfo);
if Assigned(azBlobList) then
begin
Result.Capacity := High(azBlobList);
try
for azBlob in azBlobList do
begin
Blob := TAzureBlobObject.Create;
Blob.Name := azBlob.Name;
Blob.Size := StrToInt64Def(azBlob.Properties.Values['Content-Length'],0);
Blob.LastModified := GMT2DateTime(azBlob.Properties.Values['Last-Modified']);
Result.Add(Blob);
end;
finally
//frees azbloblist objects
for azBlob in azBlobList do azBlob.Free;
azBlobList.Free;
end;
end;
azBlobList := nil;
finally
CloudResponseInfo.Free;
end;
finally
FreeAndNil(AzParams);
end;
until (cNextMarker = '') or (azResponseInfo.StatusCode <> 200);
finally
BlobService.Free;
end;
end;
{$ENDIF}
{$IFDEF DELPHITOKYO_UP}
function TQuickAzure.ListBlobsNames(const azContainer, azBlobsStartWith : string; Recursive : Boolean; out azResponseInfo : TAzureResponseInfo) : TStrings;
var
BlobService : TAzureBlobService;
azBlob : TAzureBlobItem;
azBlobList : TArray<TAzureBlobItem>;
Blob : TAzureBlobObject;
CloudResponseInfo : TCloudResponseInfo;
cNextMarker : string;
container : string;
prefix : string;
blobprefix : TArray<string>;
xmlresp : string;
folder : string;
prop : TPair<string,string>;
previousMaker : string;
begin
Result := TStringList.Create;
cNextMarker := '';
container := CheckContainer(azContainer);
BlobService := TAzureBlobService.Create(fconAzure);
try
if Recursive then prefix := ''
else prefix := '/';
BlobService.Timeout := fTimeout;
repeat
CloudResponseInfo := TCloudResponseInfo.Create;
try
previousMaker := cNextMarker;
azBlobList := BlobService.ListBlobs(azContainer,azBlobsStartWith,'/',previousMaker,100,[],cNextMarker,blobprefix,xmlresp,CloudResponseInfo);
azResponseInfo := GetResponseInfo(CloudResponseInfo);
if Assigned(azBlobList) then Result.Capacity := High(azBlobList);
//get folders (prefix)
for folder in blobprefix do
begin
Blob := TAzureBlobObject.Create;
if folder.EndsWith('/') then Blob.Name := RemoveLastChar(folder)
else Blob.Name := folder;
Result.Add(Copy(Blob.Name,Blob.Name.LastDelimiter('/')+2,Blob.Name.Length));
end;
//get files (blobs)
if Assigned(azBlobList) then
begin
for azBlob in azBlobList do
begin
Result.Add(azBlob.Name);
end;
end;
finally
CloudResponseInfo.Free;
end;
until (cNextMarker = '') or (azResponseInfo.StatusCode <> 200);
finally
BlobService.Free;
end;
end;
{$ELSE}
function TQuickAzure.ListBlobsNames(const azContainer, azBlobsStartWith : string; Recursive : Boolean; out azResponseInfo : TAzureResponseInfo) : TStrings;
var
BlobService : TAzureBlobService;
azBlob : TAzureBlob;
azBlobList : TList<TAzureBlob>;
CloudResponseInfo : TCloudResponseInfo;
cNextMarker : string;
AzParams : TStrings;
container : string;
begin
Result := TStringList.Create;
cNextMarker := '';
container := CheckContainer(azContainer);
BlobService := TAzureBlobService.Create(fconAzure);
CloudResponseInfo := TCloudResponseInfo.Create;
try
BlobService.Timeout := fTimeout;
repeat
AzParams := TStringList.Create;
try
AzParams.Values['prefix'] := azBlobsStartWith;
if not Recursive then AzParams.Values['delimiter'] := '/';
if cNextMarker <> '' then AzParams.Values['marker'] := cNextMarker;
azBlobList := BlobService.ListBlobs(container,cNextMarker,AzParams,CloudResponseInfo);
azResponseInfo := GetResponseInfo(CloudResponseInfo);
if Assigned(azBlobList) then
begin
Result.Capacity := azBlobList.Count;
Result.BeginUpdate;
try
for azBlob in azBlobList do Result.Add(azBlob.Name);
finally
Result.EndUpdate;
//frees bloblist objects
for azBlob in azBlobList do azBlob.Free;
azBlobList.Free;
end;
end;
finally
AzParams.Free;
end;
until (cNextMarker = '') or (azResponseInfo.StatusCode <> 200);
finally
BlobService.Free;
CloudResponseInfo.Free;
end;
end;
{$ENDIF}
function TQuickAzure.ExistsContainer(const azContainer : string) : Boolean;
var
Container : string;
Containers : TStrings;
ResponseInfo : TAzureResponseInfo;
begin
Result := False;
Containers := ListContainers(azContainer,ResponseInfo);
try
if (ResponseInfo.StatusCode = 200) and (Assigned(Containers)) then
begin
for Container in Containers do
begin
if Container = azContainer then
begin
Result := True;
Break;
end;
end;
end;
finally
Containers.Free;
end;
end;
function TQuickAzure.ListContainers(const azContainersStartWith : string; out azResponseInfo : TAzureResponseInfo) : TStrings;
var
BlobService : TAzureBlobService;
CloudResponseInfo : TCloudResponseInfo;
cNextMarker : string;
AzParams : TStrings;
AzContainer : TAzureContainer;
AzContainers : TList<TAzureContainer>;
begin
Result := TStringList.Create;
cNextMarker := '';
BlobService := TAzureBlobService.Create(fconAzure);
CloudResponseInfo := TCloudResponseInfo.Create;
try
BlobService.Timeout := fTimeout;
repeat
AzParams := TStringList.Create;
try
if azContainersStartWith <> '' then AzParams.Values['prefix'] := azContainersStartWith;
if cNextMarker <> '' then AzParams.Values['marker'] := cNextMarker;
AzContainers := BlobService.ListContainers(cNextMarker,AzParams,CloudResponseInfo);
try
azResponseInfo := GetResponseInfo(CloudResponseInfo);
if (azResponseInfo.StatusCode = 200) and (Assigned(AzContainers)) then
begin
Result.Capacity := AzContainers.Count;
for AzContainer in AzContainers do
begin
Result.Add(AzContainer.Name);
end;
end;
finally
if Assigned(AzContainers) then
begin
//frees ContainerList objects
for AzContainer in AzContainers do AzContainer.Free;
AzContainers.Free;
end;
end;
finally
AzParams.Free;
end;
until (cNextMarker = '') or (azResponseInfo.StatusCode <> 200);
finally
BlobService.Free;
CloudResponseInfo.Free;
end;
end;
function TQuickAzure.CreateContainer(const azContainer : string; azPublicAccess : TBlobPublicAccess; out azResponseInfo : TAzureResponseInfo) : Boolean;
var
BlobService : TAzureBlobService;
CloudResponseInfo : TCloudResponseInfo;
begin
Result := False;
if azContainer = '' then Exit;
BlobService := TAzureBlobService.Create(fconAzure);
try
BlobService.Timeout := fTimeout;
CloudResponseInfo := TCloudResponseInfo.Create;
try
Result := BlobService.CreateContainer(azContainer,nil,azPublicAccess,CloudResponseInfo);
azResponseInfo := GetResponseInfo(CloudResponseInfo);
finally
CloudResponseInfo.Free;
end;
finally
BlobService.Free;
end;
end;
function TQuickAzure.DeleteContainer(const azContainer : string; out azResponseInfo : TAzureResponseInfo) : Boolean;
var
BlobService : TAzureBlobService;
CloudResponseInfo : TCloudResponseInfo;
begin
Result := False;
if azContainer = '' then Exit;
BlobService := TAzureBlobService.Create(fconAzure);
try
BlobService.Timeout := fTimeout;
CloudResponseInfo := TCloudResponseInfo.Create;
try
Result := BlobService.DeleteContainer(azContainer,CloudResponseInfo);
azResponseInfo := GetResponseInfo(CloudResponseInfo);
finally
CloudResponseInfo.Free;
end;
finally
BlobService.Free;
end;
end;
end.