简体   繁体   English

从 AWS S3 Python 加载/访问文件夹的所有图像

[英]Load/ Access all images of a folder from AWS S3 Python

I want to load, access to manipulate with all images in a folder hosted on AWS S3.我想加载、访问以操作 AWS S3 上托管的文件夹中的所有图像。 At local, i use path='foldername'.在本地,我使用 path='foldername'。 In AWS, i used this:在 AWS 中,我使用了这个:

import boto3
s3 = boto3.client('s3', aws_access_key_id='XXXX', aws_secret_access_key= 'XXXX', region_name='xxx')
obj = s3.get_object(Bucket='xxx', Key='foldername/')
path= obj.load()

But it's cause an error:但这会导致错误:

botocore.errorfactory.NoSuchKey: An error occurred (NoSuchKey) when calling the GetObject operation: The specified key does not exist.

It appears that you want the images to appear on a web page, while keeping them secure.您似乎希望图像出现在网页上,同时保证它们的安全。

You can do this by using <img src=...> tags, with the URL generated as an Amazon S3 pre-signed URL , which is a time-limited URL that provides temporary access to a private object.您可以使用<img src=...>标签来执行此操作,并将 URL 生成为Amazon S3 预签名 URL ,这是一个提供对私有对象的临时访问的时间限制 URL。

However, your back-end code would need to know the name of each object to generate the pre-signed URL and the <img> tags in the HTML.但是,您的后端代码需要知道每个对象的名称才能生成预签名 URL 和 HTML 中的<img>标记。 To do that, the back-end code should call list_objects_v2() to obtain a list of Keys (filenames).为此,后端代码应调用list_objects_v2()以获取密钥(文件名)列表。 The code can then loop through the results and generate the <img> tags.然后代码可以遍历结果并生成<img>标记。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM