使用 python boto3 使用 s3 和 cloudfront 部署静态站点

Deploying static site with s3 and cloudfront using python boto3

尝试使用 boto3 自动部署静态网站。我有一个位于存储桶中的静态网站 (angular/javascript/html),需要使用 aws cloudfront CDN。

无论如何,看起来制作 s3 存储桶并在 html/js 中复制工作正常。

import boto3
cf = boto3.client('cloudfront')

cf.create_distribution(DistributionConfig=dict(CallerReference='firstOne',
            Aliases = dict(Quantity=1, Items=['mydomain.com']),
            DefaultRootObject='index.html',
            Comment='Test distribution',
            Enabled=True,
            Origins = dict(
                Quantity = 1, 
                Items = [dict(
                    Id = '1',
                    DomainName='mydomain.com.s3.amazonaws.com')
                ]),
            DefaultCacheBehavior = dict(
                TargetOriginId = '1',
                ViewerProtocolPolicy= 'redirect-to-https',
                TrustedSigners = dict(Quantity=0, Enabled=False),
                ForwardedValues=dict(
                    Cookies = {'Forward':'all'},
                    Headers = dict(Quantity=0),
                    QueryString=False,
                    QueryStringCacheKeys= dict(Quantity=0),
                    ),
                MinTTL=1000)
            )
)

当我尝试创建云端分发时,出现以下错误:

InvalidOrigin: An error occurred (InvalidOrigin) when calling the CreateDistribution operation: The specified origin server does not exist or is not valid. An error occurred (InvalidOrigin) when calling the CreateDistribution operation: The specified origin server does not exist or is not valid.

有趣的是,它看起来是在抱怨来源,mydomain.com.s3.amazonaws.com,但是当我在 Web 控制台中为 s3 存储桶创建一个分发时,它没有问题原域名。

更新: 我可以通过以下方式使它与 boto 一起使用,但宁愿使用 boto3:

import boto
c = boto.connect_cloudfront()
origin = boto.cloudfront.origin.S3Origin('mydomain.com.s3.amazonaws.com')
distro = c.create_distribution(origin=origin, enabled=False, comment='My new Distribution')

原来他们是必需的参数,但没有正确记录。

由于 Origin 是一个 S3 存储桶,您必须定义 S3OriginConfig = dict(OriginAccessIdentity = '') 即使没有使用 OriginAccessIdentity,并且是一个空字符串。

以下命令有效。请注意,您仍然需要一个存储桶策略来使对象可访问,并且需要一个 route53 条目来为我们想要云端生成的主机名的 cname 取别名。

cf.create_distribution(DistributionConfig=dict(CallerReference='firstOne',
            Aliases = dict(Quantity=1, Items=['mydomain.com']),
            DefaultRootObject='index.html',
            Comment='Test distribution',
            Enabled=True,
            Origins = dict(
                Quantity = 1, 
                Items = [dict(
                    Id = '1',
                    DomainName='mydomain.com.s3.amazonaws.com',
                    S3OriginConfig = dict(OriginAccessIdentity = ''))
                ]),
            DefaultCacheBehavior = dict(
                TargetOriginId = '1',
                ViewerProtocolPolicy= 'redirect-to-https',
                TrustedSigners = dict(Quantity=0, Enabled=False),
                ForwardedValues=dict(
                    Cookies = {'Forward':'all'},
                    Headers = dict(Quantity=0),
                    QueryString=False,
                    QueryStringCacheKeys= dict(Quantity=0),
                    ),
                MinTTL=1000)
            )
)