Skip to content

Instantly share code, notes, and snippets.

@managedkaos
Last active October 30, 2024 21:58
Show Gist options
  • Save managedkaos/e1636ae6538ba3614aee084a733876f5 to your computer and use it in GitHub Desktop.
Save managedkaos/e1636ae6538ba3614aee084a733876f5 to your computer and use it in GitHub Desktop.
Scripts for creating and working with pre-signed URLS for S3 uploads.
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)
#!/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
# 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")
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")
# 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"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment