滚动新闻:
当前位置:FX168财经网 >> 财经 > >正文

世界快播:关于aws-s3-bucket-静态网站托管相关的查询-S3.Client.get_bucket_website(**kwargs)

时间:2023-03-25 13:11:34  |  来源: 博客园

关于aws-s3-bucket-静态网站托管相关的查询,这里笔者主要整理一下基于AWS SDK for Python (Boto3)方式,相关的判断和逻辑

1、首先是判断s3-bucket有没有开启静态网站托管,即页面上的 Static website hosting的值Enabled|Disabled

可以通过ResponseMetadata=S3client.get_bucket_website(Bucket=Name)看是否会抛出异常,如果一个s3 bucket没有开启静态网站则会输出


(相关资料图)

botocore.exceptions.ClientError: An error occurred (NoSuchWebsiteConfiguration) when calling the GetBucketWebsite operation:

The specified bucket does not have a website configuration, 判断逻辑如下图所示

2、接下来就是托管类型的判断,Hosting type,目前只要开启了静态网站托管,

就必须从(Host a static website|Redirect requests for an object)选择一种,笔者这里是一个Host a static website类型的,于是能得到如下的返回信息:

{"ResponseMetadata": {"RequestId": "2SR4CJWMC8VM85NX", "HostId": "6Y9F6aMU+57rKMlzmkvLtkI3zf6W8PyFDuFIk0lAHlg2UvBT/QwlYcOcfHxvV3psaRPsefNS2Rg=", "HTTPStatusCode": 200, "HTTPHeaders": {"x-amz-id-2": "6Y9F6aMU+57rKMlzmkvLtkI3zf6W8PyFDuFIk0lAHlg2UvBT/QwlYcOcfHxvV3psaRPsefNS2Rg=", "x-amz-request-id": "2SR4CJWMC8VM85NX", "date": "Sat, 25 Mar 2023 03:13:22 GMT", "content-type": "application/xml", "transfer-encoding": "chunked", "server": "AmazonS3"}, "RetryAttempts": 0}, "IndexDocument": {"Suffix": "index.html"}}

ResponseMetadata数据每次访问都会不一样,真正有效的数据为:"IndexDocument": {"Suffix": "index.html"}

只有托管静态网站类型时才会有/定义IndexDocument,而且必须要有此值, 因此我们就可以通过这个字段来判断托管类型 了,原理与上面的一样

3、对于Redirect requests for an object (重定向对某个对象的请求)类型的返回数据如下,注若页面Protocol为None,则将没有Protocol字段

{"ResponseMetadata": {"RequestId": "V9WW0D0FN889E9CY", "HostId": "rHp0bm+aTgZGZIzxtU2yXQaL5s0JgnmEdvGsRZdzkltPUK1wew6dEuqCSw5ZWeUGIbcSOKxA7m/mBdjcEGB6vQ==", "HTTPStatusCode": 200, "HTTPHeaders": {"x-amz-id-2": "rHp0bm+aTgZGZIzxtU2yXQaL5s0JgnmEdvGsRZdzkltPUK1wew6dEuqCSw5ZWeUGIbcSOKxA7m/mBdjcEGB6vQ==", "x-amz-request-id": "V9WW0D0FN889E9CY", "date": "Sat, 25 Mar 2023 05:07:59 GMT", "content-type": "application/xml", "transfer-encoding": "chunked", "server": "AmazonS3"}, "RetryAttempts": 0}, "RedirectAllRequestsTo": {"HostName": "test.com", "Protocol": "https"}}

4、关于 存储桶网站终端节点(Bucket website endpoint),可以在返回值中是看不出来的,但是对于s3静态网站托管,基名称格式是固定有规律的,如下:

http://{s3-bucket-Name}.s3-website.{Region}.amazonaws.com.cn

尊重别人的劳动成果 转载请务必注明出处:https://www.cnblogs.com/5201351/p/17254489.html

关键词: