Using CURL to Upload Files via POST to Amazon S3

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.

Write a Comment

Comment

28 Comments

    • 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.

  1. 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

  2. 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;

  3. 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

  4. 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

  5. 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.

    • 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.

  6. 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}

  7. 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?