Python/Flask, PostgreSQL, and S3 integration



我正在为我的摄影创建一个网站。图像(位于较大一侧(存储在S3存储桶中,该存储桶不具有公共访问权限。与图像相关的数据,如图像名称、拍摄位置和S3 URL,存储在PostgreSQL表中。

当客户端请求一个或多个图像时,我的计划是查询表中的图像对象,获取S3 URL,使用所述S3 URL获取文件,并使用jpeg(或jpegs(+postgres实例中的数据来响应客户端。

但现在我已经到了能够做到这一点的地步,我不知道该怎么做。要实现这一点,响应对象会是什么样子?我习惯于通过公开的URL提供图像,并将其放入元素或静态资产中。

编辑:我的S3对象是否应该是公共的,这样我就可以按照";我习惯了"?如果是这样的话,我该如何防止过度阅读并提高我的账单?我看到AWS内置了节流功能,但我想节流来自单个客户端的读取,而不是整个存储桶的读取。

此代码取自boto3文档:https://boto3.amazonaws.com/v1/documentation/api/latest/guide/s3-presigned-urls.html

输出包含s3文件的url:

import boto3
from botocore.exceptions import ClientError
def create_presigned_url(bucket_name, object_name, expiration=3600):
"""Generate a presigned URL to share an S3 object
s3_client = boto3.client('s3')
try:
response = s3_client.generate_presigned_url(
'get_object',
Params={'Bucket':bucket_name,'Key':object_name},
ExpiresIn=expiration
)
except ClientError as e:
return None
# The response contains the presigned URL
return response

最新更新