'Retrieves a bucket if it exists, otherwise creates it.'

def _get_or_create_bucket(self, name):

    bucket = self.connection.Bucket(name)
    if self.auto_create_bucket:
        try:
            bucket.meta.client.head_bucket(Bucket=name)
        except self.connection_response_error as err:
            if (err.response['ResponseMetadata']['HTTPStatusCode'] == 301):
                raise ImproperlyConfigured(('Bucket    %s    exists,    but    in    a    different    region    than    we    are    connecting    to.    Set    the    region    to    connect    to    by    setting    AWS_S3_REGION_NAME    to    the    correct    region.' % name))
                bucket_params = {'ACL': self.bucket_acl}
                region_name = self.connection.meta.client.meta.region_name
                if (region_name != 'us-east-1'):
                    bucket_params['CreateBucketConfiguration'] = {'LocationConstraint': region_name}
                bucket.create(ACL=self.bucket_acl)
            else:
                raise ImproperlyConfigured(('Bucket    %s    does    not    exist.    Buckets    can    be    automatically    created    by    setting    AWS_AUTO_CREATE_BUCKET    to    ``True``.' % name))
    return bucket
