Revanth
Revanth

Reputation: 99

How to get the URL of all the objects in our aws s3 bucket programmatically using python?

I am posting this here because I found it really hard to find the function to get all objects from our s3 bucket using python. When I tried to find get_object_data function, I was directed to downloading the object function. So, how do we get the data of all the objects in our AWS s3 bucket using boto3(aws sdk for python)?

Upvotes: 1

Views: 2615

Answers (1)

Revanth
Revanth

Reputation: 99

  1. import boto3 to your python shell
  2. make a connection to your AWS account and specify the resource(s3-bucket here) you want to access? (make sure that the IAM credentials you are giving have access to that resource)
  3. get the data required

The code looks something like this

import boto3
s3_resource = boto3.resource(service_name='s3',
                             region_name='<your bucket region>'
                             aws_access_key_id='<your access key id>'
                             aws_secret_access_key='<your secret access key>')
a = s3_resource.Bucket('<your bucket name>') 
for obj in a.objects.all():
    #object URL
    print("https://<your bucket name>.s3.<your bucket region>.amazonaws.com/" + obj.key)
    #if you want to print all the data of object, just print obj

Upvotes: 4

Related Questions