techman
techman

Reputation: 433

get all available buckets and print but only with bucket name

Im showing all available buckets with code below, and Im having this result:

<Bucket: test>

But do you know if its possible have only this result (without <Bucket...>, like this:

test

import boto
from boto.s3.connection import S3Connection
s3 = boto.connect_s3()  
buckets = s3.get_all_buckets() 
for key in buckets:
    print key

Upvotes: 3

Views: 10189

Answers (2)

R2AD
R2AD

Reputation: 31

I wrote up this sample code today, to test out a few things....you may find it helpful as well. This assumes that you have authorization to execute the S3 function or to list the specific bucket:

import boto3
import time
import sys

print ("S3 Listing at %s" % time.ctime())
s3 = boto3.client('s3');

def showSingleBucket( bucketName ):
  "Displays the contents of a single bucket"
  if ( len(bucketName) == 0 ):
    print ("bucket name not provided, listing all buckets....") 
    time.sleep(8)
  else:
    print ("Bucket Name provided is: %s" % bucketName)
    s3bucket = boto3.resource('s3')
    my_bucket = s3bucket.Bucket(bucketName)
    for object in my_bucket.objects.all():
      print(object.key)
  return

def showAllBuckets():
  "Displays the contents of S3 for the current account"
  try:
    # Call S3 to list current buckets
    response = s3.list_buckets()
    for bucket in response['Buckets']:
      print (bucket['Name']) 
  except ClientError as e:
    print("The bucket does not exist, choose how to deal with it or raise the exception: "+e)
  return

if ( len(sys.argv[1:]) != 0 ):
  showSingleBucket(''.join(sys.argv[1]))
else:
  showAllBuckets()

Upvotes: 2

Siddarth
Siddarth

Reputation: 1020

import boto
from boto.s3.connection import S3Connection
s3 = boto.connect_s3()  
buckets = s3.get_all_buckets() 
for key in buckets:
    print key.name

This should work.. key.name

Upvotes: 7

Related Questions