I'm trying to create a s3 bucket but I keep getting this error code:
File "/home/ec2-user/environment/homework_1.py", line 64, in create_bucket region = self.bucket.meta.client.meta.region_name AttributeError: 'NoneType' object has no attribute 'bucket'
My code is as follows:
def create_bucket(self=None, region_override=None):
"""Create an Amazon S3 bucket in the default Region for the account or in the
specified Region."""
logger = logging.getLogger(__name__)
bucket = None
if region_override is not None:
region = region_override
else:
region = self.bucket.meta.client.meta.region_name
try:
self.bucket.create(CreateBucketConfiguration={'LocationConstraint': region})
self.bucket.wait_until_exists()
logger.info("Created bucket '%s' in region=%s", self.bucket.name, region)
except ClientError as error:
logger.exception("Couldn't create bucket named '%s' in region=%s.", self.bucket.name, region)
raise error
I've tried to run the code without that particular line in the code thinking that would fix it but ended up with the same error message on "self.bucket.create(CreateBucketConfiguration={'LocationConstraint': region})". I'm new to cloud programming. Can someone help?
It looks like you need to use this function as a method of a class where self refers to the class instantiation. Eg:
import boto3
class YourClass:
def __init__(self, bucket_name):
self.bucket_name = bucket_name
self.bucket = boto3.resource('s3').Bucket(self.bucket_name)
def create_bucket(self, region_override=None):
... your function code
Then instantiate your class and call the function like this:
myBucketName = "SomeNewBucket"
myBucketClass = YourClass(myBucketName)
myBucketClass.create_bucket()
For documentation see: https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/s3.html#bucket