可以使用 boto3 中的 generate_presigned_url() 方法来创建一个预签名 URL。以下是一个示例代码:
import boto3
from botocore.client import Config
# Instantiate an S3 client using localstack
s3 = boto3.client('s3', endpoint_url='http://localhost:4566', aws_access_key_id='test', aws_secret_access_key='test', config=Config(signature_version='s3v4'))
# Set an expiration time for the URL
expiration = 3600
# Generate a presigned URL for a specified S3 object
presigned_url = s3.generate_presigned_url('get_object', Params={'Bucket': 'my_bucket', 'Key': 'my_object'}, ExpiresIn=expiration)
print(presigned_url)
上述代码中,我们实例化了一个 S3 客户端并设置了本地 AWS 的端点 URL、访问密钥 ID 和密钥。然后,我们设置了 URL 的过期时间,并使用 generate_presigned_url() 方法创建一个预签名 URL,然后打印出来。
上一篇:AWSS3(带有SDK2.x)-是否有可用的上传文件到S3选项(低内存消耗且无临时文件)?
下一篇:AWSS3:Bucket无法使用ObjectOwnership的BucketOwnerEnforced设置设置ACL。