1
votes

I am using rest API to create a container in Azure storage. I want to give public permission or container permission to it to upload blob into the container dynamically.I am wriing "x-ms-blob-public-access:container" in header but when I am including below error coming

Error

Result HTTP/1.1 403 Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature. Content-Length: 709 Content-Type: application/xml Server: Microsoft-HTTPAPI/2.0 x-ms-request-id: 832b00af-501e-0032-7c96-f1ba27000000 Date: Tue, 22 May 2018 06:29:51 GMT AuthenticationFailedServer failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature. RequestId:832b00af-501e-0032-7c96-f1ba27000000 Time:2018-05-22T06:29:51.7678173ZThe MAC signature found in the HTTP request 'bmJk1ntjZX17woKGTm1YjEaIU3N/NSOXt/a8Gq4lCI0=' is not the same as any computed signature. Server used following string to sign: 'PUT 0 x-ms-blob-public-access:container x-ms-date:Tue, 22 May 2018 06:29:51 GMT x-ms-version:2009-09-19 /dsmsstor1/66-62-108-images restype:container'.Error

I think I have to put "x-ms-blob-public-access:container" in sig string also but I can't do it properly. Help me to form correct sig string. i.e (PUT\n\n\n0\n\n\n\n\n\n\n\n\nx-ms-date:". $date . "\nx-ms-version:".$version."\n/dsmsstor1/66-62-108-images\nrestype:container) My code ----------

<?php
date_default_timezone_set ( 'GMT' ); 
$date = date ( "D, j M Y H:i:s T" ); 
$version = "2009-09-19";
$account_key = 'xxxxxxxxx';

 $utf8_encode_str = utf8_encode ("PUT\n\n\n0\n\n\n\n\n\n\n\n\nx-ms-date:". $date . "\nx-ms-version:".$version."\n/dsmsstor1/66-62-108-images\nrestype:container" );
 $signature_str = base64_encode(hash_hmac('sha256',$utf8_encode_str, base64_decode($account_key), true));
  $header = array (
   "x-ms-date: " . $date,
   "x-ms-version: " . $version,
   "Authorization: SharedKey dsmsstor1:" . $signature_str,
   "Content-Length: 0",
   "x-ms-blob-public-access:container"
   );
   $url="https://dsmsstor1.blob.core.windows.net/66-62-108-images?restype=container";
  $ch = curl_init ();
  curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, false);
  curl_setopt ($ch, CURLOPT_CUSTOMREQUEST, 'PUT' );
  curl_setopt ($ch, CURLOPT_URL, $url );
  curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true );
  curl_setopt ($ch, CURLOPT_HTTPHEADER, $header);
  curl_setopt ($ch, CURLOPT_HEADER, True );
  $result = curl_exec ( $ch );
  echo ('Result<br/>');
    print_r($result);

    echo ('Error<br/>');
    print_r(curl_error($ch));

    curl_close($ch);
?>
1

1 Answers

0
votes

You're missing x-ms-blob-public-access:container in your $utf8_encode_str.

Please try by changing the following line of code:

$utf8_encode_str = utf8_encode ("PUT\n\n\n0\n\n\n\n\n\n\n\n\nx-ms-date:". $date . "\nx-ms-version:".$version."\n/dsmsstor1/66-62-108-images\nrestype:container" );

to

$utf8_encode_str = utf8_encode ("PUT\n\n\n0\n\n\n\n\n\n\n\n\nx-ms-blob-public-access:container\nx-ms-date:". $date . "\nx-ms-version:".$version."\n/dsmsstor1/66-62-108-images\nrestype:container" );