Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
exilon committed Nov 5, 2020
2 parents 42d0550 + a96ed24 commit f753c1d
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 59 deletions.
91 changes: 57 additions & 34 deletions Quick.Amazon.pas
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
Author : Kike Pérez
Version : 1.4
Created : 18/11/2016
Modified : 11/09/2020
Modified : 05/11/2020
This file is part of QuickLib: https://github.com/exilon/QuickLib
Expand Down Expand Up @@ -39,24 +39,35 @@ interface
System.Generics.Collections,
IPPeerClient,
Data.Cloud.CloudAPI,
Data.Cloud.AmazonAPI;
Data.Cloud.AmazonAPI,
Quick.Commons;

const

AWSRegionSet : array of string = [
'eu-west-1',
'eu-west-1',
'eu-central-1',
'us-east-1',
'us-west-1',
'us-west-2',
'ap-southeast-1',
'ap-southeast-2',
'ap-northeast-1',
'ap-northeast-2',
'sa-east-1',
'us-east-1', // deprecate
'us-east-1','us-east-1'];
'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

Expand Down Expand Up @@ -92,6 +103,7 @@ TQuickAmazon = class
procedure SetAWSRegion(Value : TAmazonRegion);
function FileToArray(cFilename : string) : TArray<Byte>;
function StreamToArray(cStream : TStream) : TArray<Byte>;
function ByteContent(DataStream: TStream): TBytes;
public
constructor Create; overload;
constructor Create(amAccountName, amAccountKey : string); overload;
Expand Down Expand Up @@ -145,7 +157,15 @@ destructor TQuickAmazon.Destroy;
procedure TQuickAmazon.SetAWSRegion(Value : TAmazonRegion);
begin
fAWSRegion := Value;
fconAmazon.StorageEndpoint := Format('s3-%s.amazonaws.com',[GetAWSRegion(Value)]);
if not StrInArray(Value,AWSRegionSet) then raise Exception.CreateFmt('%s is not a valid region for AmazonS3!',[Value]);

//fconAmazon.StorageEndpoint := Format('s3-%s.amazonaws.com',[GetAWSRegion(Value)]);
//fconAmazon.StorageEndpoint := Format('s3.%s.amazonaws.com',[GetAWSRegion(Value)]);
{$IFDEF DELPHISYDNEY_UP}
fconAmazon.Region := Value;
{$ELSE}
fconAmazon.StorageEndpoint := Format('s3.%s.amazonaws.com',[GetAWSRegion(Value)]);
{$ENDIF}
end;

procedure TQuickAmazon.SetAccountName(amAccountName : string);
Expand Down Expand Up @@ -183,13 +203,7 @@ function TQuickAmazon.FileToArray(cFilename : string) : TArray<Byte>;
begin
fs := TFileStream.Create(cFilename, fmOpenRead);
try
bs := TBytesStream.Create(Result);
try
bs.LoadFromStream(fs);
Result := bs.Bytes;
finally
bs.Free
end;
Result := ByteContent(fs);
finally
fs.Free;
end;
Expand All @@ -208,12 +222,18 @@ function TQuickAmazon.StreamToArray(cStream : TStream) : TArray<Byte>;
end;
end;

{function TQuickAmazon.StreamToArray(cStream : TStream) : TArray<Byte>;
function TQuickAmazon.ByteContent(DataStream: TStream): TBytes;
var
Buffer: TBytes;
begin
SetLength(Result,cStream.Size);
cStream.WriteData(Result,Length(Result));
end;}

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
Expand All @@ -234,23 +254,26 @@ function TQuickAmazon.PutObject(amBucket, cFilename, amObjectName : string; amAC
begin
AmazonS3 := TAmazonStorage.Create(fconAmazon);
if amBucket = '' then amBucket := '$root';
CloudResponseInfo := TCloudResponseInfo.Create;
try
Content := FileToArray(cFilename);
if amObjectName = '' then amObjectName := cFilename;
if amObjectName.StartsWith('/') then amObjectName := Copy(amObjectName,2,Length(amObjectName));
Result := AmazonS3.UploadObject(amBucket,amObjectName,Content,False,nil,nil,amACLType,CloudResponseInfo);
amResponseInfo := GetResponseInfo(CloudResponseInfo);
CloudResponseInfo := TCloudResponseInfo.Create;
try
Result := AmazonS3.UploadObject(amBucket,amObjectName,Content,False,nil,nil,amACLType,CloudResponseInfo);
amResponseInfo := GetResponseInfo(CloudResponseInfo);
finally
CloudResponseInfo.Free;
end;
finally
AmazonS3.Free;
CloudResponseInfo.Free;
end;
end;

function TQuickAmazon.PutObject(amBucket : string; cStream : TStream; amObjectName : string; amACLType : TAmazonACLType; var amResponseInfo : TAmazonResponseInfo) : Boolean;
var
AmazonS3 : TAmazonStorage;
Content : TArray<Byte>;
Content : TBytes;
CloudResponseInfo : TCloudResponseInfo;
begin
amResponseInfo.StatusCode := 500;
Expand All @@ -263,7 +286,7 @@ function TQuickAmazon.PutObject(amBucket : string; cStream : TStream; amObjectNa
CloudResponseInfo := TCloudResponseInfo.Create;
try
//CloudResponseInfo.Headers.AddPair();
Content := StreamToArray(cStream);
Content := ByteContent(cStream);
Result := AmazonS3.UploadObject(amBucket,amObjectName,Content,False,nil,nil,amACLType,CloudResponseInfo);
amResponseInfo := GetResponseInfo(CloudResponseInfo);
finally
Expand Down
54 changes: 32 additions & 22 deletions Quick.Azure.pas
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
Author : Kike Pérez
Version : 1.4
Created : 27/08/2015
Modified : 07/04/2020
Modified : 05/11/2020
This file is part of QuickLib: https://github.com/exilon/QuickLib
Expand Down Expand Up @@ -76,6 +76,7 @@ TQuickAzure = class
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;
Expand Down Expand Up @@ -165,13 +166,7 @@ function TQuickAzure.FileToArray(cFilename : string) : TArray<Byte>;
begin
fs := TFileStream.Create(cFilename, fmOpenRead);
try
bs := TBytesStream.Create(Result);
try
bs.LoadFromStream(fs);
Result := bs.Bytes;
finally
bs.Free
end;
Result := ByteContent(fs);
finally
fs.Free;
end;
Expand All @@ -190,6 +185,19 @@ function TQuickAzure.StreamToArray(cStream : TStream) : TArray<Byte>;
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
Expand Down Expand Up @@ -278,7 +286,6 @@ function TQuickAzure.PutBlob(const azContainer, cFilename, azBlobName : string;
container : string;
blobname : string;
begin
Result := False;
BlobService := TAzureBlobService.Create(fconAzure);
try
container := CheckContainer(azContainer);
Expand All @@ -302,11 +309,12 @@ function TQuickAzure.PutBlob(const azContainer, cFilename, azBlobName : string;
function TQuickAzure.PutBlob(const azContainer : string; cStream : TStream; const azBlobName : string; out azResponseInfo : TAzureResponseInfo) : Boolean;
var
BlobService : TAzureBlobService;
Content : TArray<Byte>;
Content : TBytes;
CloudResponseInfo : TCloudResponseInfo;
container : string;
blobname : string;
begin
Result := False;
azResponseInfo.StatusCode := 500;
if cStream.Size = 0 then
begin
Expand All @@ -322,7 +330,7 @@ function TQuickAzure.PutBlob(const azContainer : string; cStream : TStream; cons
BlobService.Timeout := fTimeout;
CloudResponseInfo := TCloudResponseInfo.Create;
try
Content := StreamToArray(cStream);
Content := ByteContent(cStream);
Result := BlobService.PutBlockBlob(container,blobname,Content,EmptyStr,nil,nil,CloudResponseInfo);
azResponseInfo := GetResponseInfo(CloudResponseInfo);
finally
Expand All @@ -349,7 +357,6 @@ function TQuickAzure.GetBlob(const azContainer, azBlobName, cFilenameTo : string
container : string;
blobname : string;
begin
Result := False;
container := CheckContainer(azContainer);
blobname := RemoveFirstSlash(azBlobName);
BlobService := TAzureBlobService.Create(fconAzure);
Expand Down Expand Up @@ -380,7 +387,7 @@ function TQuickAzure.GetBlob(const azContainer, azBlobName : string; out azRespo
begin
Stream := TMemoryStream.Create;
try
GetBlob(azContainer,azBlobName,azResponseInfo,TStream(Stream));
Result := GetBlob(azContainer,azBlobName,azResponseInfo,TStream(Stream));
except
Stream.Free;
end;
Expand All @@ -398,7 +405,6 @@ function TQuickAzure.GetBlob(const azContainer, azBlobName: string; out azRespon
container : string;
blobname : string;
begin
Result := False;
container := CheckContainer(azContainer);
blobname := RemoveFirstSlash(azBlobName);
BlobService := TAzureBlobService.Create(fconAzure);
Expand Down Expand Up @@ -431,7 +437,6 @@ function TQuickAzure.CopyBlob(const azSourceContainer, azSourceBlobName : string
sourceblobname : string;
targetblobname : string;
begin
Result := False;
sourcecontainer := CheckContainer(azSourceContainer);
targetcontainer := CheckContainer(azTargetContainer);
sourceblobname := RemoveFirstSlash(azSourceBlobName);
Expand Down Expand Up @@ -557,7 +562,6 @@ function TQuickAzure.DeleteBlob(const azContainer,azBlobName : string; out azRes
container : string;
blobname : string;
begin
Result := False;
container := CheckContainer(azContainer);
blobname := RemoveFirstSlash(azBlobName);
BlobService := TAzureBlobService.Create(fconAzure);
Expand Down Expand Up @@ -833,11 +837,14 @@ function TQuickAzure.CreateContainer(const azContainer : string; azPublicAccess
try
BlobService.Timeout := fTimeout;
CloudResponseInfo := TCloudResponseInfo.Create;
Result := BlobService.CreateContainer(azContainer,nil,azPublicAccess,CloudResponseInfo);
azResponseInfo := GetResponseInfo(CloudResponseInfo);
try
Result := BlobService.CreateContainer(azContainer,nil,azPublicAccess,CloudResponseInfo);
azResponseInfo := GetResponseInfo(CloudResponseInfo);
finally
CloudResponseInfo.Free;
end;
finally
BlobService.Free;
CloudResponseInfo.Free;
end;
end;

Expand All @@ -853,11 +860,14 @@ function TQuickAzure.DeleteContainer(const azContainer : string; out azResponseI
try
BlobService.Timeout := fTimeout;
CloudResponseInfo := TCloudResponseInfo.Create;
Result := BlobService.DeleteContainer(azContainer,CloudResponseInfo);
azResponseInfo := GetResponseInfo(CloudResponseInfo);
try
Result := BlobService.DeleteContainer(azContainer,CloudResponseInfo);
azResponseInfo := GetResponseInfo(CloudResponseInfo);
finally
CloudResponseInfo.Free;
end;
finally
BlobService.Free;
CloudResponseInfo.Free;
end;
end;

Expand Down
13 changes: 10 additions & 3 deletions Quick.Commons.pas
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ EShellError = class(Exception);
//returns n times a char
function FillStr(const C : Char; const Count : Integer) : string;
//checks if string exists in array of string
function StrInArray(const aValue : string; const aInArray : array of string) : Boolean;
function StrInArray(const aValue : string; const aInArray : array of string; aCaseSensitive : Boolean = True) : Boolean;
//checks if integer exists in array of integer
function IntInArray(const aValue : Integer; const aInArray : array of Integer) : Boolean;
//check if array is empty
Expand Down Expand Up @@ -731,13 +731,20 @@ function FillStr(const C : Char; const Count : Integer) : string;
end;


function StrInArray(const aValue : string; const aInArray : array of string) : Boolean;
function StrInArray(const aValue : string; const aInArray : array of string; aCaseSensitive : Boolean = True) : Boolean;
var
s : string;
begin
for s in aInArray do
begin
if s = aValue then Exit(True);
if aCaseSensitive then
begin
if s = aValue then Exit(True);
end
else
begin
if CompareText(aValue,s) = 0 then Exit(True);
end;
end;
Result := False;
end;
Expand Down

0 comments on commit f753c1d

Please sign in to comment.