如何使用Boto3在S3上通过URL访问图像? [英] How to access image by url on s3 using boto3?

查看:564
本文介绍了如何使用Boto3在S3上通过URL访问图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要完成的工作是生成一个链接来查看文件(例如image或pdf).无法通过URL访问该项目( https://[bucket] .s3.amazonaws. com/img_name.jpg ),我认为是因为它是私有的而不是公共的? (我不是存储桶的所有者,但他给了我access_key和secret_key吗?)

What I want to accomplish is to generate a link to view the file (ex.image or pdf). The item is not accessible by URL (https://[bucket].s3.amazonaws.com/img_name.jpg), I think because its private and not public? (I'm not the owner of the bucket, but he gave me the access_key and secret_key?)

目前,我所能做的就是下载具有此代码的文件.

For now, all I can do is to download a file with this code.

s3.Bucket('mybucket').download_file('upload/nocturnes.png', 'dropzone/static/pdf/download_nocturnes.png') 

我想访问s3上的图像,以便可以将其放在HTML上,是否可以使用访问权和密钥查看它?谢谢您的帮助!

I want to access an image on s3 so I can put it on an HTML, can I view it using the access and secret key?. Thank you for those who can help!

推荐答案

您可以使用

You can accomplish this using a pre-signed URL using the generate_presigned_url function. The caveat is that pre-signed URLs must have an expiration date. I was unable to find documentation on the maximum allowed duration. Here is an example:

url = s3.generate_presigned_url('get_object',
                                Params={
                                    'Bucket': 'mybucket',
                                    'Key': 'upload/nocturnes.png',
                                },                                  
                                ExpiresIn=3600)
print url

这篇关于如何使用Boto3在S3上通过URL访问图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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