Not able to upload file to aws s3 using shell scri

2019-03-01 03:30发布

问题:

I am getting following error while trying to upload to s3. The script below seems to be correct but still I get above error. Please can someone help me in solving this error. My secret key and access ID are correct as I am able to connect to AWS using these keys in java and ruby.

<?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>AKIAJNODAIRHFUX3LHFQ</AWSAccessKeyId><StringToSign>PUT

application/x-compressed-tar
Sun, 20 Dec 2015 19:54:47 -0500
/test-pk-proj//home/rushi/pk.tar.gz</StringToSign><SignatureProvided>M1PcN+Umkq5WFtVVSerHRGNABb8=</SignatureProvided><StringToSignBytes>50 55 54 0a 0a 61 70 70 6c 69 63 61 74 69 6f 6e 2f 78 2d 63 6f 6d 70 72 65 73 73 65 64 2d 74 61 72 0a 53 75 6e 2c 20 32 30 20 44 65 63 20 32 30 31 35 20 31 39 3a 35 34 3a 34 37 20 2d 30 35 30 30 0a 2f 74 65 73 74 2d 70 6b 2d 70 72 6f 6a 2f 2f 68 6f 6d 65 2f 72 75 73 68 69 2f 70 6b 2e 74 61 72 2e 67 7a</StringToSignBytes><RequestId>5439C7C84533E7C6</RequestId><HostId>620896ul+wnRwCjWl1ZtNZQ5NEJMGl29FqESC3iJyvnWhYhOECLlPl0417RfF3eovKFb7ac2.amazonaws.com port 443: Connection timed out

Below is my shell script which I am using to upload data to s3

file=/home/rushi/1.pdf
bucket=xxxxxxxxxxxxxxxxxxx
resource="/${bucket}/${file}"
contentType="application/x-compressed-tar"
dateValue=`date -R`
stringToSign="PUT\n\n${contentType}\n${dateValue}\n${resource}"
s3Key=xxxxxxxxxxxxxxxxxxxxxxxxxxx
s3Secret=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
signature=`echo -en ${stringToSign} | openssl sha1 -hmac ${s3Secret} -binary | base64`
curl -L -X PUT -T "${file}" \
  -H "Host: ${bucket}.s3-website-us-west-2.amazonaws.com" \
  -H "Date: ${dateValue}" \
  -H "Content-Type: ${contentType}" \
  -H "Authorization: AWS ${s3Key}:${signature}" \
  https://${bucket}.s3-website-us-west-2.amazonaws.com/${file}

回答1:

Install AWS CLI from link given below

Configure AWS by aws configure command and enter keys and region

To copy file to S3 use this command in shell script

aws s3 cp fileName s3://bucketName

Link: http://docs.aws.amazon.com/cli/latest/userguide/installing.html#install-bundle-other-os

p.s If you receive connection timed out error open port 443 (HTTPS) in security group.



回答2:

Please follow the below steps,

Create one IAM user with the permission to upload files in s3 bucket.

Note: You can also create a policy where you can access all the s3 buckets available in your region and you can attach the policy to the user that u have created in IAM for uploading files in S3 bucket

Make a note of your access and secret key for the IAM user.

Login in to your server for example [Aws linux server]

use the following command

aws configure

enter the access key enter the secret key enter the region for example if it is mumbau region use ap-south-1 enter the output as "text"

after this you are ready to upload files from your server to s3 bucket.

use the example command for uploading files in s3 bucket

aws s3 cp or mv fileName s3://bucketName

The above aws cli can be used in script for uploading files.