Skip to content

Instantly share code, notes, and snippets.

@raylee
Created October 9, 2024 01:43
Show Gist options
  • Save raylee/e25a13f7954eef0d0dfda58e4a66ee81 to your computer and use it in GitHub Desktop.
Save raylee/e25a13f7954eef0d0dfda58e4a66ee81 to your computer and use it in GitHub Desktop.
Minimal dependency upload to s3 using curl, openssl, base64, and file.
#!/bin/bash
target_bucket=some-bucket-name-here
upload-file() {
local file_to_upload="$1" bucket="$2"
# metadata
contentType=$(file --brief --mime-type "$file_to_upload")
dateValue=$(date -R)
filepath="/${bucket}/${file_to_upload}"
signature_string="PUT\n\n${contentType}\n${dateValue}\n${filepath}"
#s3 keys
s3_access_key=$(cat ~/.s3-access-key)
s3_secret_key=$(cat ~/.s3-secret-key)
#prepare signature hash to be sent in Authorization header
signature_hash=$(echo -en "${signature_string}" | openssl sha1 -hmac "${s3_secret_key}" -binary | base64)
# actual curl command to do PUT operation on s3
curl -X PUT -T "${file_to_upload}" \
-H "Host: ${bucket}.s3.amazonaws.com" \
-H "Date: ${dateValue}" \
-H "Content-Type: ${contentType}" \
-H "Authorization: AWS ${s3_access_key}:${signature_hash}" \
https://${bucket}.s3.amazonaws.com/${file_to_upload}
}
upload-file "$1" ${target_bucket}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment