Created
March 23, 2024 14:14
-
-
Save jovialcore/b673027abe502e0250e6f49a549e20b3 to your computer and use it in GitHub Desktop.
Upload media to azure blob storage and generate a media url
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
public function uploadToAzureCloud($request) | |
{ | |
try { | |
$storageAccountName = config('services.azure_storage.account_name'); | |
$containerName = config('services.azure_storage.container'); | |
$accessKey = config('services.azure_storage.key'); | |
if ($request->hasFile('file')) { | |
$file = $request->file('file'); | |
$orignalFileName = $file->getClientOriginalName(); | |
$blobName = $file->hashName(); | |
$fileSize = filesize($file->path()); | |
$dateTime = gmdate('D, d M Y H:i:s \G\M\T'); | |
$urlResource = "/$storageAccountName/$containerName/{$blobName}"; | |
$headerResource = "x-ms-blob-cache-control:max-age=3600\nx-ms-blob-type:BlockBlob\nx-ms-date:$dateTime\nx-ms-version:2019-12-12"; | |
// Generate signature | |
$arraysign = []; // initiate an empty array (don't remove this 🙂) | |
$arraysign[] = 'PUT'; /*HTTP Verb*/ | |
$arraysign[] = ''; /*Content-Encoding*/ | |
$arraysign[] = ''; /*Content-Language*/ | |
$arraysign[] = $fileSize; /*Content-Length (include value when zero)*/ | |
$arraysign[] = ''; /*Content-MD5*/ | |
$arraysign[] = 'image/png'; /*Content-Type*/ | |
$arraysign[] = ''; /*Date*/ | |
$arraysign[] = ''; /*If-Modified-Since */ | |
$arraysign[] = ''; /*If-Match*/ | |
$arraysign[] = ''; /*If-None-Match*/ | |
$arraysign[] = ''; /*If-Unmodified-Since*/ | |
$arraysign[] = ''; /*Range*/ | |
$arraysign[] = $headerResource; /*CanonicalizedHeaders*/ | |
$arraysign[] = $urlResource; /*CanonicalizedResource*/ | |
// converts the array to a string as required by MS | |
$str2sign = implode("\n", $arraysign); | |
$sig = base64_encode(hash_hmac('sha256', utf8_encode($str2sign), base64_decode($accessKey), true)); | |
$url = "https://$storageAccountName.blob.core.windows.net/$containerName/{$blobName}"; | |
// use GuzzleHttp\Client; | |
$client = new Client(); | |
$response = $client->request('PUT', $url, [ | |
'headers' => [ | |
'Authorization' => "SharedKey $storageAccountName:$sig", | |
'x-ms-blob-cache-control' => 'max-age=3600', | |
'x-ms-blob-type' => 'BlockBlob', | |
'x-ms-date' => $dateTime, | |
'x-ms-version' => '2019-12-12', | |
'Content-Type' => 'image/png', | |
'Content-Length' => $fileSize | |
], | |
'body' => fopen($file->path(), 'r'), | |
]); | |
if ($response->getStatusCode() == 201) { | |
// image sas token | |
$urlSasToken = config('services.azure_storage.sas_token'); | |
return | |
[ | |
'original_name' => $orignalFileName, | |
'media_url' => "$url?$urlSasToken", | |
] | |
; | |
} else { | |
return response()->json(['message' => 'Something went wrong']); | |
} | |
} | |
} catch (RequestException $e) { | |
// If there's an error, log the error message | |
$errorMessage = $e->getMessage(); | |
return response()->json(['error' => $errorMessage], 500); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment