Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Amazon S3 file download through curl by using IAM user credentials

I have created an IAM user with access to only one bucket. I have tested the credentials and permissions through web and python boto. Its working fine.

Now I have requirement to use these credentials and download the private file from that bucket through curl.

signature="$(echo -n "GET" | openssl sha1 -hmac "f/rHQ8yCvPthxxxxxxxXxxxx" -binary | base64)"
date="$(LC_ALL=C date -u +"%a, %d %b %Y %X %z")"

curl -H "Host: my-bucket.s3.amazonaws.com" -H "Date: $date" -H "Authorization: AWS 'XXXAJX2NY3QXXX35XXX':$signature" -H "Content-Type: 'text/plain'" https://my-bucket.s3.amazonaws.com/path/to_file.txt

but i am getting the following error:

InvalidAccessKeyIdThe AWS Access Key Id you provided does not exist in our records.

Please help, how do I download the file using curl ? Is there anything am I missing or its not possible through curl command?

Thanks!

like image 552
Ahsan Avatar asked Feb 17 '15 12:02

Ahsan


People also ask

Can you curl S3 bucket?

By using curl, you can actually upload the file on aws s3. The requirement is that you must have the access key and the secret key.

How do I download data from AWS S3?

In the Amazon S3 console, choose your S3 bucket, choose the file that you want to open or download, choose Actions, and then choose Open or Download. If you are downloading an object, specify where you want to save it. The procedure for saving the object depends on the browser and operating system that you are using.


1 Answers

Following is the example on how you can download with s3 curl script,

#!/bin/sh 
file=path/to/file 
bucket=your-bucket 
resource="/${bucket}/${file}" 
contentType="application/x-compressed-tar" 
dateValue="`date +'%a, %d %b %Y %H:%M:%S %z'`" 
stringToSign="GET 
${contentType} 
${dateValue} 
${resource}" 
s3Key=xxxxxxxxxxxxxxxxxxxx 
s3Secret=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx 
signature=`/bin/echo -en "$stringToSign" | openssl sha1 -hmac ${s3Secret} -binary | base64`
curl -H "Host: ${bucket}.s3.amazonaws.com" \
-H "Date: ${dateValue}" \
-H "Content-Type: ${contentType}" \ 
-H "Authorization: AWS ${s3Key}:${signature}" \ 
https://${bucket}.s3.amazonaws.com/${file}

Hope it helps.

like image 116
Kannaiyan Avatar answered Oct 08 '22 05:10

Kannaiyan