Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

I want to extract only the Aws s3 bucket name and display it in the form of list

Hi I am using boto & boto3 library to read the names of AWS s3 buckets. I was successful to access aws s3 bucket using my access key and secret access key by using following code.

import boto
from boto.s3.key import Key
import boto.s3.connection
from __future__ import print_function

ACCESS_KEY_ID ='****'
SECRET_ACCESS_KEY ='****'

conn = boto.s3.connect_to_region('us-**',
                                 aws_access_key_id=ACCESS_KEY_ID, aws_secret_access_key=SECRET_ACCESS_KEY)
ls ={}
ls = conn.get_all_buckets()
print(ls)

It is successfully returning the list of buckets in my account as

 Out[39]:  [<Bucket: aaaa>, <Bucket: bbbb>]

However, I just wanted the bucket name in terms of list like as follows

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

[aaaa,bbbb]

How can I achieve this? is there any way that I can get the bucket names in terms of list?

>Solution :

You are currently returning a list of Bucket objects. You just need to access the name field for each object and construct a list of the names.

Try this:

conn = boto.s3.connect_to_region('us-**',
                                 aws_access_key_id=ACCESS_KEY_ID, aws_secret_access_key=SECRET_ACCESS_KEY)
ls = list(map(lambda bucket: bucket.name, conn.get_all_buckets()))
print(ls)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading