Powershell AWS 自动化管理 (8) - CloudFront

这节来看看如何使用 PowerShell 在AWS里面创建CloudFront Distributions.  CloudFront是AWS提供的CDN服务,允许创建一个分布点指向S3 或者Web server,各地的DNS会自动解析到最近的边缘服务器上,以便实现最佳访问速度。


具体的图像界面操作可以参考

http://beanxyz.blog.51cto.com/5570417/1532813 


下面看看PowerShell如何操作。


首先需要有一个S3 bucket(前面已经创建过了),然后我上传一个图片做测试,记得把图片的访问权限设为公共可读

1
2
3
Write-S3Object -BucketName yuanpicture -Key "1.jpg" -File "C:\Users\yli\OneDrive\Pictures\2010-09-28 001\1.jpg"
set-s3acl -BucketName yuanpicture -Key "1.jpg" -PublicReadOnly
get-s3object -BucketName yuanpicture -Key 1.jpg

Powershell AWS 自动化管理 (8) - CloudFront


直接访问看看没问题。


Powershell AWS 自动化管理 (8) - CloudFront


接下来我们需要设置一个cloudfront的分布点指向这个S3 Bucket


设置origin指向S3 Bucket,设置范围为全球,我还设置了一个别名test.beanxyz.com 因为他自动生成的域名实在是太长了


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
$origin New-Object Amazon.CloudFront.Model.Origin
$origin.DomainName="yuanpicture.s3.amazonaws.com"
$origin.id="S3-yuanpicture"
$origin.S3OriginConfig = New-Object Amazon.CloudFront.Model.S3OriginConfig
$origin.S3OriginConfig.OriginAccessIdentity = ""
New-CFDistribution `
      -DistributionConfig_Enabled $true `
      -DistributionConfig_Comment "Test distribution" `
      -Origins_Item $origin `
      -Origins_Quantity 1 `
      -DistributionConfig_CallerReference Client1 `
      -DefaultCacheBehavior_TargetOriginId $origin.Id `
      -ForwardedValues_QueryString $true `
      -Cookies_Forward all `
      -WhitelistedNames_Quantity 0 `
      -TrustedSigners_Enabled $false `
      -TrustedSigners_Quantity 0 `
      -DefaultCacheBehavior_ViewerProtocolPolicy allow-all `
      -DefaultCacheBehavior_MinTTL 1000 `
      -DistributionConfig_PriceClass "PriceClass_All" `
      -CacheBehaviors_Quantity 0 `
      -Aliases_Quantity 1 `
      -Aliases_Item "test.beanxyz.com"


执行命令以后,就开始创建了,大概15分钟后就能用了。


Powershell AWS 自动化管理 (8) - CloudFront

于此同时,在我godaddy的dns上我添加一个别名指向我的cloudfront 域名

Powershell AWS 自动化管理 (8) - CloudFront

等待5分钟之后,查看一下DNS 已经可以成功解析了


Powershell AWS 自动化管理 (8) - CloudFront


访问看看 成功!

Powershell AWS 自动化管理 (8) - CloudFrontPowershell AWS 自动化管理 (8) - CloudFront







本文转自 beanxyz 51CTO博客,原文链接:http://blog.51cto.com/beanxyz/1826315,如需转载请自行联系原作者