AWS S3 sync: Sync a Directory to S3
Synchronize a local directory with an S3 prefix using aws s3 sync. Only uploads changed files, with --delete support for mirror syncing.
Detailed Explanation
Syncing Directories with aws s3 sync
The aws s3 sync command intelligently synchronizes files between a local directory and an S3 prefix (or between two S3 locations). It only transfers files that are new or have changed, making it much faster than cp --recursive for incremental updates.
Basic Sync Command
aws s3 sync ./dist/ s3://my-website-bucket/
This uploads only files that have been added or modified since the last sync.
Mirror Sync with --delete
aws s3 sync ./dist/ s3://my-website-bucket/ --delete
The --delete flag removes files from the destination that no longer exist in the source, creating an exact mirror. Use with caution — this will permanently delete S3 objects.
Selective Sync with Filters
aws s3 sync ./project/ s3://my-bucket/project/ \
--exclude "*.tmp" \
--exclude ".git/*" \
--exclude "node_modules/*" \
--include "*.js" \
--include "*.css"
Filters are applied in order. First all files are included, then --exclude patterns remove files, then --include patterns add files back.
Dry Run Preview
aws s3 sync ./data/ s3://my-bucket/data/ --dryrun
The --dryrun flag shows what would be transferred without actually copying any files. Always use this before running with --delete on production data.
How Sync Determines Changes
S3 sync compares:
- File size — different sizes trigger a transfer
- Last modified time — newer local files are uploaded
- It does NOT compare file content (checksums) by default
Use Case
Deploying static website builds to S3, backing up project directories to cloud storage, or maintaining mirror copies of data directories across local and S3 environments.