Created
November 30, 2010 10:32
-
-
Save hayashih/721484 to your computer and use it in GitHub Desktop.
Create CloudFront Distribution from S3 Bucket. 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("Create CloudFront dictributions S3 Origin"); | |
CreateCloudDistributionS3Origin(); | |
Console.WriteLine("Create CloudFront private distribution S3 Origin"); | |
CreateCloudFrontPriveteContentDistributionS3Origin(); | |
Console.WriteLine("Finish"); | |
} | |
Console.WriteLine("Press any key to continue..."); | |
Console.Read(); | |
} | |
static void CreateCloudDistributionS3Origin() | |
{ | |
try | |
{ | |
CreateDistributionRequest req = new CreateDistributionRequest(); | |
S3Origin origin = new S3Origin(); | |
origin.DNSName = "S3BUCKET_DNS_NAME"; | |
CloudFrontDistributionConfig config = new CloudFrontDistributionConfig(); | |
config.S3Origin = origin; | |
config.Enabled = true; | |
req.WithDistributionConfig(config); | |
var response = cfClient.CreateDistribution(req); | |
} | |
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); | |
} | |
} | |
static void CreateCloudFrontPriveteContentDistributionS3Origin() | |
{ | |
try | |
{ | |
CreateDistributionRequest req = new CreateDistributionRequest(); | |
CloudFrontOriginAccessIdentity oai = new CloudFrontOriginAccessIdentity(); | |
oai.Id = "ORIGIN_ACCESS_IDENTITY_ID"; | |
oai.S3CanonicalUserId = "S3_CANONICAL_USER_ID"; | |
S3Origin origin = new S3Origin(); | |
origin.DNSName = "S3BUCKET_DNS_NAME"; | |
origin.OriginAccessIdentity = oai; | |
CloudFrontDistributionConfig config = new CloudFrontDistributionConfig(); | |
config.S3Origin = origin; | |
config.Enabled = true; | |
config.Comment = "Privete Content Distribution"; | |
req.WithDistributionConfig(config); | |
var response = cfClient.CreateDistribution(req); | |
CloudFrontDistribution distribution = response.Distribution; | |
String xml = response.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