すらいむがあらわれた

こまんど >  たたかう  にげる

AWS SDK for .NETでCloudFront Origin Access Identityを扱う

CloudFront Origin Access IdentityはS3上のオブジェクト(ファイルなど)にアクセスするためのCloudFront用のアクセス権です。

詳しい解説は以下のドキュメント参照。
Amazon CloudFront Developer Guide
Serving Private Contentの
Distributions and CloudFront Origin Access Identities の項目
http://docs.amazonwebservices.com/AmazonCloudFront/latest/DeveloperGuide/index.html?PrivateContent.html#DistributionsAndOAIs

Amazon CloudFront API Reference
Actions on Origin Access Identities
http://docs.amazonwebservices.com/AmazonCloudFront/latest/APIReference/index.html?Actions_OAI.html

AWS SDK for .NETでOrigin Access Identitie(OAI)を扱うサンプルコードはこちら。
https://gist.github.com/raw/721449/f0df89721e4fc6950f2345f41550702791764498/CloudFrontOriginAccessIdentity.cs

・CreateCloudFrontOAI
OAIを作成します。

                                                                                                    • -

var res = cfClient.CreateOriginAccessIdentity(req);
CloudFrontOriginAccessIdentity id = res.OriginAccessIdentity;

Console.WriteLine("OAI id:" + id.Id);
Console.WriteLine("OAI S3CanonicalUserId:" + id.S3CanonicalUserId);

                                                                                                    • -

の個所で、作成したOAIの値を表示させています。
・id 
OAI自体のidです。OAIの情報を取得、変更、削除などしたい場合に必要となります。
・S3CanonicalUserId 
S3のオブジェクトのアクセス権設定の際に使うユーザーIDです。

・GetCloudFrontOAI
requestのidで指定したOAiの情報を取得します。

                                                                                                    • -

GetOriginAccessIdentityInfoResponse response = cfClient.GetOriginAccessIdentityInfo(request);

Console.WriteLine("Etag: " + response.Headers["Etag"]);

                                                                                                    • -

responseヘッダでEtagが返ります。これが重要です。
OAIの変更、削除を行う場合、そのOAIの現在のEtagの値をrequestのヘッダに入れて送信しなくてはなりません。
そのためにここで取得します。

・SetCloudFrontOAI
OAIの情報を変更します。

                                                                                                    • -

SetOriginAccessIdentityConfigRequest request = new SetOriginAccessIdentityConfigRequest();
request.Id = "ORIGIN_ACCESS_IDENTITY_ID";
request.ETag = "Etag Value";

                                                                                                    • -

requestを送信する際にIdの他、Etagの設定が必要です。
このEtagが適切でないとrequestは受け付けられません。

                                                                                                    • -

SetOriginAccessIdentityConfigResponse response = cfClient.SetOriginAccessIdentityConfig(request);

Console.WriteLine("Etag: " + response.Headers["Etag"]);

                                                                                                    • -

このメソッドでもresponseヘッダでEtagが返ります。

・DeleteCloudFrontOAI
OAIを削除します。

                                                                                                    • -

DeleteOriginAccessIdentityRequest request = new DeleteOriginAccessIdentityRequest();
request.Id = "ORIGIN_ACCESS_IDENTITY_ID";
request.ETag = "ETag Value";

                                                                                                    • -

requestを送信する際にIdの他、Etagの設定が必要です。
このEtagが適切でないとrequestは受け付けられません。

・ListCloudFrontOAI
登録されているOAIのリストを取得します。