Skip to content

Instantly share code, notes, and snippets.

@jovialcore
Last active March 22, 2024 09:51
Show Gist options
  • Save jovialcore/eeccda08192f0bd88627c169f03bc9eb to your computer and use it in GitHub Desktop.
Save jovialcore/eeccda08192f0bd88627c169f03bc9eb to your computer and use it in GitHub Desktop.
Simple Azure Rest API code to upload to azure services
<?php
// Learn more : https://learn.microsoft.com/en-gb/archive/blogs/ptsblog/how-to-upload-a-blob-to-azure-storage-by-rest-api
try {
// Replace with your Azure storage account details
$accountName = 'your account name';
$accountKey = "Your Azure Storage account key
$containerName = 'Your container name';
$blobName = 'blob name'; // image.png
// Date in UTC format
$date = gmdate('D, d M Y H:i:s T');
// Constructing the string to sign
$stringToSign = "GET\n\n\n\n\n\n\n\n\n\n\n\nx-ms-date:$date\nx-ms-version:2015-02-21\n/$accountName/$containerName/$blobName";
// Creating the HMAC-SHA256 signature
$signature = base64_encode(hash_hmac('sha256', utf8_encode($stringToSign), base64_decode($accountKey), true));
// Constructing the Authorization header
$authorizationHeader = "SharedKey $accountName:$signature";
// Guzzle client
$client = new Client();
// Making the request
$response = $client->request('GET', "https://$accountName.blob.core.windows.net/$containerName/$blobName", [
'headers' => [
'Authorization' => $authorizationHeader,
'x-ms-date' => $date,
'x-ms-version' => '2015-02-21'
]
]);
} catch (\Exception $e) {
// Handle the exception
return response()->json(['error' => $e->getMessage()], 500);
}
// Learn more : https://learn.microsoft.com/en-gb/archive/blogs/ptsblog/how-to-upload-a-blob-to-azure-storage-by-rest-api
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment