Skip to content

Instantly share code, notes, and snippets.

@hayashih
Created November 30, 2010 10:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hayashih/721449 to your computer and use it in GitHub Desktop.
Save hayashih/721449 to your computer and use it in GitHub Desktop.
Control CloudFront Origin Access Identity. Require AWS SDK for .NET http://aws.amazon.com/sdkfornet/
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();
}
/// <summary>
/// POST Origin Access Identity
/// http://docs.amazonwebservices.com/AmazonCloudFront/latest/APIReference/index.html?CreateOAI.html
/// </summary>
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);
}
}
/// <summary>
/// GET Origin Access Identity
/// http://docs.amazonwebservices.com/AmazonCloudFront/latest/APIReference/index.html?GetOAI.html
/// </summary>
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);
}
}
/// <summary>
/// PUT Origin Access Identity Config
/// http://docs.amazonwebservices.com/AmazonCloudFront/latest/APIReference/index.html?PutOAIConfig.html
/// </summary>
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);
}
}
/// <summary>
/// DELETE Origin Access Identity
/// http://docs.amazonwebservices.com/AmazonCloudFront/latest/APIReference/index.html?DeleteOAI.html
/// </summary>
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);
}
}
/// <summary>
/// GET Origin Access Identity List
/// http://docs.amazonwebservices.com/AmazonCloudFront/latest/APIReference/index.html?ListOAIs.html
/// </summary>
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);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment