using System; using System.Collections.Generic; using System.Linq; using System.Text; using Amazon.CloudFront; using Amazon.CloudFront.Model; using System.Collections.Specialized; using System.Configuration; // Require AWS SDK for .NET http://aws.amazon.com/sdkfornet/ namespace AWS_Test { class Program { static string accessKeyID = "Your AccessKey ID"; static string secretAccessKeyID = "Your SecretAccessKey ID"; static AmazonCloudFront cfClient = null; static void Main(string[] args) { using (cfClient = Amazon.AWSClientFactory.CreateAmazonCloudFrontClient(accessKeyID, secretAccessKeyID)) { Console.WriteLine("Create CloudFront Origin Access Identity"); CreateCloudFrontOAI(); Console.WriteLine("Get CloudFront Origin Access Identity"); GetCloudFrontOAI(); Console.WriteLine("Put CloudFront Origin Access Identity"); SetCloudFrontOAI(); Console.WriteLine("Delete CloudFront Origin Access Identity"); DeleteCloudFrontOAI(); Console.WriteLine("List CloudFront Origin Access Identity"); ListCloudFrontOAI(); Console.WriteLine("Finish"); } Console.WriteLine("Press any key to continue..."); Console.Read(); } /// /// POST Origin Access Identity /// http://docs.amazonwebservices.com/AmazonCloudFront/latest/APIReference/index.html?CreateOAI.html /// static void CreateCloudFrontOAI() { try { CloudFrontOriginAccessIdentityConfig config = new CloudFrontOriginAccessIdentityConfig(); config.Comment = "YOUR COMMENT"; CreateOriginAccessIdentityRequest req = new CreateOriginAccessIdentityRequest(); req.OriginAccessIdentityConfig = config; var res = cfClient.CreateOriginAccessIdentity(req); CloudFrontOriginAccessIdentity id = res.OriginAccessIdentity; String xml = res.XML; Console.WriteLine("OAI id:" + id.Id); Console.WriteLine("OAI S3CanonicalUserId:" + id.S3CanonicalUserId); Console.WriteLine(xml); } catch (AmazonCloudFrontException cfEx) { Console.WriteLine("An Error, number {0}, occurred when create cloud distribution with the message '{1}", cfEx.ErrorCode, cfEx.Message); } catch (Exception ex) { Console.WriteLine("UnknownError:{0}", ex.Message); } } /// /// GET Origin Access Identity /// http://docs.amazonwebservices.com/AmazonCloudFront/latest/APIReference/index.html?GetOAI.html /// static void GetCloudFrontOAI() { try { GetOriginAccessIdentityInfoRequest request = new GetOriginAccessIdentityInfoRequest(); request.Id = "ORIGIN_ACCESS_IDENTITY_ID"; GetOriginAccessIdentityInfoResponse response = cfClient.GetOriginAccessIdentityInfo(request); String xml = response.XML; Console.WriteLine(xml); Console.WriteLine("Etag: " + response.Headers["Etag"]); } catch (AmazonCloudFrontException cfEx) { Console.WriteLine("An Error, number {0}, occurred when create cloud distribution with the message '{1}", cfEx.ErrorCode, cfEx.Message); } catch (Exception ex) { Console.WriteLine("UnknownError:{0}", ex.Message); } } /// /// PUT Origin Access Identity Config /// http://docs.amazonwebservices.com/AmazonCloudFront/latest/APIReference/index.html?PutOAIConfig.html /// static void SetCloudFrontOAI() { try { CloudFrontOriginAccessIdentityConfig config = new CloudFrontOriginAccessIdentityConfig(); config.Comment = "Change commnet. at " + DateTime.Now.ToString(); // set new comment config.CallerReference = "CALLER REFERENCE"; // set original caller reference SetOriginAccessIdentityConfigRequest request = new SetOriginAccessIdentityConfigRequest(); request.Id = "ORIGIN_ACCESS_IDENTITY_ID"; request.ETag = "Etag Value"; // The value of the ETag header you received from a previous GET or PUT request. request.OriginAccessIdentityConfig = config; SetOriginAccessIdentityConfigResponse response = cfClient.SetOriginAccessIdentityConfig(request); String xml = response.XML; Console.WriteLine(xml); Console.WriteLine("Etag: " + response.Headers["Etag"]); } catch (AmazonCloudFrontException cfEx) { Console.WriteLine("An Error, number {0}, occurred when create cloud distribution with the message '{1}", cfEx.ErrorCode, cfEx.Message); } catch (Exception ex) { Console.WriteLine("UnknownError:{0}", ex.Message); } } /// /// DELETE Origin Access Identity /// http://docs.amazonwebservices.com/AmazonCloudFront/latest/APIReference/index.html?DeleteOAI.html /// static void DeleteCloudFrontOAI() { try { DeleteOriginAccessIdentityRequest request = new DeleteOriginAccessIdentityRequest(); request.Id = "ORIGIN_ACCESS_IDENTITY_ID"; request.ETag = "ETag Value"; // The value of the ETag header you received from a previous GET or PUT request. DeleteOriginAccessIdentityResponse response = cfClient.DeleteOriginAccessIdentity(request); String xml = response.XML; Console.WriteLine(xml); } catch (AmazonCloudFrontException cfEx) { Console.WriteLine("An Error, number {0}, occurred when create cloud distribution with the message '{1}", cfEx.ErrorCode, cfEx.Message); } catch (Exception ex) { Console.WriteLine("UnknownError:{0}", ex.Message); } } /// /// GET Origin Access Identity List /// http://docs.amazonwebservices.com/AmazonCloudFront/latest/APIReference/index.html?ListOAIs.html /// static void ListCloudFrontOAI() { try { ListOriginAccessIdentitiesResponse response = cfClient.ListOriginAccessIdentities(); String xml = response.XML; Console.WriteLine(xml); } catch (AmazonCloudFrontException cfEx) { Console.WriteLine("An Error, number {0}, occurred when create cloud distribution with the message '{1}", cfEx.ErrorCode, cfEx.Message); } catch (Exception ex) { Console.WriteLine("UnknownError:{0}", ex.Message); } } } }