Boto API call is returning "The specified bucket does not exist" even though I can see it in the console and list it using Boto

StackOverflow https://stackoverflow.com/questions/22578617

Question

For some background, this code is part of a larger script. I'm currently using the .boto config file for my creds. I am able to list all buckets using the boto API call as well as create buckets, however, this section of code is giving me some problems, and it looks spot on (the code itself was actually working in a previous version and now it seems to be getting hung up).

#!/usr/bin/python
import boto
from boto.s3.connection import S3Connection

conn = S3Connection()
n = "zapbucketx"
rs = conn.get_all_buckets()
for b in rs:
    if b.name == n:
                print b.name + n
                try:
                        conn.delete_bucket('n')
                        print "Bucket did not contian keys and was deleted!"
                except:

                        print "WARNING: Bucket Contains Keys!"
                        ans = raw_input("Do you want to continue? (y/n)")
                        mod_ans = str(ans.upper())
                        if mod_ans == 'Y':
                                        #print "got here"
                                full_bucket = conn.get_bucket(n)
                                print "[+] Deleting keys..."
                                for key in full_bucket.list():
                                        key.delete()
                                print "[+] Keys deleted, Deleting Bucket"
                                conn.delete_bucket(n)
                                print "[+] Bucket deleted"
                                exit(0)
                        elif mod_ans == 'N':
                                print "No bucket was deleted."
                                exit(0)
                        else:
                                print "Please answer 'y' or 'n'."
                                exit(0)

                        exit(0)
        else:
                print "That bucket does not exist!"
                exit(0)

It seems to not be getting past:

if b.name == n:

As I am getting my own "error" in the "else" statement:

print "That bucket does not exist!"

I have tried the code on different instances and double checked the creds in the file (but since I can list buckets, it seems it is not a creds issue).

Any help would greatly be appreciated, Thanks!

Was it helpful?

Solution

for b in rs:
    if b.name == n:
    ...
    else:
        print "That bucket does not exist!"
        exit(0)

This seems to assume there's only one bucket on the account. If there's more than one bucket, it's a crapshoot whether this code will work correctly: Maybe the bucket zapbucketx will be returned first, maybe it won't.

Seems like what you really want is

else:
    continue

So that the loop will continue through the list of buckets until it finds zapbucketx.

Also, this line is wrong:

conn.delete_bucket('n')

You want conn.delete_bucket(n).

You probably also want an exit after the delete_bucket call, or the loop will continue to the next bucket-name and then complain about bucket-doesn't-exist.

Also, you have a typo in the successful-delete message: "contian"

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top