使用python boto3使用s3和cloudfront部署静态站点(Deploying static site with s3 and cloudfront using python boto3)

编程入门 行业动态 更新时间:2024-10-24 04:35:39
使用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) ) )

当我尝试创建cloudfront分发时,我收到以下错误:

InvalidOrigin:调用CreateDistribution操作时发生错误(InvalidOrigin):指定的源服务器不存在或无效。 调用CreateDistribution操作时发生错误(InvalidOrigin):指定的源服务器不存在或无效。

有趣的是,它似乎在抱怨起源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')

Trying to automate the deployment of a static website using boto3. I have a static website (angular/javascript/html) sitting in a bucket, and need to use the aws cloudfront CDN.

Anyway, looks like making the s3 bucket and copying in the html/js is working fine.

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) ) )

When I try to create the cloudfront distribution, I get the following error:

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.

Interestingly, it looks to be complaining about the origin, mydomain.com.s3.amazonaws.com, however when I create a distribution for the s3 bucket in the web console, it has no problem with the same origin domain name.

Update: I can get this to work with boto with the following, but would rather use 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存储桶,因此即使未使用OriginAccessIdentity,也必须定义S3OriginConfig = dict(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) ) )

Turns out their is a required parameter that is not documented properly.

Since the Origin is a S3 bucket, you must have S3OriginConfig = dict(OriginAccessIdentity = '') defined even if OriginAccessIdentity not used, and is an empty string.

The following command works. Note, you still need a bucket policy to make the objects accessible, and a route53 entry to alias the cname we want to cloudfront generated hostname.

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) ) )

更多推荐

本文发布于:2023-04-29 11:38:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1336392.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:静态   站点   python   cloudfront   static

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!