A few months ago I wrote a post about creating Amazon S3 HMAC Signatures without PEAR or PHP5. One of the things I was using that PHP script for was to feed the necessary information to a bash script hosted on a remote machine. The bash script was to upload a file via POST to Amazon S3 using the information provided.
Since CURL was already installed on the remote machine, I wanted to use that to do the actual uploading. I found very little help on the net regarding how to do this with CURL so here you go:
eris:~ raam$ curl
-F "key=screenshots/current_screenshot.jpg"
-F "acl=public-read"
-F "AWSAccessKeyId=2EO6H8MX1X8YWEA0V432"
-F "Policy=eyAiZXhwaXshdGlvbpI6ICIyMDA4LTErLTAxVDtyOjAwOjAwLjAsMFoiLAogICJjb25kaXRpb25zPjogWwoJeyJidWNrZXQiOiAiczNwaG90b3MubW9hcHAubmV0IiB9LAogICAgWyJzdGFydHMtd2l0aCIsICIka2V5IiwgIkxpdmVTaG90cy8iXSwKICAgIHsiYWNsIjogInB1YmxpYy1yZWFkIiB9LAoJWyJlcSIsICIkQ29udGVudC1UeXBlIiwgImltYWdlL2pwZWciXSwKICBdCn0K"
-F "Signature=20uh08kU75ADHL49NyhYRgZW8BY="
-F "Content-Type=image/jpeg"
-F "file=@current_screenshot.jpg"
http://screenshots.ekarma.net
Keep in mind this assumes the current_screenshot.jpg
file is in your current directory.
How did you get the “Policy” number?
I was able to get other information from the firebug.
See my other post; it describes how to generate the Policy: https://raamdev.com/2008/05/03/amazon-s3-hmac-signatures-without-pear-or-php5/
Thanks for this usefull post.
I’m trying to remove a file from s3. Would it work if I include ‘-X DELETE’ in the request?
Thanks
Hi Maryam,
Its been awhile since I’ve used the AWS API, so I can’t tell you off the top of my head. The answer can be found in the API docs on Amazon’s website.
I can’t tell you how much I appreciated seeing a familiar name when I searched for a curl-for-s3 example. Thanks, duder!
Haha, that’s awesome! Good to hear from you as well! π
Hello Sir.
I wasted a complete day to test tools like s3cmd and I was unsatisfied.
Well, your Post was very helpful to me. This will be part of my server backup script.
Thanks a lot
Friedhelm
You’re most welcome, Friedhelm. I’m so happy this helped you. π
I tried to do the same in php curl but got access denied. Any thought? I am sure all the parameter passing through curl were correct.
snippet:
$params = array(
‘key’ => $key,
‘acl’ => $acl,
‘AWSAccessKeyId’ => $AWSAccessKeyId,
‘Policy’ => $Policy,
‘Signature’ => $Signature,
‘Content-Type’ => $Contenttype,
‘file’ => $file
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
$response = curl_exec($ch);
curl_close($ch);
echo $response;
My only thought is that maybe you’ve got the key and/or acl wrong. Have you taken a look at Amazon S3 HMAC Signatures without PEAR or PHP5?
Did you make it work? If yes, could you please share your code?
I want to delete the bucket along with all data from it. Could you please let me know the complete CURL command to do the cleanup and bucket deletion.
Thanks,
Rajesh
Hi Rajesh,
I don’t know the command, but you might try posting this question on Stackoverflow.com.
Awesome, thank you!
You’re welcome, John!
Hey I couldn’t get your example to work, so I modified it a bit. This takes the signature out and generates it on the fly.
file=$1
bucket=YOUR_BUCKET
resource=”/${bucket}/${file}”
contentType=”application/x-itunes-ipa”
dateValue=`date -R`
stringToSign=”PUTnn${contentType}n${dateValue}n${resource}”
s3Key=YOUR_KEY_HERE
s3Secret=YOUR_SECRET
echo “SENDING TO S3”
signature=`echo -en ${stringToSign} | openssl sha1 -hmac ${s3Secret} -binary | base64`
curl -vv -X PUT -T “${file}”
-H “Host: ${bucket}.s3.amazonaws.com”
-H “Date: ${dateValue}”
-H “Content-Type: ${contentType}”
-H “Authorization: AWS ${s3Key}:${signature}”
http://www.jamesransom.net/?p=58
Thanks for sharing that, James! π
thanks james.
i’m gonna copy/paste this if you don’t mind
Could you please share the same code in php language. I am in a critical situation. I need to upload file to amazon s3 using php and curl.
You may want to check out the Amazon SDK for PHP.
hey ram any idea on api gateway service proxy for sending object to s3
Hi Krishna,
I’m sorry, but I haven’t worked with this code in a long time so I’m not sure how to help. I suggest posting a question on StackOverflow.com.
How do you specify the file name?
The filename in the key in the return xml should be substituted with the concrete file name.
See this question, which was posted by me:
http://stackoverflow.com/questions/37212932/how-to-upload-amazon-s3-using-curl-using-a-presigned-token
I am doing s3 post object test with curl,but i always get SignatureDoesNotMatch response. i have spent several days finding the solution, but it’s in vain. Could you please share me some experience about this? Following is part of my script.
bucket=$2
object=$3
file=$4
s3Key=${S3_ACCESS_KEY_ID}
s3Secret=${S3_SECRET_ACCESS_KEY}
host=${S3_HOSTNAME}
resource=”/${bucket}”
contentType=”multipart/form-data;boundary=-9431149156168″
contentMD5=
openssl md5 -binary ${file} | base64
method=POST
date=
date -R -u
stringToSign=”${method}
${contentMD5}
${contentType}
${date}
${resource}”
signature=
/bin/echo -n "$stringToSign" | openssl sha1 -hmac ${s3Secret} -binary | base64
curl -v -X POST \
-H “Date:${date}”\
-H “Content-MD5:${contentMD5}”\
-H “Content-Type:${contentType}”\
-H “Authorization: AWS ${s3Key}:${signature}”\
-F “key=${bucket}/${object}”\
-F “AWSAccessKeyId=${S3_ACCESS_KEY_ID}”\
-F “Signature=${signature}”\
-F “Content-Type=text/plain”\
-F “file=@${file}”\
http://${host}${resource}
I am doing s3 post object test with curl,but i always get SignatureDoesNotMatch response. i have spent several days finding the solution, but itβs in vain. Could you please share me some experience about this?
Hi, Can we change the code to connect S3 via Cloudfront? As far as I know, Cloudfront doesn’t accept option verb!
Hi Shokoofeh,
Apologies for the very, very late reply. I’m not too experienced with AWS anymore, so unfortunately I won’t be much help.
I would like to Amazon AWS but it showing signature miss match