-1
import boto3
resource = boto3.resource('s3') 
response = resource.buckets.all()
for buckets in response:
    print(buckets)
    myobjects=resource.list_objects_v2(bucket=buckets)
    for object in myobjects:
        print(object)
luk2302
  • 55,258
  • 23
  • 97
  • 137
  • `list_objects_v2` is available on the `client`, not on the `resource`: https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/s3.html – luk2302 Aug 05 '22 at 10:57
  • Does this answer your question? [Python boto, list contents of specific dir in bucket](https://stackoverflow.com/questions/27292145/python-boto-list-contents-of-specific-dir-in-bucket) – luk2302 Aug 05 '22 at 11:00
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Aug 05 '22 at 12:02

1 Answers1

1

There are two ways of using boto3:

  • client methods map 1:1 with AWS API calls
  • resource methods are more Pythonic

Here is your program using resource methods:

import boto3
s3_resource = boto3.resource('s3') 
buckets = s3_resource.buckets.all()
for bucket in buckets:
    print(bucket.Name)
    objects = bucket.objects.all()
    for object in objects:
        print(object.key)
John Rotenstein
  • 241,921
  • 22
  • 380
  • 470