Skip to content

Instantly share code, notes, and snippets.

@managedkaos
Last active October 30, 2024 21:58
Show Gist options
  • Select an option

  • Save managedkaos/e1636ae6538ba3614aee084a733876f5 to your computer and use it in GitHub Desktop.

Select an option

Save managedkaos/e1636ae6538ba3614aee084a733876f5 to your computer and use it in GitHub Desktop.

Revisions

  1. managedkaos revised this gist Oct 30, 2024. 1 changed file with 13 additions and 6 deletions.
    19 changes: 13 additions & 6 deletions upload.sh
    Original file line number Diff line number Diff line change
    @@ -15,22 +15,29 @@ if [ ! -f "$POST_FILE" ]; then
    exit 1
    fi

    # Get the hostname and current date in YYYY-MM-DD format
    HOSTNAME=$(hostname)
    CURRENT_DATE=$(date +%F) # %F outputs YYYY-MM-DD

    # Create the S3 "folder" structure using hostname and date as prefixes
    PREFIX="${HOSTNAME}/${CURRENT_DATE}"
    FILENAME=$(basename "$FILE_TO_UPLOAD")
    KEY="${PREFIX}/${FILENAME}"

    # Extract fields from the JSON file
    URL=$(jq -r '.url' "$POST_FILE")
    KEY_PREFIX=$(jq -r '.fields.key' "$POST_FILE")
    ACL=$(jq -r '.fields.acl' "$POST_FILE")
    AWS_ACCESS_KEY_ID=$(jq -r '.fields.AWSAccessKeyId' "$POST_FILE")
    POLICY=$(jq -r '.fields.policy' "$POST_FILE")
    SIGNATURE=$(jq -r '.fields.signature' "$POST_FILE")

    # Replace ${filename} in key with the actual filename
    KEY=$(echo "$KEY_PREFIX" | sed "s/\${filename}/$(basename "$FILE_TO_UPLOAD")/")

    # Use curl to upload the file
    curl -F "key=$KEY" \
    # Use curl to upload the file with the new key
    curl -F "key=uploads/$KEY" \
    -F "acl=$ACL" \
    -F "AWSAccessKeyId=$AWS_ACCESS_KEY_ID" \
    -F "policy=$POLICY" \
    -F "signature=$SIGNATURE" \
    -F "file=@$FILE_TO_UPLOAD" \
    "$URL"

    echo "File uploaded to: $KEY"
  2. managedkaos created this gist Oct 24, 2024.
    33 changes: 33 additions & 0 deletions post-gen.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,33 @@
    import boto3
    import json

    def generate_presigned_post(bucket_name, key_prefix='uploads/', expiration=3600):
    # Initialize the S3 client
    s3 = boto3.client('s3')

    # Generate the pre-signed POST request
    response = s3.generate_presigned_post(
    Bucket=bucket_name,
    Key=f"{key_prefix}${{filename}}", # Arbitrary filenames allowed
    Fields={"acl": "private"}, # Default ACL for uploaded files
    Conditions=[
    {"acl": "private"}, # Enforce private uploads
    ["starts-with", "$key", key_prefix], # Ensure files follow prefix structure
    ],
    ExpiresIn=expiration # URL expiration time in seconds
    )
    return response

    def save_presigned_post_to_file(bucket_name, file_name='presigned_post.json'):
    # Generate the POST data
    post_data = generate_presigned_post(bucket_name)

    # Save the JSON data to a file
    with open(file_name, 'w') as f:
    json.dump(post_data, f, indent=4)

    print(f"Pre-signed POST request saved to {file_name}")

    # Usage example
    bucket_name = 'more-dogfish-20230223041658282000000004'
    save_presigned_post_to_file(bucket_name)
    36 changes: 36 additions & 0 deletions upload.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,36 @@
    #!/bin/bash

    # Usage: ./upload.sh my-local-file.txt
    if [ $# -ne 1 ]; then
    echo "Usage: $0 <file-to-upload>"
    exit 1
    fi

    FILE_TO_UPLOAD=$1
    POST_FILE="presigned_post.json"

    # Check if the presigned POST JSON file exists
    if [ ! -f "$POST_FILE" ]; then
    echo "Error: $POST_FILE not found!"
    exit 1
    fi

    # Extract fields from the JSON file
    URL=$(jq -r '.url' "$POST_FILE")
    KEY_PREFIX=$(jq -r '.fields.key' "$POST_FILE")
    ACL=$(jq -r '.fields.acl' "$POST_FILE")
    AWS_ACCESS_KEY_ID=$(jq -r '.fields.AWSAccessKeyId' "$POST_FILE")
    POLICY=$(jq -r '.fields.policy' "$POST_FILE")
    SIGNATURE=$(jq -r '.fields.signature' "$POST_FILE")

    # Replace ${filename} in key with the actual filename
    KEY=$(echo "$KEY_PREFIX" | sed "s/\${filename}/$(basename "$FILE_TO_UPLOAD")/")

    # Use curl to upload the file
    curl -F "key=$KEY" \
    -F "acl=$ACL" \
    -F "AWSAccessKeyId=$AWS_ACCESS_KEY_ID" \
    -F "policy=$POLICY" \
    -F "signature=$SIGNATURE" \
    -F "file=@$FILE_TO_UPLOAD" \
    "$URL"