← All posts
Reading fromAWS from Zero
Volume 2 · Chapter 111. S3 bucket naming rules from the CLI12. S3 create-bucket: us-east-1 vs other regions13. S3 CLI vs S3 API commands14. Upload an S3 object with metadata15. Inspect an S3 object with head-object16. List S3 buckets with s3api17. Copy S3 objects inside one bucket18. Copy S3 objects across buckets19. Use include and exclude rules with S3 sync20. Delete one S3 object safely
ch 2.1 · 0/10 · lesson 9
2 min read
Use include and exclude rules with S3 sync
#aws#cli#s3#storage
📑 On this page
Part 19 of AWS from Zero. Today we make sync selective.
What we are learning
--exclude removes matching paths from a transfer. --include adds matching paths back. The order of these rules matters.
Before you run it
REGION="ap-south-1"
BUCKET="aws-zero-sync-filters-12345"
aws s3api create-bucket \
--bucket "$BUCKET" \
--region "$REGION" \
--create-bucket-configuration LocationConstraint="$REGION"
mkdir sync-demo
echo "first note" > sync-demo/one.txt
echo "second note" > sync-demo/two.txt
echo "temporary data" > sync-demo/cache.tmp
echo '{"ready":true}' > sync-demo/config.jsonPreview the command
aws s3 sync sync-demo/ "s3://$BUCKET/demo/" \
--exclude "*" \
--include "*.txt" \
--dryrun--dryrun shows what would happen without uploading anything.
Run the sync
aws s3 sync sync-demo/ "s3://$BUCKET/demo/" \
--exclude "*" \
--include "*.txt"Inspect the result
aws s3api list-objects-v2 \
--bucket "$BUCKET" \
--prefix "demo/" \
--query "Contents[].Key" \
--output tableOnly the two .txt files should appear.
One tiny variation
Include JSON files as well:
aws s3 sync sync-demo/ "s3://$BUCKET/demo/" \
--exclude "*" \
--include "*.txt" \
--include "*.json" \
--dryrunCommon mistake
This order does not produce the same selection:
--include "*.txt" --exclude "*"Filters are evaluated in order, and later matching rules can override earlier ones. Start by excluding everything, then include the patterns you want.
Cleanup
aws s3 rm "s3://$BUCKET/" --recursive
aws s3api delete-bucket --bucket "$BUCKET" --region "$REGION"
rm -rf sync-demoNext, we will delete one object and verify that it is gone.