Skip to content

Instantly share code, notes, and snippets.

@carlosribas
Last active March 2, 2023 08:27
Show Gist options
  • Save carlosribas/2162ce2b57a7e3a0eddfa58cf59e01d8 to your computer and use it in GitHub Desktop.
Save carlosribas/2162ce2b57a7e3a0eddfa58cf59e01d8 to your computer and use it in GitHub Desktop.
Bash script to upload files and directories to S3
#!/bin/bash
# S3 server
server=""
# set the path based on the first argument
path=$1
# remove the last slash from the directory, if necessary
if [[ "$path" =~ '/'$ ]]; then
path=${path%?}
fi
# name of the S3 bucket
bucket_name=""
# credentials
secret=""
s3_key=""
function uploadFile
{
path=$1
# get the current date to calculate the signature
date=`date +'%a, %d %b %Y %H:%M:%S %z'`
# calculate the signature to be sent as a header
content_type="application/octet-stream"
string_to_sign="PUT\n\n$content_type\n${date}\n/${bucket_name}/${path}"
signature=$(echo -en "${string_to_sign}" | openssl sha1 -hmac "${secret}" -binary | base64)
# upload file
echo "uploading ${path}"
curl -X PUT -T "${path}" \
-H "Host: ${server}/${bucket_name}" \
-H "Date: $date" \
-H "Content-Type: $content_type" \
-H "Authorization: AWS ${s3_key}:${signature}" \
"https://${server}/${bucket_name}/${path}"
}
# loop through the path and upload the files
find $path -type f -print0 | while read -d $'\0' file; do
uploadFile "$file"
done
@sohailanjum97
Copy link

Now aws cant support sha1.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment