Report this

What is the reason for this report?

DigitalOcean Spaces - File Upload Using Curl

Posted on August 9, 2019

Hi!

I’m trying to upload some files to DigitalOcean’s Spaces with PHP and cURL but for some reason I cannot do it. I don’t want to use some libraries like Amazon’s SDK because they are too big. I want something more simple. Here is where I am at now.

 $file = '/home/development/www/source.png';

    $contents = file_get_contents($file);
    $filesize = filesize($file);

    $accessKeyId = '###';
    $secretKey = '###';

    $longDate = gmdate('Ymd\THis\Z');
    $shortDate = gmdate('Ymd');

    $newfile = urlencode('/new-dir/new-file.png');

    $cannonical = 'PUT'.PHP_EOL.''.$newfile.''.PHP_EOL.''.PHP_EOL.'content-length:'.$filesize.''.PHP_EOL.'host:fra1.digitaloceanspaces.com'.PHP_EOL.'x-amz-date:'.$longDate.''.PHP_EOL.'x-amz-acl:public-read'.PHP_EOL.'content-length;host;x-amz-date;x-amz-acl;'.PHP_EOL.''.hash_file('sha256', $file).''.PHP_EOL.'';

    $cannonical = hash('sha256', $cannonical);

    $string = 'AWS4-HMAC-SHA256'.PHP_EOL.''.$longDate.''.PHP_EOL.''.$shortDate.'/fra1/s3/aws4_request'.PHP_EOL.''.$cannonical.''.PHP_EOL.'';

    $signingKey = hash_hmac('sha256', $shortDate, 'AWS4'.$secretKey, true);
    $signingKey = hash_hmac('sha256', 'fra1', $signingKey, true);
    $signingKey = hash_hmac('sha256', 's3', $signingKey, true);
    $signingKey = hash_hmac('sha256', 'aws4_request', $signingKey, true);
    $signature  = hash_hmac('sha256', $signingKey.$string, $signingKey);


    $ch = curl_init();
    curl_setopt($ch, CURLOPT_HEADER, true);
    curl_setopt($ch, CURLOPT_URL, 'https://bucket.fra1.digitaloceanspaces.com/'.$newfile);
    curl_setopt($ch, CURLOPT_HTTPHEADER, [
            'Content-Length: '.$filesize,
            'Host: bucket.fra1.digitaloceanspaces.com',
            'x-amz-date: '.$longDate,
            'x-amz-acl: public-read',
            'Authorization: AWS4-HMAC-SHA256 Credential='.$accessKeyId.'/'.$longDate.'/fra1/s3/aws4_request, SignedHeaders=host;x-amz-date;x-amz-acl;content-length, Signature='.$signature
    ]);

    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
    curl_setopt($ch, CURLOPT_POSTFIELDS, $contents);
    curl_exec($ch);
    echo curl_error($ch);
    $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    echo $http_code;

Every time I try to upload something it says 403 Forbidden. What am I doing wrong in the signature calculation?

Thank you.



This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Hey!

I’m not sure what’s missing in your code here. We do have similar case discussed earlier in community link below which might help you to upload files using PHP: https://www.digitalocean.com/community/questions/how-to-use-digitalocean-spaces-with-the-aws-s3-sdks?answer=39594

If you are still getting an error then I would highly recommend you to open a support ticket along with the error message so that our support team looks into it.

-Dikshith

I got same issues with you, and after modify some of your code, I’m able to upload file.

Here’s my update

<?php

$file = 'module_table_bottom.png';

$contents = file_get_contents($file);
$filesize = filesize($file);

$accessKeyId = '=======';
$secretKey = '====';

$longDate = gmdate('Ymd\THis\Z');
$shortDate = gmdate('Ymd');

$newfile = urlencode('DEV/module_table_bottom.png');

$cannonical = 'PUT'.PHP_EOL.'/'.$newfile.''.PHP_EOL.''.PHP_EOL.'content-length:'.$filesize.''.PHP_EOL.'host:BUCKET.sgp1.digitaloceanspaces.com'.PHP_EOL.'x-amz-content-sha256:'.hash_file('sha256', $file).PHP_EOL.'x-amz-date:'.$longDate.''.PHP_EOL.''.PHP_EOL.'content-length;host;x-amz-content-sha256;x-amz-date'.PHP_EOL.''.hash_file('sha256', $file);

$cannonical = hash('sha256', $cannonical);

$string = 'AWS4-HMAC-SHA256'.PHP_EOL.''.$longDate.''.PHP_EOL.''.$shortDate.'/ap-southeast-1/s3/aws4_request'.PHP_EOL.''.$cannonical;

$signingKey = hash_hmac('sha256', $shortDate, 'AWS4'.$secretKey, true);
$signingKey = hash_hmac('sha256', 'ap-southeast-1', $signingKey, true);
$signingKey = hash_hmac('sha256', 's3', $signingKey, true);
$signingKey = hash_hmac('sha256', 'aws4_request', $signingKey, true);
//$signature  = hash_hmac('sha256', $signingKey.$string, $signingKey);
$signature  = hash_hmac('sha256', $string, $signingKey);


$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_URL, 'https://BUCKET.sgp1.digitaloceanspaces.com/'.$newfile);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
        'Content-Length: '.$filesize,
        'Host: BUCKET.sgp1.digitaloceanspaces.com',
        'x-amz-date: '.$longDate,
		'x-amz-content-sha256: '.hash_file('sha256', $file),		
        'Authorization: AWS4-HMAC-SHA256 Credential='.$accessKeyId.'/'.$shortDate.'/ap-southeast-1/s3/aws4_request, SignedHeaders=content-length;host;x-amz-content-sha256;x-amz-date, Signature='.$signature
]);

curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($ch, CURLOPT_POSTFIELDS, $contents);
curl_exec($ch);
echo curl_error($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
echo $http_code;

?>

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.