Skip to content

Instantly share code, notes, and snippets.

@dzvon
Created September 28, 2020 08:46
Show Gist options
  • Save dzvon/868b351ccd88575924adfbc2d9b7409f to your computer and use it in GitHub Desktop.
Save dzvon/868b351ccd88575924adfbc2d9b7409f to your computer and use it in GitHub Desktop.
S3 uploader
#!/usr/bin/env bash
echo "The goal of this script is to upload file to s3 storage."
echo "Endpoint (default: s3-cn-east-1.qiniucs.com):"
read endpoint
echo "Your s3 access key:"
read s3AccessKey
echo "Your s3 secret key:"
read s3SecretKey
echo "Bucket name:"
read s3Bucket
echo "File path (do not preceded by ./):"
read fileName
dateFormatted="date -R"
relativePath="/${s3Bucket}/${fileName}"
contentType="application/octet-stream"
stringToSign="PUT\n\n${contentType}\n${dateFormatted}\n${relativePath}"
signature=`echo -en ${stringToSign} | openssl sha1 -hmac ${s3SecretKey} -binary | base64`
host=${s3Bucket}.${endpoint:-s3-cn-east-1.qiniucs.com}
curl -v -X PUT -T "${fileName}" -H "Host: ${host}" -H "Date: ${dateFormatted}" -H "Content-Type: ${contentType}" -H "Authorization: AWS ${s3AccessKey}:${signature}" http://${host}/${fileName}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment