Upload files to Amazon S3 with Delphi using temporary security credentials

amazon-s3, amazon-web-services, delphi

Solution

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;

Problem

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!

Original source