Skip to content

Instantly share code, notes, and snippets.

@bluejack
Last active February 27, 2025 21:03
Show Gist options
  • Save bluejack/92de1a71a699929e770f7c2f1fb89f0c to your computer and use it in GitHub Desktop.
Save bluejack/92de1a71a699929e770f7c2f1fb89f0c to your computer and use it in GitHub Desktop.
Sync local directory to S3 bucket
#!/bin/bash
## Conventions:
##
## Working directory has the same name as the s3 bucket.
## Script is executed from the parent of this directory.
##
#!/bin/bash
WATCH_PATH="${1}"
S3_BUCKET="s3://${1}"
ROOT_PATH="<fill this in for your environment>"
sync_file() {
local file="$1"
local s3_path="${file#"$WATCH_PATH/"}"
aws s3 cp "$file" "$S3_BUCKET/$s3_path"
echo "Curr watch: $WATCH_PATH"
echo "Synced: $file -> $S3_BUCKET/$s3_path"
}
delete_file() {
local file="$1"
local s3_path="${file#"$WATCH_PATH/"}"
aws s3 rm "$S3_BUCKET/$s3_path"
echo "Deleted: $S3_BUCKET/$s3_path"
}
# Monitor real-time changes
fswatch -0 -r "$WATCH_PATH" | while IFS= read -r -d "" file
do
echo "$file"
fref="${file#"$ROOT_PATH"}"
echo "$fref"
if [ -f "$file" ]; then
sync_file "$fref"
elif [ ! -e "$file" ]; then
delete_file "$fref"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment