简体   繁体   中英

Upload files to Amazon S3 with Delphi using temporary security credentials

I have an AWS S3 account and got SecretAccessKey, SessionToken, Expiration, AccessKeyId items. I would like to upload some files to the cloud, in the simplest way.

Have read a some docs regarding authorization headers ( http://docs.aws.amazon.com/AmazonS3/latest/API/sigv4-auth-using-authorization-header.html ) but still do not understand how to build them))

also, saw another example with Indy, so, please help to build the authorization header with these items that I have: SecretAccessKey, SessionToken, Expiration, AccessKeyId. It's ok to be with a "Transferring Payload in a Single Chunk" mode + "Signed payload option".

  FS := TFileStream.Create('c:\myfile.txt', fmOpenRead or fmShareDenyWrite);
  try
    IdHTTP1.Request.CustomHeaders.Values['Authorization'] := ...; // please help
    IdHTTP1.Request.BasicAuthentication := False;
    IdHTTP1.Request.Date := ...;   //what should I enter here?
    IdHTTP1.Request.Expect := '100-continue';
    IdHTTP1.Request.ProtocolVersion := pv1_1;
    ...
    IdHTTP1.Put('http://'+BucketName+'.s3.amazonaws.com/myfile.txt', FS);
  finally
    FS.Free;
  end;

Thank you!

Here's my routine to upload files to Amazon using the Cloud Components:

function UploadFile(File: TBytes; FileName: string; Bucket: string): boolean;
var Service: TAmazonStorageService;
    ConAmazon: TAmazonConnectionInfo;
begin
  try
    ConAmazon := TAmazonConnectionInfo.Create(nil);
    ConAmazon.AccountKey := 'Dih71bG09****************';
    ConAmazon.AccountName := 'AKIA***********';
    ConAmazon.QueueEndpoint := 'queue.amazonaws.com';
    ConAmazon.StorageEndpoint := 's3-eu-west-1.amazonaws.com';
    ConAmazon.TableEndpoint := 'sdb.amazonaws.com';
    ConAmazon.UseDefaultEndpoints := False;
    Service := TAmazonStorageService.Create(ConAmazon);
    Result := Service.UploadObject(Bucket, FileName, File, TRUE, nil, nil, amzbaPrivate, nil);
  finally
    ConAmazon.Free;
    Service.Free;
  end;
end;

Ok, so, finally we've solved the issue:

1.Data.Cloud from Delphi10.1 Berlin should be used. It supports Amazon AWS4 security standard. 2.TAmazonStorageService.InitHeaders should be patched by adding the following code to enable temporary session tokens usage in the header:

...    
Result.Values['x-amz-security-token'] := //your session_token string; 
...

tested it from many sides, works fine now:)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM