R
R
Ruslan Galiev2017-03-14 09:55:24
bash
Ruslan Galiev, 2017-03-14 09:55:24

Uploading file to Amazon S3 using bash script?

Hello
On the Internet, a bash script was found to upload files to Amazon S3. Accordingly, I want to use it to automate database backups.

#!/bin/bash
# Use curl command to upload file at S3 bucket.
#upload to S3 bucket
sourceFilePath="/team/test.zip";
#file path at S3
filePathAtS3="test.zip";
#Your S3 bucket name
bucket="*************";
#S3 HTTP Resource URL for your file
resource="/${bucket}/${filePathAtS3}";
#set content type
contentType="application/zip";
#get date as RFC 7231 format
dateValue=`date -R`;
acl="x-amz-acl:public-read"
#String to generate signature
stringToSign="PUT\n\n${contentType}\n${dateValue}\n${acl}\n/${resource}"
# stringToSign="PUT\n\n${contentType}\n${dateValue}\n${resource}";
#your S3 key. This is specific to S3. This is not your AWS username.
s3Key="**********************";
#your S3 secret. This is specific to S3. This is not your AWS password.
s3Secret="***************************************";
#Generate signature, Amazon re-calculates the signature and compares if it matches the one that was contained in your request. That way the secret access key never needs to be transmitted over the network.
signature=`echo -en ${stringToSign} | openssl sha1 -hmac ${s3Secret} -binary | base64`;
#Use curl to make PUT request.
curl -L -X PUT -T "${sourceFilePath}" \
  -H "Host: ${bucket}.s3.amazonaws.com" \
  -H "Date: ${dateValue}" \
  -H "Content-Type: ${contentType}" \
  -H "Authorization: AWS ${s3Key}:${signature}" \
  https://${bucket}.s3.amazonaws.com/${filePathAtS3}

Result of script execution:
<?xml version="1.0" encoding="UTF-8"?>
<Error><Code>SignatureDoesNotMatch</Code><Message>The request signature we calculated does not match the signature you provided. Check your key and signing method.</Message><AWSAccessKeyId>AKIAJP75IY7JM4HXQBKA</AWSAccessKeyId><StringToSign>

Please tell me how to fix this problem or tell me show the bash script.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Shamsudin Serderov, 2017-03-14
@Steein

link

3
3vi1_0n3, 2018-12-12
@3vi1_0n3

Better aws-cli. After adding keys
aws s3 sync ./backup-folder s3://bucket-name

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question