如何使用Boto3创建预签名URL以从S3存储桶下载文件? [英] How do I create a Presigned URL to download a file from an S3 Bucket using Boto3?

查看:88
本文介绍了如何使用Boto3创建预签名URL以从S3存储桶下载文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须将文件从我的 S3存储桶下载到服务器上,以便进行某些处理.存储桶不支持直接连接,必须使用 Pre-Signed URL .

I have to download a file from my S3 bucket onto my server for some processing. The bucket does not support direct connections and has to use a Pre-Signed URL.

Boto3 Docs 谈论使用预先签名的URL进行上传,但没有提及要下载的URL.

The Boto3 Docs talk about using a presigned URL to upload but do not mention the same for download.

推荐答案

import boto3

s3_client = boto3.client('s3')

BUCKET = 'my-bucket'
OBJECT = 'foo.jpg'

url = s3_client.generate_presigned_url(
    'get_object',
    Params={'Bucket': BUCKET, 'Key': OBJECT},
    ExpiresIn=300)

print(url)

有关另一个示例,请参见:预签名的URL— Boto 3文档

For another example, see: Presigned URLs — Boto 3 documentation

您还可以使用AWS CLI生成预签名URL:

You can also generate a pre-signed URL using the AWS CLI:

aws s3 presign s3://my-bucket/foo.jpg --expires-in 300

请参阅:前缀-AWS CLI命令参考

这篇关于如何使用Boto3创建预签名URL以从S3存储桶下载文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆