Created
December 25, 2023 22:21
-
-
Save eftakhairul/89cb912b8f2eeef4fb97dec134e90ced to your computer and use it in GitHub Desktop.
Download with authentication from private s3 bucket
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# Download a file from s3 without any 3rd party tools | |
# thanks https://www.gyanblog.com/aws/how-upload-aws-s3-curl/ | |
file_path=$1 | |
bucket=$2 | |
set -eu pipefail | |
# about the file | |
filepath="/${bucket}/${file_path}" | |
# metadata | |
contentType="application/octet-stream" | |
dateValue=`date -R` | |
signature_string="GET\n\n${contentType}\n${dateValue}\n${filepath}" | |
#s3 keys | |
s3_access_key=$AWS_ACCESS_KEY_ID | |
s3_secret_key=$AWS_SECRET_ACCESS_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 -sSo ${file_path} \ | |
-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_path} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment