Sub topic 2
Buckets: naming, scope and creation
This chapter assumes you can use a command line, know what an AWS account and an IAM identity are, and have the AWS CLI configured with credentials. You will learn what a bucket is, how to name one, where it lives, and how to create one safely.
What a bucket is
A bucket is the top-level container for objects in S3. Every object belongs to exactly one bucket. A bucket has no size limit you need to manage. It holds settings that apply to everything inside it: access policies, encryption defaults, versioning, lifecycle rules and event notifications.
An account can own many buckets, but there is a default quota (10,000 per account at the time of writing, which you can request to raise). Prefer a few buckets with well-designed key prefixes over thousands of tiny ones.
Scope: global names, regional storage
A bucket's name and its location have different scopes.
- The name is unique across all AWS accounts in a partition (the standard
awspartition, for example). Nobody else can use a name you hold in the same partition. - The data lives in one Region, which you choose at creation. The Region cannot be changed later. To move data, you create a new bucket and copy the objects.
Choosing the Region affects latency, cost, and legal requirements about where data may be stored.
Naming rules
For general purpose buckets, a name must follow these rules:
- 3 to 63 characters long.
- Only lowercase letters, numbers, dots and hyphens.
- Begins and ends with a letter or number.
- Not formatted like an IP address, such as
192.168.5.4. - Not starting with
xn--orsthree-, and not ending with-s3aliasor--ol-s3.
Dots are legal, but avoid them. They break the wildcard TLS certificate that virtual-hosted-style HTTPS requests rely on. A common pattern that keeps names unique is to add the account ID, as in my-example-bucket-123456789012.
Because names are public to the namespace, do not put secrets or personal data in them.
Creating a bucket
The next example creates a bucket in eu-west-1 and turns on all four Block Public Access settings. Outside us-east-1, you must pass a LocationConstraint that matches the Region.
aws s3api create-bucket \
--bucket my-example-bucket-123456789012 \
--region eu-west-1 \
--create-bucket-configuration LocationConstraint=eu-west-1
aws s3api put-public-access-block \
--bucket my-example-bucket-123456789012 \
--public-access-block-configuration BlockPublicAcls=true,IgnorePublicAcls=true,BlockPublicPolicy=true,RestrictPublicBuckets=trueCommon errors
| Error | Cause | Fix |
|---|---|---|
| `BucketAlreadyExists` | Another account owns the name. | Choose a different name, for example with a suffix. |
| `BucketAlreadyOwnedByYou` | You already created this name. | Reuse the bucket. In `us-east-1` a repeat create may succeed silently. |
| `IllegalLocationConstraintException` | The `LocationConstraint` is missing or does not match the Region. | Pass the matching constraint for any Region except `us-east-1`. |
| `InvalidBucketName` | The name breaks a naming rule. | Check length, characters and prefix rules. |
Deleting a bucket
You can delete a bucket only when it is empty. For versioned buckets, that includes all object versions and delete markers. Use aws s3 rb s3://BUCKET for an empty bucket, or add --force to remove the objects first. Be careful with --force on a versioned bucket, because it does not remove old versions.
Exercise
You run aws s3api create-bucket --bucket data-archive --region eu-west-1 and get BucketAlreadyExists. Name two separate things you should do or change, and say why the same command without a location setting would also fail for this Region.
Show answer
Pick a globally unique name, for example by appending your account ID, because bucket names are shared by all accounts in the partition. Also add --create-bucket-configuration LocationConstraint=eu-west-1. Any Region other than us-east-1 requires the constraint, otherwise S3 returns IllegalLocationConstraintException.