Delphi的AWS SDK使Delphi开发人员可以轻松地使用Amazon Web服务,并使用Amazon SES,Amazon SQS等构建可扩展解决方案。它是基于.NET的官方AWS SDK的非官方SDK。示例项目可以在AWS SDK中找到Delphi样品存储库。
这是SDK当前支持的AWS服务列表。随着时间的流逝,更多的会添加:
请使用GitHub问题页面报告问题或请求功能。
自Delphi 10.3 Rio(即,Delphi 10.4 Sydney,Delphi 11,Delphi 12等)以来,Delphi的AWS SDK支持所有最近的Delphi版本,并支持这些Delphi版本中可用的所有平台:Windows,Linux,Linux,Android,Android,ios和MacOS。
Delphi的AWS SDK不需要任何第三方库,它用100%的本机Delphi代码编写,并在支持的Delphi版本的所有SKU上都使用。
您可以安装执行Manuall安装的库。
对于每个不同平台,您要安装SDK:
AWSPackages.groupproj项目组。<sdk_folder>/packages/d12/Win32/Release文件夹到Delphi 12,Win32 Library Path。每个Amazon Web服务都有其自己的软件包和单位名称方案,即AWS<service>.dproj and AWS.<service>.*.pas 。例如,对于Amazon SQS(简单的队列服务),软件包名称为AWSSQS.dproj和单位名称为AWS.SQS.pas (以及包装中的所有其他单元,都遵循相同的模式,例如AWS.SQS.Client.pas或AWS.SQS.ClientIntf.pas 。
您需要的大多数类型都将位于主单元中,例如AWS.SQS 。因此,这是您唯一需要使用大多数功能的单元。您可以从那里访问所有可用的API操作。每个操作方法都会收到请求接口并返回响应接口。
总而言之,这是执行API请求的过程:
AWS.SQS ;TAmazonSQSClient );以下示例从SQS队列接收一条消息,并输出收到的每个消息的ID和正文:
// 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 ;以下示例使用指定主题和消息将电子邮件发送到指定地址:
// 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 ;在所有请求对象中,列表和词典已经实例化,而其他相关对象则不是。您无需销毁从操作传递或接收到的任何接口或对象,也不需要添加到请求中。一些操作已超载的方法超越了原始参数而不是完整的请求对象,以简单地使用。
有关可用操作的更多信息,请参阅您要使用的亚马逊服务的API文档。或者,当然,请使用Delphi代码完成来查找每个服务客户端中可用的所有方法。
您可以完全使用Amazon SNS:Delphi的AWS SDK的简单通知服务。主单元是AWS.SNS 。您可以在本文中找到有关将Amazon SNS与AWS SDK一起用于Delphi的更多详细信息,当然,您可以参考Amazon SNS API参考本身。
创建客户:
var
Client: IAmazonSimpleNotificationService;
begin
Client := TAmazonSimpleNotificationServiceClient.Create;
end ;获取所有现有主题的ARN:
function GetAllTopics (AllTopics: TList<string>);
var
ListRequest: IListTopicsRequest;
ListResponse: IListTopicsResponse;
Topic: TTopic;
begin
ListRequest := TListTopicsRequest.Create;
repeat
ListResponse := Client.ListTopics(ListRequest);
for Topic in ListResponse.Topics do
AllTopics.Add(Topic.TopicArn);
ListRequest.NextToken := ListResponse.NextToken;
until ListRequest.NextToken = ' ' ;
end ;创建一个主题,获取ARN,设置/获取其属性并删除主题:
var
Response: ICreateTopicResponse;
TopicArn: string;
SetAttrRequest: ISetTopicAttributesRequest;
GetAttrResponse: IGetTopicAttributesResponse;
begin
// create new topic and get ARN
Response := Client.CreateTopic( ' test-name ' );
TopicArn := Response.TopicArn;
// set topic attribute
SetAttrRequest := TSetTopicAttributesRequest.Create(TopicArn, ' DisplayName ' , ' My topic ' );
Client.SetTopicAttributes(SetAttrRequest);
// verify topic attributes
GetAttrResponse := Client.GetTopicAttributes(TopicArn);
DisplayName := GetAttrResponse.Attributes[ ' DisplayName ' ]);
// delete new topic
Client.DeleteTopic(TopicArn);
end ;通过队列URL订阅一个主题为SQS队列,添加适当的权限:
function SubscribeQueue (Client: IAmazonSimpleNotificationService;
const TopicArn: string; SQSClient: IAmazonSQS; const SQSQueueUrl: string): string;
var
GetAttrResponse: IGetQueueAttributesResponse;
GetAttrRequest: IGetQueueAttributesRequest;
SQSQueueArn: string;
Policy: TPolicy;
PolicyStr: string;
TopicArn: string;
SetAttrRequest: ISetQueueAttributesRequest;
begin
// Get the queue's existing policy and ARN
GetAttrRequest := TGetQueueAttributesRequest.Create;
GetAttrRequest.QueueUrl := SQSQueueUrl;
GetAttrRequest.AttributeNames.Add(TSQSConsts.ATTRIBUTE_ALL);
GetAttrResponse := SQSClient.GetQueueAttributes(GetAttrRequest);
SQSQueueArn := GetAttrResponse.Attributes[ ' QueueArn ' ];
if GetAttrResponse.Attributes.TryGetValue( ' Policy ' , PolicyStr) then
Policy := TPolicy.FromJson(PolicyStr)
else
Policy := TPolicy.Create;
try
SetLength(Result, 0 );
if not HasSQSPermission(Policy, TopicArn, SQSQueueArn) then
AddSQSPermission(Policy, TopicArn, SQSQueueArn);
Result := Client.Subscribe(TopicArn, ' sqs ' , SQSQueueArn).SubscriptionArn;
SetAttrRequest := TSetQueueAttributesRequest.Create;
SetAttrRequest.QueueUrl := SQSQueueUrl;
SetAttrRequest.Attributes.Add( ' Policy ' , Policy.ToJson);
SQSClient.SetQueueAttributes(SetAttrRequest);
finally
Policy.Free;
end ;
end ;订阅一个主题以发送电子邮件并返回订阅ARN:
function SubscribeTopic ( const TopicArn, EmailAddress: string): string;
var
Latest: TDateTime;
Response: IListSubscriptionsByTopicResponse;
begin
// subscribe an email address to the topic
Client.Subscribe(TSubscribeRequest.Create(TopicArn, ' email ' , EmailAddress));
// wait until subscription has been confirmed, wait time for two minutes
Latest := IncMinute(Now, 2 );
while Now < Latest do
begin
// get subscriptions for topic
Response := Client.ListSubscriptionsByTopic(TopicArn);
// test whether the subscription has been confirmed
if Response.Subscriptions[ 0 ].SubscriptionArn <> ' PendingConfirmation ' then
Exit(Response.Subscriptions[ 0 ].SubscriptionArn);
// wait
Sleep( 15 * 1000 );
end ;
end ;将消息发布到主题:
// publish a message to the topic
Client.Publish(TPublishRequest.Create(TopicArn, ' Test message ' , ' Subject ' ));删除订阅:
// delete the subscription
Client.Unsubscribe(SubArn);解析JSON的SNS消息并验证签名:
function GetMessage ( const Json: string): AWS.SNS.Message.TMessage;
begin
Result := AWS.SNS.Message.TMessage.ParseMessage(Json);
if not Result.IsMessageSignatureValid then
raise Exception.Create( ' Invalid message: bad signature ' );
end ;将SMS消息发送到电话号码:
var
Client: IAmazonSimpleNotificationService;
Request: IPublishRequest;
begin
Client := TAmazonSimpleNotificationServiceClient.Create;
Request := TPublishRequest.Create;
Request.PhoneNumber := ' +184298765321 ' ;
Request.Message := ' Hello from AWS SDK for Delphi! ' ;
Client.Publish(Request);
end ;Delphi的AWS SDK按一定顺序搜索凭据,并使用第一个可用的设置用于当前应用程序。
TAWSConfigs.AWSProfileName中的值指定。AWS_PROFILE环境变量指定。TAWSConfigs.AWSProfilesLocation属性控制Delphi的AWS SDK如何找到凭据配置文件。如果是空的,它将搜索默认位置中的共享AWS凭据文件。如果不存在配置文件,请搜索~/.aws/config (linux或macOS)或%USERPROFILE%.awsconfig (Windows)。如果TAWSConfigs.AWSProfilesLocation包含AWS凭据文件格式中文件的路径,则SDK仅在指定的文件中搜索具有指定名称的配置文件中的凭据。
请参阅AWS文档以获取有关凭据文件设置和命名配置文件的更多信息。
您可以直接在客户端构造函数中直接传递访问密钥ID和秘密密钥:
Client := TAmazonSQSClient.Create(myAccessKey, mySecretKey);尽管建议使用凭据配置文件,因为它易于管理,并且与AWS命令行接口兼容。
重要的是:不要在生产帐户上运行集成测试。
集成测试可以在AWSTEST项目中找到。这些测试假定已为凭据配置了默认配置文件。您可以使用配置文件,名为配置文件或环境变量设置凭据。
测试旨在创建和删除测试所需的资源,但保持数据的安全很重要。请勿在包含生产数据或资源的帐户上运行这些测试。由于在运行这些测试期间创建和删除了AWS资源,因此可能会发生费用。通过运行测试的重点关注的AWS资源的成本最低,这是通过进行费用而发生的。
一些OpenSSL内部类是从delphi-openssl存储库中启发的:https://github.com/lminuti/delphi-openssl。
单位AWS.Json.Helpers基于Uwe Raabe的REST-JSON-HELPERS:https://github.com/uweraabe/rest-json-helpers。
Delphi的AWS SDK是带有Commons条款许可证的Apache 2.0下的公平代码。