Skip to content

Instantly share code, notes, and snippets.

@tuxfight3r
Last active March 27, 2024 08:42
Show Gist options
  • Save tuxfight3r/7ccbd5abc4ded37ecdbc8fa46966b7e8 to your computer and use it in GitHub Desktop.
Save tuxfight3r/7ccbd5abc4ded37ecdbc8fa46966b7e8 to your computer and use it in GitHub Desktop.
AWS - Upload files to S3 via curl
#!/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
@ningjunwei2
Copy link

tag

@evilone79
Copy link

amazonaws.com$aws_path${file_path##/*/}
should be changed to
amazonaws.com$aws_path/${file_path##/*/}

@MadDaelim
Copy link

If you have a curl version >= 7.75.0 you can add files to s3 like this

#!/bin/sh

FILE=$1

S3_ACCESS_KEY="<ACCESS_KEY>"
S3_SECRET_KEY="<SECRET_KEY>"
S3_BUCKET="<BUCKET_NAME>"
S3_REGION="<REGION>"

curl --progress-bar -X PUT \
    --user "${S3_ACCESS_KEY}":"${S3_SECRET_KEY}" \
    --aws-sigv4 "aws:amz:${S3_REGION}:s3" \
    --upload-file ${FILE} \
    https://${S3_BUCKET}.s3.${S3_REGION}.amazonaws.com

@Allan-Nava
Copy link

is working this script?

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