AWS (Amazon Web Services) SDK for Delphi

A new relevant open source project is now available for Delphi community on GitHub: AWS SDK for Delphi.

The AWS SDK for Delphi enables Delphi developers to easily work with Amazon Web Services and build scalable solutions with Amazon SES, Amazon SQS, and more. It is a non-official SDK based on the official AWS SDK for .NET. A few selected popular services are added, and more will be added very soon.

About Amazon Web Services

From Amazon Web Services (AWS) web site itself: “AWS is the world’s most comprehensive and broadly adopted cloud platform, offering over 200 fully featured services from data centers globally. Millions of customers—including the fastest-growing startups, largest enterprises, and leading government agencies—are using AWS to lower costs, become more agile, and innovate faster.

AWS includes useful services for developers to send notifications (Amazon SNS), send and receive e-mails (Amazon SES), use queue systems (Amazon SQS), several database systems (Amazon RDS, Amazon DynamoDB), memory cache (Amazon ElastiCache), content delivery network (Amazon CloudFront), storage (Amazon S3 and Amazon Glacier), and many more!.

Using the SDK

Each Amazon web service has its own package and unit name scheme, which is AWS<service>.dproj and AWS.<service>.*.pas, respectively. For example, for Amazon SQS (Simple Queue Service), the package name is AWSSQS.dproj and unit name is AWS.SQS.pas (and all other units in the package follow same pattern, like AWS.SQS.Client.pas or AWS.SQS.ClientIntf.pas.

Most types you need will be in the main unit, which for example is AWS.SQS. So that’s the only unit you will need to use most of the functions. From there you can access all the available API operations. Each operation method receives a request interface and returns a response interface.

The following examples receive a message from an SQS queue and output the id and body of each message received:

// 1. Use main unit
uses AWS.SQS;

procedure WriteMessageIds(const QueueUrl: string);
var
  Client: IAmazonSQS;
  Response: IReceiveMessageResponse;
  Request: IReceiveMessageRequest;
  Msg: AWS.SQS.TMessage;
begin
  // 2. Instantiate client interface
  Client := TAmazonSQSClient.Create;

  // 3. Create and fill the request
  Request := TReceiveMessageRequest.Create;
  Request.QueueUrl := QueueUrl;

  // 4. Call operation method passing the request to receive the response;
  Response := Client.ReceiveMessage(Request);

  // 5. Process the response
  for Msg in Response.Messages do
  begin
    WriteLn(Msg.MessageId);
    WriteLn(Msg.Body);
    WriteLn;
  end;
end;

The following example sends an e-mail to the specified address using the specified subject and message:

// 1. Use main unit
uses AWS.SES;

procedure SendEmail(const Recipient, Subject, Content: string);
var
  Client: IAmazonSimpleEmailService;
  Request: ISendEmailRequest;
  Response: ISendEmailResponse;
begin
  // 2. Instantiate client interface
  Client := TAmazonSimpleEmailServiceClient.Create;

  // 3. Create and fill the request
  Request := TSendEmailRequest.Create;
  Request.Source := SenderEmail; 
  Request.Destination := TDestination.Create;
  Request.Destination.ToAddresses.Add(Recipient);
  Request.Message := TMessage.Create(
    TContent.Create(Subject),
    TBody.Create(TContent.Create(Content)));

  // 4. Call operation method passing the request to receive the response;
  Response := Client.SendEmail(Request);

  // 5. Process the response
  WriteLn(Response.MessageId);
end;

Credentials

The AWS SDK for Delphi searches for credentials in a certain order and uses the first available set for the current application.

Credential search order

  1. Credentials that are explicitly set on the AWS service client, as described in below.
  2. A credentials profile with the name specified by a value in TAWSConfigs.AWSProfileName.
  3. A credentials profile with the name specified by the AWS_PROFILE environment variable.
  4. The [default] credentials profile.

Passing access and secret keys directly to client

You can simply pass the Access key ID and Secret key directly in the client constructor:

  Client := TAmazonSQSClient.Create(myAccessKey, mySecretKey);

Although using credentials profile is recommended as it’s easier to manage and also compatible with AWS Command Line Interface.

Enjoy and comment!

Thanks for being with me so far. AWS SDK is was born to grow and make the whole AWS world accessible to you. It is fair-code distributed under Apache 2.0 with Commons Clause license, and requires TMS Business, more specifically TMS BCL and TMS Sparkle. Because life is short. Please leave your impressions and comments here, and if you have any suggestion for the framework, feel free to use the GitHub Issues page of the project!

(*) AWS building photo by Tony Webster. Original picture was cropped to better fit in the page.

Notable Replies

  1. Thank you for your comments, @Russell. Indeed, just after that blog post was published, one month ago, six more APIs were added:

    Feel free to report issues and suggest more APIs here in forum in the Issues area of the AWS SDK for Delphi repository.

Continue the discussion at forum.landgraf.dev

3 more replies

Participants