bluethundr
bluethundr

Reputation: 1325

Python3 Boto Create S3 Bucket Already Exists Error

When I try to create an S3 bucket in python3 I get a 'Bucket Already Exists' error. Even if I try using an outlandish bucket name that certainly doesn't exist.

This is the code:

import boto3
# Create an S3 client
s3 = boto3.client('s3')
bucket_name = input("Enter a bucket name: ")
s3.create_bucket(Bucket='bucket_name')

But I get a bucket name already exists error no matter what name I give it:

   PS C:\Users\tdunphy\Desktop\important_folders\git\aws_scripts\python\virtualenvs3\boto3> python3 .\aws_s3_create_bucket.py
Enter a bucket name: company-timd-test-3
Traceback (most recent call last):
  File ".\aws_s3_create_bucket.py", line 9, in <module>
    s3.create_bucket(Bucket='bucket_name')
  File "C:\Users\tdunphy\AppData\Local\Programs\Python\Python37-32\lib\site-packages\botocore\client.py", line 357, in _api_call
    return self._make_api_call(operation_name, kwargs)
  File "C:\Users\tdunphy\AppData\Local\Programs\Python\Python37-32\lib\site-packages\botocore\client.py", line 661, in _make_api_call
    raise error_class(parsed_response, operation_name)
botocore.errorfactory.BucketAlreadyExists: An error occurred 

(BucketAlreadyExists) when calling the CreateBucket operation: The requested bucket name is not available. The bucket namespace is shared by all users of the system. Please select a different name and try again

The bucket name I tried was: company-timd-test3

But if I hard code the name of the s3 bucket in the program like so:

import boto3
# Create an S3 client

s3 = boto3.client('s3')

#bucket_name = input("Enter a bucket name: ")
s3.create_bucket(Bucket='company-timd-test3')

It works and the bucket is created:

aws s3 ls --profile=nonprod | findstr "company*"
2019-02-25 14:00:16 company-timd-test3

What's really wrong and how do I correct this problem?

Upvotes: 2

Views: 1875

Answers (1)

eatsfood
eatsfood

Reputation: 1088

The problem with your code is in this line:

s3.create_bucket(Bucket='bucket_name')

You are explicitly passing the bucket name of 'bucket_name' here, not the variable. I can guarantee that the name, 'bucket_name' is already taken. Your code should look like this (no single quotes):

s3.create_bucket(Bucket=bucket_name)

Upvotes: 2

Related Questions