Question:
I have following piece of code, that utilizes list_objects_v2 from boto3. I get an error => keyError : ‘Contents’. I am assuming if name of the file i’m passing doesn’t exist , it throws this error.
1 2 3 4 5 6 7 8 9 |
import boto3 s3_R = boto3.client('s3') s3_b = s3_R.Bucket("MyBucket") response = s3_R.list_objects_v2(Bucket=s3_b, Prefix='myfilename') for obj in response['contents']: file = obj['key'] print(file) |
Answer:
It should be Contents
, not contents
, assuming some objects are returned:
1 2 3 4 5 6 7 8 9 |
response = s3_R.list_objects_v2(Bucket='MyBucket', Prefix='myfilename') if 'Contents' in response: for obj in response['Contents']: file = obj['Key'] print(file) else: print("No objects returned") |