简体   繁体   中英

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. At local, i use path='foldername'. In AWS, i used this:

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.

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. To do that, the back-end code should call list_objects_v2() to obtain a list of Keys (filenames). The code can then loop through the results and generate the <img> tags.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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