Last active
September 30, 2024 15:12
-
-
Save tuxfight3r/7ccbd5abc4ded37ecdbc8fa46966b7e8 to your computer and use it in GitHub Desktop.
AWS - Upload files to S3 via curl
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
#!/bin/bash -x | |
#Date: 21/7/2017 | |
#Author: Mohan | |
#Purpose: To upload files to AWS S3 via Curl | |
#Uploads file at the top level folder by default | |
#S3 parameters | |
S3KEY="XXXXXXXXXXX" | |
S3SECRET="XXXXXXXXXXXXXXXX" | |
S3BUCKET="storage-backup" | |
S3STORAGETYPE="STANDARD" #REDUCED_REDUNDANCY or STANDARD etc. | |
AWSREGION="s3-eu-west-1" | |
function putS3 | |
{ | |
file_path=$1 | |
aws_path=$2 | |
bucket="${S3BUCKET}" | |
date=$(date -R) | |
acl="x-amz-acl:private" | |
content_type="application/x-compressed-tar" | |
storage_type="x-amz-storage-class:${S3STORAGETYPE}" | |
string="PUT\n\n$content_type\n$date\n$acl\n$storage_type\n/$bucket$aws_path${file_path##/*/}" | |
signature=$(echo -en "${string}" | openssl sha1 -hmac "${S3SECRET}" -binary | base64) | |
curl -s --retry 3 --retry-delay 10 -X PUT -T "$file_path" \ | |
-H "Host: $bucket.${AWSREGION}.amazonaws.com" \ | |
-H "Date: $date" \ | |
-H "Content-Type: $content_type" \ | |
-H "$storage_type" \ | |
-H "$acl" \ | |
-H "Authorization: AWS ${S3KEY}:$signature" \ | |
"https://$bucket.${AWSREGION}.amazonaws.com$aws_path${file_path##/*/}" | |
} | |
function usage | |
{ | |
echo "Usage: $0 <absolutepath_to_file> <s3_folder_path>" | |
echo "$0 '/tmp/storage_backup/storage-backup_07192017_112945.tar.gz' '/'" | |
} | |
#validate positional parameters are present | |
if [ $# -ne 2 ]; then | |
usage | |
echo "Exiting .." | |
exit 2 | |
fi | |
putS3 $1 $2 |
is working this script?
Hi,
I come across this question.
In ubuntu I had to change "function usage" for " usage()" for solve error
@jamontesg
is it an issue with Bash vs sh 'POSIX' syntax ? Were you using Bash ?
see Bash documentation functions or https://linuxize.com/post/bash-functions/ for example.
HTH
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If you have a curl version >= 7.75.0 you can add files to s3 like this