Learning Python Network Programming

(Sean Pound) #1
Chapter 3

Note that we open the local file in binary mode. The file could contain any type
of data, so we don't want text transforms applied. We could pull this data from
anywhere, such as a database or another web API. Here, we just use a local file
for simplicity.


The URL is the same endpoint that we constructed in create_bucket() with the
S3 object name appended to the URL path. Later, we can use this URL to retrieve
the object.


Now, run the command shown here to upload a file:


$ python3.4 s3_client.py mybucket.example.com test.jpg ~/test.jpg


Uploaded ~/test.jpg OK


You'll need to replace mybucket.example.com with your own bucket name. Once
the file gets uploaded, you will see it in the S3 Console.


I have used a JPEG image that was stored in my home directory as the source file.
You can use any file, just change the last argument to an appropriate path. However,
using a JPEG image will make the following sections easier for you to reproduce.


Retrieving an uploaded file through a web browser


By default, S3 applies restrictive permissions for buckets and objects. The account
that creates them has full read-write permissions, but access is completely denied
for anyone else. This means that the file that we've just uploaded can only be
downloaded if the download request includes authentication for our account. If we
try the resulting URL in a browser, then we'll get an access denied error. This isn't
very useful if we're trying to use S3 for sharing files with other people.


The solution for this is to use one of S3's mechanisms for changing the permissions.
Let's look at the simple task of making our uploaded file public. Change
upload_file() to the following:


def upload_file(bucket, s3_name, local_path, acl='private'):
data = open(local_path, 'rb').read()
url = 'http://{}.{}/{}'.format(bucket, endpoint, s3_name)
headers = {'x-amz-acl': acl}
r = requests.put(url, data=data, headers=headers, auth=auth)
if r.ok:
print('Uploaded {} OK'.format(local_path))
else:
xml_pprint(r.text)
Free download pdf