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

Time:12-03

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

[aaaa,bbbb]

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

CodePudding user response:

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)
  • Related