APIs in Action
S3 buckets and objects
S3 organizes the data that we store in it using two concepts: buckets and objects. An
object is the equivalent of a file, that is, a blob of data with a name, and a bucket is
equivalent to a directory. The only difference between buckets and directories is that
buckets cannot contain other buckets.
Every bucket has its own URL of the form:
In the URL,
region where the bucket is present, for example eu-west-1. The bucket name and
region are set when we create the bucket.
Bucket names are shared globally among all S3 users, and so they must be unique.
If you own a domain, then a subdomain of that will make an appropriate bucket
name. You could also use your email address by replacing the @ symbol with a
hyphen or underscore.
Objects are named when we first upload them. We access objects by adding the
object name to the end of the bucket's URL as a path. For example, if we have a
bucket called mybucket.example.com in the eu-west-1 region containing the
object cheeseshop.txt, then we can access it by using the URL http://mybucket.
example.com.s3-eu-west-1.amazonaws.com/cheeseshop.txt.
Let's create our first bucket through the AWS Console. We can perform most of the
operations that the API exposes manually through this web interface, and it's a good
way of checking that our API client is performing the desired tasks:
- Log into the Console at https://console.aws.amazon.com.
- Go to the S3 service. You will see a page, which will prompt you to
create a bucket. - Click on the Create Bucket button.
- Enter a bucket name, pick a region, and then click on Create.
- You will be taken to the bucket list, and you will be able to see your bucket.
An S3 command-line client
Okay, enough preparation, let's get to coding. For the rest of this section on S3, we
will be writing a small command line client that will enable us to interact with the
service. We will create buckets, and then upload and download files.