Sub topic 2
Buckets: naming, scope and creation
A bucket is the top-level container in S3. Every object lives in exactly one bucket, and nearly every setting in later chapters (access, encryption, versioning, lifecycle) is attached to a bucket. This chapter covers the rules for naming a bucket, what a bucket is scoped to, and how to create one correctly.
It assumes you have the AWS CLI configured with credentials, know what an IAM user or role is, and can read short Python scripts.
What a bucket is
A bucket is a named container for objects, created in one AWS Region. It is not a folder and not a disk. You cannot nest buckets, and you do not mount one. You address a bucket by name over HTTPS, and S3 stores the objects you put in it.
A bucket has three properties that matter in practice:
- Name. The name is unique across all AWS accounts in the same partition (the standard
awspartition covers the commercial Regions). Nobody else can use a name you hold. - Region. You choose it at creation and cannot change it. The objects in the bucket are stored in that Region unless you replicate them.
- Owner. The creating AWS account owns the bucket. Settings such as policies, encryption defaults and lifecycle rules belong to the bucket.
Naming rules
A general purpose bucket name must follow these rules:
- It is 3 to 63 characters long.
- It contains only lowercase letters, numbers, hyphens and dots.
- It begins and ends with a letter or number.
- It does not look like an IP address, such as
192.168.5.4. - It does not contain two adjacent dots.
- It does not start with
xn--orsthree-, and does not end with-s3aliasor--ol-s3. AWS reserves these for other features.
Uppercase letters and underscores are rejected. My_Bucket fails, and my-bucket is accepted.
Choosing a name
Because the namespace is shared, short names like backups or logs are almost certainly taken. A name that is easy to guess is also easy for someone else to claim before you do. A common convention combines the purpose, the account ID and the Region:
orders-exports-123456789012-eu-west-1This keeps names unique in practice, makes the owner obvious in logs, and avoids collisions when you deploy the same stack to several accounts or Regions. Do not put secrets or personal data in a name. Bucket names appear in URLs, logs and error messages.
Two further points about names:
- A name is permanent. To "rename" a bucket you create a new one, copy the objects and delete the old bucket.
- After you delete a bucket, its name goes back into the pool. Another account may claim it, sometimes within minutes, and AWS does not guarantee you can get it back. If an application still refers to a deleted bucket name, someone else could receive its requests. Remove references before you delete.
Scope: account, Region and namespace
The name is global, but the bucket itself is regional. Both facts affect design.
| Aspect | Scope |
|---|---|
| Bucket name | Unique across all accounts in the partition |
| Bucket location | One Region, fixed at creation |
| Data | Stored in that Region, across multiple Availability Zones for most storage classes |
| Ownership | One AWS account |
| Settings (policy, versioning, lifecycle) | Per bucket |
| Number of objects | No limit |
| Number of buckets | Quota per account (check Service Quotas; the default is generous and can be raised) |
Choose the Region by weighing four things:
- Latency. Put data near the users or compute that read it.
- Compliance. Data residency rules may require a specific country or area.
- Cost. Prices differ by Region, and data transfer between Regions is billed.
- Service availability. Some features or related services are not offered everywhere.
Requests can be sent to any Region's endpoint, but the CLI and SDKs need to find the right one. The endpoint forms are:
Virtual-hosted style (current default):
https://my-example-bucket-123456789012.s3.eu-west-1.amazonaws.com/reports/report.csv
Path style (legacy):
https://s3.eu-west-1.amazonaws.com/my-example-bucket-123456789012/reports/report.csvIn virtual-hosted style the bucket name is part of the hostname, which is another reason the naming rules are strict (a bucket name must be a valid DNS label).
Defaults for new buckets
New general purpose buckets start with safer settings than older ones had:
- Block Public Access is on, so public ACLs and public policies are blocked.
- ACLs are disabled. Object Ownership is set to
BucketOwnerEnforced, so the bucket owner owns every object and access is managed with policies. - Server-side encryption with S3-managed keys (SSE-S3) is applied to new objects by default.
- Versioning is off. You must enable it yourself.
Do not rely on defaults you have not checked, especially in older accounts or in infrastructure templates that set these values explicitly. Setting Block Public Access explicitly, as in the next section, documents your intent and protects against later changes.
Creating a bucket
The CLI call depends on the Region. For us-east-1 you omit the location constraint. For every other Region you must supply it, and it must match the Region you are calling.
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=trueThe first command creates the bucket in eu-west-1. The second turns on all four Block Public Access settings for that bucket. For us-east-1, the creation command is the same without --create-bucket-configuration:
aws s3api create-bucket \
--bucket my-example-us-bucket-123456789012 \
--region us-east-1Sending LocationConstraint=us-east-1 is an error (InvalidLocationConstraint).
You can confirm that the bucket exists and that you can reach it with head-bucket. It prints nothing on success and returns an error code otherwise:
aws s3api head-bucket --bucket my-example-bucket-123456789012
aws s3api get-bucket-location --bucket my-example-bucket-123456789012Creating a bucket from Python
The same operation in boto3 follows the same Region rule. This script creates the bucket in the client's Region and handles the two most common outcomes:
import boto3
from botocore.exceptions import ClientError
region = "eu-west-1"
bucket = "my-example-bucket-123456789012"
s3 = boto3.client("s3", region_name=region)
params = {"Bucket": bucket}
if region != "us-east-1":
params["CreateBucketConfiguration"] = {"LocationConstraint": region}
try:
s3.create_bucket(**params)
print("created", bucket)
except ClientError as err:
code = err.response["Error"]["Code"]
if code == "BucketAlreadyOwnedByYou":
print("already exists in your account:", bucket)
elif code == "BucketAlreadyExists":
print("name taken by another account:", bucket)
else:
raiseCommon errors
| Error | Cause | Fix |
|---|---|---|
| `BucketAlreadyExists` | Another account holds the name | Choose a different name |
| `BucketAlreadyOwnedByYou` | Your account already created it | Treat as success, or check the Region |
| `InvalidBucketName` | Name breaks the naming rules | Use lowercase letters, numbers and hyphens, 3 to 63 characters |
| `IllegalLocationConstraintException` | `--region` and `LocationConstraint` do not match | Make both the same Region |
| `InvalidLocationConstraint` | Constraint given for `us-east-1`, or an unknown Region | Omit it for `us-east-1`, or fix the Region |
| `AccessDenied` | Caller lacks `s3:CreateBucket` | Add the permission to the IAM policy |
| `BucketNotEmpty` | Deleting a bucket that still holds objects | Empty it first |
A bucket in a different Region than the request can also cause a 301 PermanentRedirect or a region mismatch error. The cure is to send the request to the bucket's own Region, which you can find with get-bucket-location.
Deleting a bucket
A bucket must be empty before S3 deletes it. If versioning was ever enabled, "empty" means every object version and every delete marker is gone, not just the visible objects. The CLI helper removes the current objects only, so for a versioned bucket you need to remove versions too, or use a lifecycle rule to expire them.
aws s3 rm s3://my-example-bucket-123456789012 --recursive
aws s3api delete-bucket --bucket my-example-bucket-123456789012 --region eu-west-1Exercise
You run aws s3api create-bucket --bucket Reports_2026 --region eu-west-1 --create-bucket-configuration LocationConstraint=eu-west-1 and get InvalidBucketName. Then you fix the name and run the same command with --region us-east-1 but keep the LocationConstraint=eu-west-1 option, and it fails again. What is wrong in each case?
Show answer
The first name breaks the rules: it has an uppercase letter and an underscore. Use lowercase letters, numbers and hyphens, such as reports-2026-123456789012. The second command fails because the --region (us-east-1) and the LocationConstraint (eu-west-1) disagree. For us-east-1 you must omit the location constraint. To create the bucket in eu-west-1, set both --region and LocationConstraint to eu-west-1.
Exercise
A teammate wants to name a bucket data.example.com so it matches a website domain, and wants to move it from eu-west-1 to us-east-1 later if costs change. What problems do you point out?
Show answer
Dots in the name break the wildcard TLS certificate for virtual-hosted-style HTTPS requests, so HTTPS access can fail. Also, a bucket's Region cannot be changed. Moving means creating a new bucket in us-east-1 with a new unique name, copying the objects, updating every reference, and deleting the old bucket.