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.
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
# 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"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment