Azure Storage: Authenticate with Entra Client Credentials
See more Azure Cloud Storage Examples
Supply account, tenant_id (directory GUID), client_id and client_secret in private qa_data/azure/storage.json. The confidential application obtains an app-only token with the https://storage.azure.com/.default scope, then reads blob properties. Assign a Blob data role, such as Storage Blob Data Reader; a management-plane Contributor role alone does not grant Blob data access. Use this secret-based flow only where the application can keep its credentials private; certificates or managed identities are alternatives.
The token is saved to private qa_data/azure/storage-token.json with an expires_at timestamp for the related examples. Cache it until shortly before expiration, then repeat token acquisition. This flow does not use a refresh token. A 401 and a 403 need different investigation: token validity/audience versus data permissions or account policy. Allow time for RBAC changes to propagate.
Uses direct Chilkat.Rest requests and Azure public-cloud endpoints with verified HTTPS. REST requests select service version 2026-04-06. Create local input/output directories before running. Account-key examples read account and access_key string fields from private qa_data/azure/storage.json; do not embed secrets in distributed applications. Entra examples explain their own configuration. Keep credentials, bearer tokens, SAS URLs and diagnostic dumps private. Shared Key is supported only where allowed by account policy; Entra authentication is preferred where available.
Transport success and HTTP status are checked separately. Writes are not automatically replayed after an uncertain response. IDs, ETags, continuation markers and file sizes are strings; counters and explicitly bounded offsets use int. No webhook or web server is required.
Chilkat PHP Downloads
<?php
use Chilkat2\DateTime;
use Chilkat2\JsonObject;
use Chilkat2\Rest;
use Chilkat2\Socket;
use Chilkat2\StringBuilder;
use Chilkat2\Xml;
$success = false;
// StringBuilder compares text by value in every generated language.
$textCompare = new StringBuilder();
// Assumes Chilkat is unlocked. See Global Unlock Sample.
$success = false;
$config = new JsonObject();
$config->Strict = true;
try {
$config->LoadFile('qa_data/azure/storage.json');
} catch (\Chilkat2\Exception $e) {
echo "Could not load private Azure configuration.\n";
exit;
}
$textCompare->Clear();
$textCompare->SetString($config->StringOf('account'));
$sameText1 = $textCompare->ContentsEqual('', true);
if (($config->JsonTypeOf('account') != 1) || ($sameText1)) {
echo "Supply the storage account name.\n";
exit;
}
$identity = new Rest();
$identity->ConnectTimeoutMs = 20000;
$identity->IdleTimeoutMs = 60000;
$expires = new DateTime();
try {
$expires->SetFromCurrentSystemTime();
} catch (\Chilkat2\Exception $e) {
echo "Could not prepare the request.\n";
exit;
}
$textCompare->Clear();
$textCompare->SetString($config->StringOf('tenant_id'));
$sameText2 = $textCompare->ContentsEqual('', true);
if (($config->JsonTypeOf('tenant_id') != 1) || ($sameText2)) {
echo "Supply tenant_id in private storage.json.\n";
exit;
}
$textCompare->Clear();
$textCompare->SetString($config->StringOf('client_id'));
$sameText3 = $textCompare->ContentsEqual('', true);
if (($config->JsonTypeOf('client_id') != 1) || ($sameText3)) {
echo "Supply client_id in private storage.json.\n";
exit;
}
$textCompare->Clear();
$textCompare->SetString($config->StringOf('client_secret'));
$sameText4 = $textCompare->ContentsEqual('', true);
if (($config->JsonTypeOf('client_secret') != 1) || ($sameText4)) {
echo "Supply client_secret in private storage.json.\n";
exit;
}
$identityTls = new Socket();
$identityTls->RequireSslCertVerify = true;
try {
$identityTls->Connect('login.microsoftonline.com', 443, true, 20000);
} catch (\Chilkat2\Exception $e) {
echo "Identity TLS connection failed.\n";
exit;
}
try {
$identity->UseConnection($identityTls, false);
$tokenPath = new StringBuilder();
// tenant_id is the application's directory GUID, not common or organizations.
$tokenPath->Append('/');
$tokenPath->Append($config->StringOf('tenant_id'));
$tokenPath->Append('/oauth2/v2.0/token');
$identity->AddQueryParam('grant_type', 'client_credentials');
$identity->AddQueryParam('client_id', $config->StringOf('client_id'));
$identity->AddQueryParam('client_secret', $config->StringOf('client_secret'));
$identity->AddQueryParam('scope', 'https://storage.azure.com/.default');
} catch (\Chilkat2\Exception $e) {
echo "Could not prepare the request.\n";
exit;
}
// FullRequestFormUrlEncoded serializes these parameters in the POST body.
try {
$tokenText = $identity->FullRequestFormUrlEncoded('POST', $tokenPath->GetAsString());
} catch (\Chilkat2\Exception $e) {
echo "Token request did not complete; inspect diagnostics privately.\n";
exit;
}
if ($identity->ResponseStatusCode != 200) {
echo "Token request failed; inspect the status privately, never print the response token.\n";
exit;
}
$token = new JsonObject();
$token->Strict = true;
try {
$token->Load($tokenText);
} catch (\Chilkat2\Exception $e) {
echo "Expected token JSON.\n";
exit;
}
$textCompare->Clear();
$textCompare->SetString($token->StringOf('access_token'));
$sameText5 = $textCompare->ContentsEqual('', true);
$textCompare->Clear();
$textCompare->SetString($token->StringOf('token_type'));
$sameText6 = $textCompare->ContentsEqual('Bearer', true);
if (($token->JsonTypeOf('access_token') != 1) || (($sameText5) || (!$sameText6))) {
echo "Missing bearer token.\n";
exit;
}
$seconds = $token->IntOf('expires_in');
if (($seconds < 120) || ($seconds > 86400)) {
echo "Unexpected token lifetime.\n";
exit;
}
try {
$expires->AddSeconds($seconds);
$token->UpdateString('expires_at', $expires->GetAsTimestamp(false));
} catch (\Chilkat2\Exception $e) {
echo "Could not prepare the request.\n";
exit;
}
try {
$token->WriteFile('qa_data/azure/storage-token.json');
} catch (\Chilkat2\Exception $e) {
echo "Could not persist token privately.\n";
exit;
}
$accessToken = $token->StringOf('access_token');
$endpoint = new StringBuilder();
try {
$endpoint->Append($config->StringOf('account'));
$endpoint->Append('.blob.core.windows.net');
} catch (\Chilkat2\Exception $e) {
echo "Could not prepare the request.\n";
exit;
}
// resourcePath is already URL-encoded. Encode each path segment once when adapting it.
// Keep slash separators; names and paths are case-sensitive.
$resourcePath = '/examplecontainer/example.bin';
$rest = new Rest();
$rest->ConnectTimeoutMs = 20000;
$rest->IdleTimeoutMs = 60000;
$connection = new Socket();
$connection->RequireSslCertVerify = true;
$connection->MaxReadIdleMs = 60000;
$connection->MaxSendIdleMs = 60000;
try {
$connection->Connect($endpoint->GetAsString(), 443, true, 20000);
} catch (\Chilkat2\Exception $e) {
echo "TLS connection failed; inspect diagnostics privately.\n";
exit;
}
// Do not replay a write automatically after losing its response.
try {
$rest->UseConnection($connection, false);
$result = new Xml();
$result->Strict = true;
$rest->AddHeader('x-ms-version', '2026-04-06');
$bearer = new StringBuilder();
$bearer->Append('Bearer ');
$bearer->Append($accessToken);
$rest->AddHeader('Authorization', $bearer->GetAsString());
} catch (\Chilkat2\Exception $e) {
echo "Could not prepare the request.\n";
exit;
}
try {
$responseText = $rest->FullRequestNoBody('HEAD', $resourcePath);
} catch (\Chilkat2\Exception $e) {
echo "Request did not complete; outcome may be uncertain.\n";
exit;
}
if ($rest->ResponseStatusCode != 200) {
echo 'HTTP status: ', $rest->ResponseStatusCode, "\n";
echo 'Azure error code: ', $rest->ResponseHdrByName('x-ms-error-code'), "\n";
echo 'Request ID: ', $rest->ResponseHdrByName('x-ms-request-id'), "\n";
// Inspect any error body privately; do not log credentials, SAS URLs or payloads.
exit;
}
echo "Storage token obtained and blob properties read.\n";
echo 'ETag: ', $rest->ResponseHdrByName('ETag'), "\n";