S3 URIs in AWS CLI Commands
Understand how the s3:// URI format is used in common AWS CLI commands for copying, syncing, and listing S3 objects.
Detailed Explanation
S3 URIs in the AWS CLI
The AWS CLI uses the s3:// URI scheme extensively for its high-level S3 commands (aws s3) and low-level API commands (aws s3api). Understanding the URI structure helps you construct correct commands and troubleshoot failures.
Common CLI Commands with S3 URIs
Copy a file to S3
aws s3 cp ./local-file.txt s3://my-bucket/path/file.txt
Parsed URI:
| Component | Value |
|---|---|
| Bucket | my-bucket |
| Key | path/file.txt |
Sync a directory
aws s3 sync ./build/ s3://static-site-bucket/
Parsed URI:
| Component | Value |
|---|---|
| Bucket | static-site-bucket |
| Key | (root prefix) |
List objects with a prefix
aws s3 ls s3://data-bucket/logs/2024/
Parsed URI:
| Component | Value |
|---|---|
| Bucket | data-bucket |
| Key | logs/2024/ (used as prefix) |
Trailing Slash Behavior
The trailing slash matters in CLI commands:
| URI | Meaning |
|---|---|
s3://bucket/folder |
Refers to the key literally named "folder" |
s3://bucket/folder/ |
Refers to all objects with prefix "folder/" |
This distinction is critical for aws s3 sync and aws s3 rm --recursive commands.
URI to HTTP URL Mapping
| s3:// URI | Equivalent HTTPS URL |
|---|---|
s3://bucket/key |
https://bucket.s3.REGION.amazonaws.com/key |
s3://bucket/ |
https://bucket.s3.REGION.amazonaws.com/ |
The CLI resolves the region automatically from your AWS configuration, so the s3:// URI doesn't need to include it.
Use Case
Writing shell scripts that automate S3 operations and need to parse or construct s3:// URIs dynamically based on environment variables for bucket name, prefix, and date partitions.