Learning Python Network Programming

(Sean Pound) #1
Chapter 3

First we'll set up our command line interpreter and initialize the authentication.
Create a file called s3_client.py and save the following code block in it:


import sys
import requests
import requests_aws4auth as aws4auth
import xml.etree.ElementTree as ET
import xml.dom.minidom as minidom

access_id = '<ACCESS ID>'
access_key = '<ACCESS KEY>'
region = '<REGION>'
endpoint = 's3-{}.amazonaws.com'.format(region)
auth = aws4auth.AWS4Auth(access_id, access_key, region, 's3')
ns = 'http://s3.amazonaws.com/doc/2006-03-01/'

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

def create_bucket(bucket):
print('Bucket name: {}'.format(bucket))

if __name__ == '__main__':
cmd, *args = sys.argv[1:]
globals()[cmd](*args)

Downloading the example code
You can download the example code files for all Packt books you have
purchased from your account at http://www.packtpub.com. If you
purchased this book elsewhere, you can visit http://www.packtpub.
com/support and register to have the files e-mailed directly to you.

You'll need to replace and with the values from the
credentials CSV that we downloaded earlier, and with the AWS region
of your choice.


So, what are we doing here? Well, first we set up our endpoint. An endpoint is a
general term for a URL which is used to access an API. Some web APIs have a single
endpoint, some have many endpoints, it depends on how the API is designed. The
endpoint we generate here is actually only a part of the full endpoint which we'll
use when we work with buckets. Our actual endpoint is the endpoint prefixed by a
bucket name.

Free download pdf