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下的公平代碼。