A photo upload is one web request
Cloud computing means renting ready-made services, such as file storage, from a provider over the web. Most apps you use store their data this way, so knowing how a call works is basic to the job.
A shop lets customers upload a photo of a faulty item. The photo is stored in an S3 bucket, a place for files in the cloud. Your code does not log in to a machine to save it. It sends a request over the web and reads an answer.
By the end of this lesson
Note
I can say what a region, a service and an API call are.
Key words
| Word | What it means | Example |
|---|---|---|
| Service | a ready-made tool you use over the web | |
| API call | one request to a service, and its answer | |
| Region | the part of the world where a service runs | eu-west-2 |
| Bucket | a named container for files in S3 | |
| Shared responsibility | the provider and the customer each look after part of the system |
The cells students fill in are left blank in this preview.
What this module is not
Note
You do not need an AWS account and nothing is deployed. You will never type a real key, password or account number. This is not a lesson on cost or billing. You read answers that are shaped like the real ones and write Python that handles them, offline.
A service is an API you call
A cloud provider runs big computers and rents out what they do. Each rented tool is a service. S3 stores files. SQS holds messages in a queue. DynamoDB stores items in tables.
You use a service by making an API call: a request that names the call, such as list_objects_v2, and the details it needs. The answer comes back as data.
Run: one call and its answer
call = {"service": "s3", "operation": "list_objects_v2", "Bucket": "shop-photos"}
answer = {
"Name": "shop-photos",
"KeyCount": 2,
"IsTruncated": False,
"Contents": [
{"Key": "faulty/kettle.png", "Size": 2048, "StorageClass": "STANDARD"},
{"Key": "faulty/toaster.png", "Size": 3072, "StorageClass": "STANDARD"},
],
}
print("service:", call["service"])
print("call:", call["operation"])
print("keys:", answer["KeyCount"])
for item in answer["Contents"]:
print(item["Key"], item["Size"])
# Change the Size of the toaster to 5000, then press Run again.
A failed call is still an answer
Not every call works. A key may be missing or a bucket may not exist. Then the service answers with an error instead: a Code that names the problem and an HTTP status.
Your code must look for the error first. Code such as "Contents" in answer only works when the call worked.
Run: tell a good answer from an error
good = {"KeyCount": 1, "Contents": [{"Key": "readme.txt", "Size": 5}]}
bad = {"error": {"Code": "NoSuchBucket", "HTTPStatusCode": 404}}
for answer in [good, bad]:
if "error" in answer:
print("failed:", answer["error"]["Code"], answer["error"]["HTTPStatusCode"])
else:
print("worked, keys:", answer["KeyCount"])
# Change "NoSuchBucket" to "NoSuchKey", then press Run again.
A common mistake
Warning
Many people think an empty answer means an error. It does not. Asking DynamoDB for an item that is not there gives {} and a good status. Look for an error entry, with its Code, and treat an empty answer as "found nothing".
A region is where a service runs
A provider has data centres in many places. A region is one area of them, such as eu-west-2, which is London. You choose the region when you make a bucket, and the files stay there.
Pick a region near your users, so answers arrive quickly. The answer that lists your buckets also shows the region of each one.
Run: read the region of each bucket
answer = {
"Buckets": [
{"Name": "shop-photos", "BucketRegion": "eu-west-2", "BucketArn": "arn:aws:s3:::shop-photos"},
{"Name": "shop-backups", "BucketRegion": "us-east-1", "BucketArn": "arn:aws:s3:::shop-backups"},
],
"Owner": {"DisplayName": "shop", "ID": "owner-id"},
}
for bucket in answer["Buckets"]:
print(bucket["Name"], "is in", bucket["BucketRegion"])
# Print only the buckets in eu-west-2: add an if inside the loop.
The provider and you share the work
The provider looks after the buildings, the machines and the service software. You look after what you put in it and who may use it.
This is shared responsibility. If you let anyone read a bucket, that was your choice. If a data centre loses power, that is the provider's job to handle.
Who looks after what
| Thing | Looked after by |
|---|---|
| The building and the power | The provider |
| The S3 software itself | The provider |
| Which files you upload | You |
| Who may read a bucket | You |
| The code that calls the service | You |
The cells students fill in are left blank in this preview.