Creating a bucket with the S3 API

In this section, we are going to write a script that will enable us to interact with the service and create a bucket with the S3 API. To create a bucket, you'll need to use the requests_aws4auth package and the aws4auth method with your AWS credentials, <ACCESS_ID> and <ACCESS_KEY>. Also, you need to specify the <REGION> with the AWS region of your choice:

>>> import requests
>>> import requests_aws4auth
>>> auth = requests_aws4auth.AWS4Auth('<ACCESS_ID>', '<ACCESS_KEY>', 'eu-west-2', 's3')

Whenever we write a client for an API, our main point of reference is the API documentation. The documentation tells us how to construct the HTTP Requests for performing operations.

http://docs.aws.amazon.com/AmazonS3/latest/API/RESTBucketPUT.html provides the details of bucket creation. This documentation tells us that to create a bucket, we need to make an HTTP Request to our new bucket's endpoint by using the HTTP PUT method:

We'll use this in conjunction with Requests to add AWS authentication to our API requests. The ns variable is a string that represents the namespace, which we'll need to work with XML from the S3 API:

ns = 'http://s3.amazonaws.com/doc/2006-03-01/'

You can see that the script will create a bucket from the command-line arguments and so calls the create_bucket() function, passing myBucket as an argument.

You can find the following code in the s3_create_bucket.py file:

import xml.etree.ElementTree as ET

def create_bucket(bucket):
print(bucket)
XML = ET.Element('CreateBucketConfiguration')
XML.attrib['xmlns'] = ns
location = ET.SubElement(XML, 'LocationConstraint')
location.text = auth.region
data = ET.tostring(XML, encoding='utf-8')
url = 'http://{}.{}'.format(bucket,endpoint)
xml_pprint(data)
response = requests.put(url, data=data, auth=auth)
print(response)
if response.ok:
print('Created bucket {} OK'.format(bucket))
else:
xml_pprint(response.text)

We can create a method for printing the XML output:

import xml.dom.minidom as minidom

def xml_pprint(xml_string):
print(minidom.parseString(xml_string).toprettyxml())

For creating a bucket, we can see that it creates an XML tree with the format that is available in the S3 documentation. If you run the script, then you will see the XML shown here:

$ python3 s3_create_bucket.py mybucket
<?xml version="1.0" ?>
<CreateBucketConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
<LocationConstraint>eu-west-2</LocationConstraint>
</CreateBucketConfiguration>

This matches the format specified in the documentation. You can see that we've used the ns variable to fill the xmlns attribute. This is the code that executes the put request:

url = 'http://{}.{}'.format(bucket, endpoint)
response = requests.put(url, data=data, auth=auth)
if response.ok:
print('Created bucket {} OK'.format(bucket))
else:
xml_pprint(response.text)

The first line shown here will generate the full URL from our bucket name and endpoint. The second line will make the put request to the S3 API. Also, note that we have supplied our auth object to the call. This will allow Requests to handle all the S3 authentication for us.

If all goes well, then we print out a message. In case everything does not go as expected, we print out the response body. S3 returns error messages as XML in the response body. So we use our xml_pprint() function to display it.

When we refresh the S3 Console in our browser, we will see that our bucket has been created:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset