Created
November 30, 2010 10:18
-
-
Save hayashih/721472 to your computer and use it in GitHub Desktop.
Control CloudFront Object Invalidation. Require AWS SDK for .NET http://aws.amazon.com/sdkfornet/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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("Get Object Invalidation List"); | |
GetObjectInvalidationList(); | |
Console.WriteLine("Get Object Invalidation"); | |
GetObjectInvalidation(); | |
Console.WriteLine("Post Object Invalidation"); | |
PostObjectInvalidation(); | |
Console.WriteLine("Finish"); | |
} | |
Console.WriteLine("Press any key to continue..."); | |
Console.Read(); | |
} | |
/// <summary> | |
/// GET Invalidation List | |
/// http://docs.amazonwebservices.com/AmazonCloudFront/latest/APIReference/index.html?ListInvalidation.html | |
/// </summary> | |
static void GetObjectInvalidationList() | |
{ | |
try | |
{ | |
GetInvalidationListRequest request = new GetInvalidationListRequest(); | |
request.WithDistributionId("CLOUDFRONT_DISTRIBUTION_ID"); | |
GetInvalidationListResponse response = cfClient.GetInvalidationList(request); | |
String resultXml = response.XML; | |
Console.WriteLine(resultXml); | |
} | |
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 Invalidation | |
/// http://docs.amazonwebservices.com/AmazonCloudFront/latest/APIReference/index.html?GetInvalidation.html | |
/// </summary> | |
static void GetObjectInvalidation() | |
{ | |
try | |
{ | |
GetInvalidationRequest request = new GetInvalidationRequest(); | |
request.WithDistribtionId("CLOUDFRONT_DISTRIBUTION_ID") | |
.WithInvalidationId("INVALIDATION_ID"); | |
GetInvalidationResponse response = cfClient.GetInvalidation(request); | |
String resultXml = response.XML; | |
Console.WriteLine(resultXml); | |
} | |
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> | |
/// POST Invalidation | |
/// http://docs.amazonwebservices.com/AmazonCloudFront/latest/APIReference/index.html?CreateInvalidation.html | |
/// </summary> | |
static void PostObjectInvalidation() | |
{ | |
try | |
{ | |
InvalidationBatch batch = new InvalidationBatch(); | |
batch.WithPaths(new string[] { "test/image1.jpg", "test/image2.jpg" }); | |
batch.CallerReference = DateTime.Now.Ticks.ToString(); | |
PostInvalidationRequest request = new Amazon.CloudFront.Model.PostInvalidationRequest(); | |
request.WithDistribtionId("CLOUDFRONT_DISTRIBUTION_ID") | |
.WithInvalidationBatch(batch); | |
PostInvalidationResponse response = cfClient.PostInvalidation(request); | |
String resultXml = response.XML; | |
Console.WriteLine(resultXml); | |
} | |
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