DelphiのAWS SDKにより、 Delphi開発者はAmazon Webサービスと簡単に連携し、Amazon SES、Amazon SQSなどでスケーラブルなソリューションを構築できます。これは、.NETの公式AWS SDKに基づいた非公式のSDKです。サンプルプロジェクトは、Delphiサンプルリポジトリ用のAWS SDKにあります。
これは、SDKが現在サポートしているAWSサービスのリストです。時間の経過とともにさらに追加されます:
GitHubの問題ページを使用して、問題を報告したり、機能を要求したりしてください。
DelphiのAWS SDKは、Delphi 10.3 Rio(つまり、Delphi 10.4 Sydney、Delphi 11、Delphi 12など)以来、すべての最近のDelphiバージョンをサポートし、Delphiバージョンで利用可能なすべてのプラットフォームをサポートしています。
DelphiのAWS SDKは、サードパーティライブラリを必要とせず、100%ネイティブDelphiコードで記述されており、サポートされているDelphiバージョンのすべてのSKUで動作します。
Manuallインストールを実行するライブラリをインストールできます。
さまざまなプラットフォームごとに、SDKをインストールするものをインストールします。
AWSPackages.groupprojプロジェクトグループをDelphi Ideで開きます。<sdk_folder>/packages/d12/Win32/ReleaseフォルダーをDelphi 12、win32ライブラリパスに追加します。各Amazon WebサービスAWS<service>.dprojは、それぞれ独自のパッケージおよびユニット名スキームがあります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 ;すべての要求オブジェクトでは、リストと辞書はすでにインスタンス化されていますが、他の関連するオブジェクトは既にインスタンス化されていません。操作に渡された、または操作から受け取ったインターフェイスまたはオブジェクトを破壊する必要はありません。一部の操作には、より単純な使用のために、フルリクエストオブジェクトの代わりに生のパラメーターが少ないメソッドが過負荷になっています。
利用可能な操作の詳細については、使用しようとしているAmazonサービスのAPIドキュメントを参照してください。または、もちろん、Delphiコードの完了を使用して、各サービスクライアントで利用可能なすべての方法を見つけます。
Amazon SNS:AWS SDK for DelphiのSimple Notification Serviceを完全に使用できます。メインユニットはAWS.SNSです。この記事では、DelphiにAWS SDKを使用してAmazon SNSを使用することに関する詳細情報を見つけることができます。もちろん、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 Environment変数によって指定された名前の資格情報プロファイル。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コマンドラインインターフェイスと互換性があるため、推奨されます。
重要:生産アカウントで統合テストを実行しないでください。
統合テストは、 Awstestsプロジェクトにあります。これらのテストでは、デフォルトのプロファイルが資格情報に対して構成されていると想定しています。プロファイルまたは環境変数という名前の構成ファイルを使用して資格情報を設定できます。
テストは、テストに必要なリソースを作成および削除するように設計されていますが、データを安全に保つことが重要です。これらのテストは、生産データまたはリソースを含むアカウントで実行しないでください。 AWSリソースはこれらのテストの実行中に作成および削除されるため、料金が発生する可能性があります。テストを実行することで料金を削減するために、コストが最小限のAWSリソースに焦点を当てます。
一部のOpenSSL内部クラスは、delphi-OpensSlリポジトリhttps://github.com/lminuti/delphi-opensslからインスピレーションを受けました。
Unit AWS.Json.Helpers 、Uwe RaabeのRest-Json-helpers:https://github.com/uweraabe/rest-json-helpersに基づいています。
AWS SDK for Delphiは、 Commons句ライセンスを使用してApache 2.0に基づいて配布されています。