-
Notifications
You must be signed in to change notification settings - Fork 186
/
Quick.Amazon.pas
608 lines (558 loc) · 19 KB
/
Quick.Amazon.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
{ ***************************************************************************
Copyright (c) 2015-2021 Kike Pérez
Unit : Quick.Amazon
Description : Amazon object operations
Author : Kike Pérez
Version : 1.4
Created : 18/11/2016
Modified : 18/11/2021
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.Amazon;
{$i QuickLib.inc}
interface
uses
Classes,
System.SysUtils,
System.Generics.Collections,
IPPeerClient,
Data.Cloud.CloudAPI,
Data.Cloud.AmazonAPI,
Quick.Commons;
const
AWSRegionSet : array of string = [
'eu-west-1',
'eu-west-2',
'eu-west-3',
'eu-central-1',
'us-east-1',
'us-east-2',
'us-west-1',
'us-west-2',
'ap-east-1',
'ap-south-1',
'ap-southeast-1',
'ap-southeast-2',
'ap-northeast-1',
'ap-northeast-2',
'ap-northeast-3',
'ca-central-1',
'sa-east-1',
'us-east-1', // deprecated
'eu-west-1', // deprecated
'cn-north-1',
'cn-northwest-1',
'eu-north-1',
'me-south-1'];
type
TAmazonProtocol = (amHTTP,amHTTPS);
TAmazonStorage = TAmazonStorageService;
TAmazonACLAccess = TAmazonACLType;
TAmazonRegion = Data.Cloud.AmazonAPI.TAmazonRegion;
TAmazonObject = class
Name : string;
Modified : TDateTime;
Size : Int64;
IsDeleted : Boolean;
end;
TAmazonObjects = class(TObjectList<TAmazonObject>);
TAmazonResponseInfo = record
StatusCode : Integer;
StatusMsg : string;
end;
TQuickAmazon = class
private
fconAmazon : TAmazonConnectionInfo;
fAccountName : string;
fAccountKey : string;
fAWSRegion : TAmazonRegion;
fAmazonProtocol : TAmazonProtocol;
procedure SetAccountName(amAccountName : string);
procedure SetAccountKey(amAccountKey : string);
procedure SetAmazonProtocol(amProtocol : TAmazonProtocol);
procedure SetAWSRegion(Value : TAmazonRegion);
function FileToArray(cFilename : string) : TArray<Byte>;
function ByteContent(DataStream: TStream): TBytes;
public
constructor Create; overload;
constructor Create(amAccountName, amAccountKey : string); overload;
destructor Destroy; override;
property AccountName : string read fAccountName write SetAccountName;
property AccountKey : string read fAccountKey write SetAccountKey;
property AmazonProtocol : TAmazonProtocol read fAmazonProtocol write SetAmazonProtocol;
property AWSRegion : TAmazonRegion read fAWSRegion write SetAWSRegion;
function StorageURL(amBucket : string) : string;
function PutObject(amBucket, cFilename, amObjectName : string; amACLType : TAmazonACLType; var amResponseInfo : TAmazonResponseInfo) : Boolean; overload;
function PutObject(amBucket : string; cStream : TStream; amObjectName : string; amACLType : TAmazonACLType; var amResponseInfo : TAmazonResponseInfo) : Boolean; overload;
function GetObject(amBucket, amObjectName, cFilenameTo : string; var amResponseInfo : TAmazonResponseInfo) : Boolean; overload;
function GetObject(amBucket, amObjectName : string; var amResponseInfo : TAmazonResponseInfo) : TMemoryStream; overload;
function ExistsObject(amBucket, amObjectName : string; amRegion : TAmazonRegion) : Boolean;
function DeleteObject(amBucket,amObjectName : string; var amResponseInfo : TAmazonResponseInfo) : Boolean;
function ListObjects(amBucket : string; amObjectsStartWith : string; amRegion : TAmazonRegion; var amResponseInfo : TAmazonResponseInfo) : TAmazonObjects;
function ListObjectsNames(amBucket : string; amObjectsStartWith : string; amRegion : TAmazonRegion; var amResponseInfo : TAmazonResponseInfo) : TStrings;
function ExistsBucket(amBucketName : string) : Boolean;
function ListBuckets(var amResponseInfo : TAmazonResponseInfo) : TStrings;
function CreateBucket(amBucket : string; amBucketRegion : TAmazonRegion; amACLType : TAmazonACLAccess; var amResponseInfo : TAmazonResponseInfo) : Boolean;
function DeleteBucket(amBucket : string; amBucketRegion : TAmazonRegion; var amResponseInfo : TAmazonResponseInfo) : Boolean;
{$IFNDEF DELPHISYDNEY_UP}
class function GetAWSRegion(Region: TAmazonRegion): string; overload;
{$ELSE}
class function GetAWSRegion(const Region : string) : TAmazonRegion; overload;
{$ENDIF}
end;
implementation
constructor TQuickAmazon.Create;
begin
inherited;
fconAmazon := TAmazonConnectionInfo.Create(nil);
fAmazonProtocol := amHTTP;
fconAmazon.UseDefaultEndpoints := False;
end;
constructor TQuickAmazon.Create(amAccountName, amAccountKey : string);
begin
Create;
SetAccountName(amAccountName);
SetAccountKey(amAccountKey);
end;
destructor TQuickAmazon.Destroy;
begin
if Assigned(fconAmazon) then fconAmazon.Free;
inherited;
end;
procedure TQuickAmazon.SetAWSRegion(Value : TAmazonRegion);
begin
fAWSRegion := Value;
//fconAmazon.StorageEndpoint := Format('s3-%s.amazonaws.com',[GetAWSRegion(Value)]);
//fconAmazon.StorageEndpoint := Format('s3.%s.amazonaws.com',[GetAWSRegion(Value)]);
{$IFDEF DELPHISYDNEY_UP}
if not StrInArray(Value,AWSRegionSet) then raise Exception.CreateFmt('%s is not a valid region for AmazonS3!',[Value]);
fconAmazon.Region := Value;
{$ELSE}
fconAmazon.StorageEndpoint := Format('s3.%s.amazonaws.com',[GetAWSRegion(Value)]);
{$ENDIF}
end;
procedure TQuickAmazon.SetAccountName(amAccountName : string);
begin
if fAccountName <> amAccountName then
begin
fAccountName := amAccountName;
fconAmazon.AccountName := amAccountName;
end;
end;
procedure TQuickAmazon.SetAccountKey(amAccountKey : string);
begin
if fAccountKey <> amAccountKey then
begin
fAccountKey := amAccountKey ;
fconAmazon.AccountKey := amAccountKey;
end;
end;
procedure TQuickAmazon.SetAmazonProtocol(amProtocol: TAmazonProtocol);
begin
if fAmazonProtocol <> amProtocol then
begin
fAmazonProtocol := amProtocol;
if amProtocol = amHTTP then fconAmazon.Protocol := 'http'
else fconAmazon.Protocol := 'https';
end;
end;
function TQuickAmazon.FileToArray(cFilename : string) : TArray<Byte>;
var
fs : TFileStream;
bs : TBytesStream;
begin
fs := TFileStream.Create(cFilename, fmOpenRead);
try
Result := ByteContent(fs);
finally
fs.Free;
end;
end;
function TQuickAmazon.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 GetResponseInfo(amResponseInfo : TCloudResponseInfo) : TAmazonResponseInfo;
begin
Result.StatusCode := amResponseInfo.StatusCode;
Result.StatusMsg := amResponseInfo.StatusMessage;
end;
function TQuickAmazon.StorageURL(amBucket : string) : string;
begin
Result := fconAmazon.StorageURL(amBucket)
end;
function TQuickAmazon.PutObject(amBucket, cFilename, amObjectName : string; amACLType : TAmazonACLType; var amResponseInfo : TAmazonResponseInfo) : Boolean;
var
AmazonS3 : TAmazonStorage;
Content : TArray<Byte>;
CloudResponseInfo : TCloudResponseInfo;
begin
AmazonS3 := TAmazonStorage.Create(fconAmazon);
if amBucket = '' then amBucket := '$root';
try
Content := FileToArray(cFilename);
if amObjectName = '' then amObjectName := cFilename;
if amObjectName.StartsWith('/') then amObjectName := Copy(amObjectName,2,Length(amObjectName));
CloudResponseInfo := TCloudResponseInfo.Create;
try
Result := AmazonS3.UploadObject(amBucket,amObjectName,Content,False,nil,nil,amACLType,CloudResponseInfo{$IFDEF DELPHIRX11_UP},fAWSRegion{$ENDIF});
amResponseInfo := GetResponseInfo(CloudResponseInfo);
finally
CloudResponseInfo.Free;
end;
finally
AmazonS3.Free;
end;
end;
function TQuickAmazon.PutObject(amBucket : string; cStream : TStream; amObjectName : string; amACLType : TAmazonACLType; var amResponseInfo : TAmazonResponseInfo) : Boolean;
var
AmazonS3 : TAmazonStorage;
Content : TBytes;
CloudResponseInfo : TCloudResponseInfo;
begin
amResponseInfo.StatusCode := 500;
if amBucket = '' then amBucket := '$root';
if amObjectName.StartsWith('/') then amObjectName := Copy(amObjectName,2,Length(amObjectName));
try
AmazonS3 := TAmazonStorage.Create(fconAmazon);
try
//AmazonS3.Timeout := fTimeout;
CloudResponseInfo := TCloudResponseInfo.Create;
try
//CloudResponseInfo.Headers.AddPair();
Content := ByteContent(cStream);
Result := AmazonS3.UploadObject(amBucket,amObjectName,Content,False,nil,nil,amACLType,CloudResponseInfo{$IFDEF DELPHIRX11_UP},fAWSRegion{$ENDIF});
amResponseInfo := GetResponseInfo(CloudResponseInfo);
finally
CloudResponseInfo.Free;
end;
finally
AmazonS3.Free;
SetLength(Content,0);
Content := nil;
end;
except
Result := False;
end;
end;
function TQuickAmazon.GetObject(amBucket, amObjectName, cFilenameTo : string; var amResponseInfo : TAmazonResponseInfo) : Boolean;
var
AmazonS3 : TAmazonStorage;
fs : TFileStream;
CloudResponseInfo : TCloudResponseInfo;
//amParams : TAmazonGetObjectOptionals;
begin
Result := False;
if amBucket = '' then amBucket := '$root';
if amObjectName.StartsWith('/') then amObjectName := Copy(amObjectName,2,Length(amObjectName));
AmazonS3 := TAmazonStorage.Create(fconAmazon);
try
//AmazonS3.Timeout := fTimeout;
CloudResponseInfo := TCloudResponseInfo.Create;
if FileExists(cFilenameTo) then fs := TFileStream.Create(cFilenameTo,fmOpenWrite)
else fs := TFileStream.Create(cFilenameTo,fmCreate);
try
try
AmazonS3.GetObject(amBucket,amObjectName,fs,CloudResponseInfo{$IFDEF DELPHIRX11_UP},fAWSRegion{$ENDIF});
amResponseInfo := GetResponseInfo(CloudResponseInfo);
if amResponseInfo.StatusCode = 200 then Result := True;
except
Result := False;
end;
finally
fs.Free;
CloudResponseInfo.Free;
end;
finally
AmazonS3.Free;
end;
end;
function TQuickAmazon.GetObject(amBucket, amObjectName : string; var amResponseInfo : TAmazonResponseInfo) : TMemoryStream;
var
AmazonS3 : TAmazonStorage;
CloudResponseInfo : TCloudResponseInfo;
begin
Result := TMemoryStream.Create;
if amBucket = '' then amBucket := '$root';
if amObjectName.StartsWith('/') then amObjectName := Copy(amObjectName,2,Length(amObjectName));
AmazonS3 := TAmazonStorage.Create(fconAmazon);
try
//AmazonS3.Timeout := fTimeout;
CloudResponseInfo := TCloudResponseInfo.Create;
try
try
AmazonS3.GetObject(amBucket,amObjectName,Result,CloudResponseInfo{$IFDEF DELPHIRX11_UP},fAWSRegion{$ENDIF});
amResponseInfo := GetResponseInfo(CloudResponseInfo);
except
Result := nil;
end;
finally
CloudResponseInfo.Free;
end;
finally
AmazonS3.Free;
end;
end;
function TQuickAmazon.ExistsObject(amBucket, amObjectName : string; amRegion : TAmazonRegion) : Boolean;
var
amObject : string;
amObjects : TStrings;
ResponseInfo : TAmazonResponseInfo;
begin
Result := False;
amObjects := ListObjectsNames(amBucket,amObjectName,amRegion,ResponseInfo);
try
if (ResponseInfo.StatusCode = 200) and (Assigned(amObjects)) then
begin
for amObject in amObjects do
begin
if amObject = amObjectName then
begin
Result := True;
Break;
end;
end;
end;
finally
amObjects.Free;
end;
end;
function TQuickAmazon.DeleteObject(amBucket,amObjectName : string; var amResponseInfo : TAmazonResponseInfo) : Boolean;
var
AmazonS3 : TAmazonStorage;
CloudResponseInfo : TCloudResponseInfo;
begin
if amBucket = '' then amBucket := '$root';
if amObjectName.StartsWith('/') then amObjectName := Copy(amObjectName,2,Length(amObjectName));
AmazonS3 := TAmazonStorage.Create(fconAmazon);
try
//AmazonS3.Timeout := fTimeout;
CloudResponseInfo := TCloudResponseInfo.Create;
try
Result := AmazonS3.DeleteObject(amBucket,amObjectName,CloudResponseInfo);
amResponseInfo := GetResponseInfo(CloudResponseInfo);
finally
CloudResponseInfo.Free;
end;
finally
AmazonS3.Free;
end;
end;
function TQuickAmazon.ListObjects(amBucket : string; amObjectsStartWith : string; amRegion : TAmazonRegion; var amResponseInfo : TAmazonResponseInfo) : TAmazonObjects;
var
AmazonS3 : TAmazonStorage;
amObject : TAmazonObject;
i : Integer;
amBucketResult : TAmazonBucketResult;
CloudResponseInfo : TCloudResponseInfo;
cNextMarker : string;
amParams : TStrings;
begin
Result := TAmazonObjects.Create(True);
cNextMarker := '';
if amBucket = '' then amBucket := '$root';
AmazonS3 := TAmazonStorage.Create(fconAmazon);
CloudResponseInfo := TCloudResponseInfo.Create;
try
//AmazonS3.Timeout := fTimeout;
repeat
amParams := TStringList.Create;
try
amParams.Values['prefix'] := amObjectsStartWith;
if cNextMarker <> '' then amParams.Values['marker'] := cNextMarker;
amBucketResult := AmazonS3.GetBucket(amBucket,amParams,CloudResponseInfo,amRegion);
amResponseInfo := GetResponseInfo(CloudResponseInfo);
if Assigned(amBucketResult) then
begin
try
Result.Capacity := amBucketResult.Objects.Count;
for i := 0 to amBucketResult.Objects.Count-1 do
begin
amObject := TAmazonObject.Create;
amObject.Name := amBucketResult.Objects[i].Name;
amObject.Modified := StrToDateTime(amBucketResult.Objects[i].LastModified);
amObject.Size := amBucketResult.Objects[i].Size;
amObject.IsDeleted := amBucketResult.Objects[i].IsDeleted;
Result.Add(amObject);
end;
finally
amBucketResult.Free;
end;
cNextMarker := amBucketResult.Marker;
end;
finally
amParams.Free;
end;
until (cNextMarker = '') or (amResponseInfo.StatusCode <> 200);
finally
AmazonS3.Free;
CloudResponseInfo.Free;
end;
end;
function TQuickAmazon.ListObjectsNames(amBucket : string; amObjectsStartWith : string; amRegion : TAmazonRegion; var amResponseInfo : TAmazonResponseInfo) : TStrings;
var
AmazonS3 : TAmazonStorage;
i : Integer;
amBucketResult : TAmazonBucketResult;
CloudResponseInfo : TCloudResponseInfo;
cNextMarker : string;
amParams : TStrings;
begin
Result := TStringList.Create;
cNextMarker := '';
if amBucket = '' then amBucket := '$root';
AmazonS3 := TAmazonStorage.Create(fconAmazon);
CloudResponseInfo := TCloudResponseInfo.Create;
try
//AmazonS3.Timeout := fTimeout;
repeat
amParams := TStringList.Create;
try
if amObjectsStartWith <> '' then amParams.Values['prefix'] := amObjectsStartWith;
if cNextMarker <> '' then amParams.Values['marker'] := cNextMarker;
amBucketResult := AmazonS3.GetBucket(amBucket,amParams,CloudResponseInfo,amRegion);
amResponseInfo := GetResponseInfo(CloudResponseInfo);
if Assigned(amBucketResult) then
begin
try
Result.Capacity := amBucketResult.Objects.Count;
for i := 0 to amBucketResult.Objects.Count-1 do Result.Add(amBucketResult.Objects[i].Name);
finally
amBucketResult.Free;
end;
cNextMarker := amBucketResult.Marker;
end;
finally
amParams.Free;
end;
until (cNextMarker = '') or (amResponseInfo.StatusCode <> 200);
finally
AmazonS3.Free;
CloudResponseInfo.Free;
end;
end;
function TQuickAmazon.ExistsBucket(amBucketName : string) : Boolean;
var
amBucket : string;
amBuckets : TStrings;
ResponseInfo : TAmazonResponseInfo;
begin
Result := False;
amBuckets := ListBuckets(ResponseInfo);
try
if (ResponseInfo.StatusCode = 200) and (Assigned(amBuckets)) then
begin
for amBucket in amBuckets do
begin
if amBucket = amBucketName then
begin
Result := True;
Break;
end;
end;
end;
finally
amBuckets.Free;
end;
end;
function TQuickAmazon.ListBuckets(var amResponseInfo : TAmazonResponseInfo) : TStrings;
var
AmazonS3 : TAmazonStorageService;
CloudResponseInfo : TCloudResponseInfo;
Buckets : TStrings;
i : Integer;
begin
AmazonS3 := TAmazonStorageService.Create(fconAmazon);
Result := TStringList.Create;
try
//AmazonS3.Timeout := fTimeout;
CloudResponseInfo := TCloudResponseInfo.Create;
try
Buckets := AmazonS3.ListBuckets(CloudResponseInfo);
try
Result.Capacity := Buckets.Count;
for i := 0 to Buckets.Count -1 do
begin
Result.Add(Buckets.Names[i]);
end;
amResponseInfo := GetResponseInfo(CloudResponseInfo);
finally
Buckets.Free;
end;
finally
CloudResponseInfo.Free;
end;
finally
AmazonS3.Free;
end;
end;
function TQuickAmazon.CreateBucket(amBucket : string; amBucketRegion : TAmazonRegion; amACLType : TAmazonACLAccess; var amResponseInfo : TAmazonResponseInfo) : Boolean;
var
AmazonS3 : TAmazonStorageService;
CloudResponseInfo : TCloudResponseInfo;
begin
Result := False;
if amBucket = '' then Exit;
AmazonS3 := TAmazonStorageService.Create(fconAmazon);
try
CloudResponseInfo := TCloudResponseInfo.Create;
try
Result := AmazonS3.CreateBucket(amBucket,amACLType,amBucketRegion,CloudResponseInfo);
amResponseInfo := GetResponseInfo(CloudResponseInfo);
finally
CloudResponseInfo.Free;
end;
finally
AmazonS3.Free;
end;
end;
function TQuickAmazon.DeleteBucket(amBucket : string; amBucketRegion : TAmazonRegion; var amResponseInfo : TAmazonResponseInfo) : Boolean;
var
AmazonS3 : TAmazonStorageService;
CloudResponseInfo : TCloudResponseInfo;
begin
Result := False;
if amBucket = '' then Exit;
AmazonS3 := TAmazonStorageService.Create(fconAmazon);
try
CloudResponseInfo := TCloudResponseInfo.Create;
try
Result := AmazonS3.DeleteBucket(amBucket,CloudResponseInfo,amBucketRegion);
amResponseInfo := GetResponseInfo(CloudResponseInfo);
finally
CloudResponseInfo.Free;
end;
finally
AmazonS3.Free;
end;
end;
{$IFNDEF DELPHISYDNEY_UP}
class function TQuickAmazon.GetAWSRegion(Region: TAmazonRegion): string;
begin
Result := TAmazonStorageService.GetRegionString(Region);
end;
{$ELSE}
class function TQuickAmazon.GetAWSRegion(const Region : string) : TAmazonRegion;
begin
Result := TAmazonStorageService.GetRegionFromString(Region);
end;
{$ENDIF}
end.