Learning Python Network Programming

(Sean Pound) #1
Chapter 3

Downloading also follows a similar pattern. Try the following commands:





bucket = conn.get_bucket('mybucket.example.com')








key = bucket.get_key('parrot.txt')








key.get_contents_to_filename('~/parrot.txt')





This downloads the parrot.txt S3 object in the mybucket.example.com bucket and
then stores it in the ~/parrot.txt local file.


Once we have a reference to the key, just use the following to set the ACL:





key.set_acl('public-read')





I'll leave you to further explore the boto package's functionality with the help of
the tutorial, which can be found at https://boto.readthedocs.org/en/latest/
s3_tut.html.


It should be evident that for everyday S3 work in Python, boto should be your go
to package.


Wrapping up with S3


So, we've discussed some of the uses of the Amazon S3 API, and learned some things
about working with XML in Python. These skills should give you a good start in
working with any XML based REST API, whether or not it has a pre-built library
like boto.


However, XML isn't the only data format that is used by web APIs, and the S3 way
of working with HTTP isn't the only model used by web APIs. So, we're going to
move on and take a look at the other major data format in use today, JSON and
another API: Twitter.


JSON


JavaScript Object Notation (JSON) is a standard way of representing simple objects,
such as lists and dicts, in the form of text strings. Although, it was originally
developed for JavaScript, JSON is language independent and most languages can
work with it. It's lightweight, yet flexible enough to handle a broad range of data.
This makes it ideal for exchanging data over HTTP, and a large number of web APIs
use this as their primary data format.

Free download pdf