Skip to content

Instantly share code, notes, and snippets.

@camallen
Created February 20, 2025 06:29
Show Gist options
  • Save camallen/ac7419a5cefb1a4b039e5d9edc5a3f11 to your computer and use it in GitHub Desktop.
Save camallen/ac7419a5cefb1a4b039e5d9edc5a3f11 to your computer and use it in GitHub Desktop.
Edit S3 remote file and upload
#!/usr/bin/env bash
SUM=$(which sha512sum || which sha || which md5sum || which md5 || true)
# modify the following to match the s3 setup you desire
path="s3://bucket_name/path/$1"
tempfile=$(mktemp ~/.tmpXXXXXX)
trap "rm -f $tempfile" INT TERM HUP EXIT
aws s3 cp $path $tempfile
if [ ! -z "$SUM" ]
then
FILEHASH=$($SUM $tempfile)
fi
"${EDITOR:-vim}" $tempfile
if [ -z "$FILEHASH" ] || [ "$FILEHASH" != "$($SUM $tempfile)" ]
then
aws s3 cp --sse aws:kms $tempfile $path
fi
@camallen
Copy link
Author

Claude AI improvements

I'll help modify the script to work better with VSCode while maintaining the security aspects. Here's an improved version:

#!/usr/bin/env bash

# Configuration
EDITOR=${EDITOR:-"code -w"} # Default to VSCode in wait mode
S3_BUCKET="s3://bucket_name"
S3_PATH="path"

# Ensure required commands exist
command -v aws >/dev/null 2>&1 || { echo "AWS CLI required but not installed. Aborting." >&2; exit 1; }
command -v code >/dev/null 2>&1 || { echo "VSCode (code) command not found. Ensure VSCode is in your PATH." >&2; exit 1; }

# Determine hash command
if command -v sha512sum >/dev/null 2>&1; then
    SUM="sha512sum"
elif command -v sha256sum >/dev/null 2>&1; then
    SUM="sha256sum"
elif command -v md5sum >/dev/null 2>&1; then
    SUM="md5sum"
else
    echo "No suitable hash command found. Installing sha256sum..."
    exit 1
fi

# Validate input
if [ -z "$1" ]; then
    echo "Usage: $0 <filename>"
    echo "Example: $0 config.json"
    exit 1
fi

# Create temporary directory with meaningful name
TEMP_DIR=$(mktemp -d ~/.s3edit-XXXXXX)
TEMP_FILE="$TEMP_DIR/$1"

# Cleanup function
cleanup() {
    rm -rf "$TEMP_DIR"
}
trap cleanup EXIT INT TERM

# Full S3 path
S3_FULL_PATH="${S3_BUCKET}/${S3_PATH}/$1"

echo "Downloading $S3_FULL_PATH..."

# Download file from S3
if ! aws s3 cp "$S3_FULL_PATH" "$TEMP_FILE"; then
    echo "Error downloading file from S3"
    exit 1
fi

# Calculate initial hash
INITIAL_HASH=$($SUM "$TEMP_FILE")

# Open in VSCode and wait for edit
echo "Opening in VSCode..."
$EDITOR "$TEMP_FILE"

# Calculate new hash
NEW_HASH=$($SUM "$TEMP_FILE")

# Upload if changed
if [ "$INITIAL_HASH" != "$NEW_HASH" ]; then
    echo "File changed, uploading to S3..."
    if aws s3 cp --sse aws:kms "$TEMP_FILE" "$S3_FULL_PATH"; then
        echo "Successfully uploaded to S3"
    else
        echo "Error uploading to S3"
        exit 1
    fi
else
    echo "No changes detected"
fi

Key improvements made:

  1. Uses VSCode's wait flag (-w): This makes the script wait until you close the file in VSCode before continuing

  2. Better file organization:

    • Creates a temporary directory instead of just a file
    • Preserves the original filename for better VSCode recognition
    • Cleaner cleanup using a trap on a directory
  3. Added error handling:

    • Checks for required commands (aws, code)
    • Validates S3 operations
    • Provides meaningful error messages
  4. Better user feedback:

    • Progress messages
    • Clear usage instructions
    • Status updates on file changes
  5. Configuration section at the top:

    • Easy to modify S3 bucket and path
    • Configurable editor with VSCode default

To use this script:

  1. Save it as something like s3edit.sh
  2. Make it executable: chmod +x s3edit.sh
  3. Run it: ./s3edit.sh myfile.json

You can also set up a VSCode keybinding for this script by adding to your keybindings.json:

{
    "key": "cmd+shift+s",  // or whatever binding you prefer
    "command": "workbench.action.terminal.sendSequence",
    "args": {
        "text": "./s3edit.sh ${file}\n"
    }
}

One additional improvement you might consider is adding a file extension whitelist or specific S3 path restrictions for security. Let me know if you'd like me to add those or explain any part in more detail!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment