Azure Table: Preserve Existing Policies When Updating a Table ACL
See more Azure Table Service Examples
Reads the existing XML policy collection, inserts or updates only example-read, preserves the other entries, and writes the full collection. Disabled by default. Refuses duplicate IDs and a sixth policy. This changes any SAS associated with example-read; it sets read permission and a short validity window.
Set Table ACL replaces the complete collection and has no ETag compare-and-swap protection here. Coordinate administrators to avoid lost concurrent edits. A changed policy may take up to 30 seconds to propagate; do not interpret an immediate SAS 403 as proof that the update failed.
Targets Azure Storage Table service in the public cloud, not Azure Cosmos DB for Table. Uses Chilkat 11.6.0 or later and an unlocked library. Direct data requests pin x-ms-version 2026-04-06 and OData 3.0. Shared Key examples read private qa_data/azure/storage.json with account and access_key string fields. Authentication examples explain their additional fields. Create private local directories before running; keep credentials, tokens, SAS, entity values, checkpoints and diagnostics outside public content and source control.
HTTPS certificates are verified. Transport completion and HTTP status are checked separately. Writes are not automatically retried after an uncertain outcome. Entity keys, ETags and continuation tokens remain strings. Examples operate on the existing ExampleCustomers table unless noted; review the targets before adapting them. Microsoft Entra ID with suitable Table data roles is preferred where available.
Chilkat PHP ActiveX Downloads
<?php
$success = 0;
// StringBuilder compares text by value in every generated language.
$textCompare = new COM("Chilkat.StringBuilder");
$applyChange = 0;
if ($applyChange != 1) {
print 'This replaces the stored access-policy collection. Review the target and deliberately enable applyChange.' . "\n";
exit;
}
// Assumes Chilkat is unlocked. See Global Unlock Sample.
$success = 0;
$config = new COM("Chilkat.JsonObject");
$config->Strict = 1;
$success = $config->LoadFile('qa_data/azure/storage.json');
if ($success != 1) {
print 'Could not load private Azure configuration.' . "\n";
exit;
}
$textCompare->Clear();
$textCompare->SetString($config->stringOf('account'));
$sameText1 = $textCompare->ContentsEqual('',1);
if (($config->JsonTypeOf('account') != 1) or ($sameText1 == 1)) {
print 'Supply the storage account name.' . "\n";
exit;
}
// access_key is the base64 account key, not a connection string or SAS.
$textCompare->Clear();
$textCompare->SetString($config->stringOf('access_key'));
$sameText2 = $textCompare->ContentsEqual('',1);
if (($config->JsonTypeOf('access_key') != 1) or ($sameText2 == 1)) {
print 'Supply the base64 account access key.' . "\n";
exit;
}
$accountText = new COM("Chilkat.StringBuilder");
$success = $accountText->Append($config->stringOf('account'));
if ($success != 1) {
print 'Could not prepare the request.' . "\n";
exit;
}
if (($accountText->Length < 3) or ($accountText->Length > 24)) {
print 'Storage account names need 3-24 lowercase letters or digits.' . "\n";
exit;
}
$allowedAccountChars = new COM("Chilkat.StringBuilder");
$success = $allowedAccountChars->Append('abcdefghijklmnopqrstuvwxyz0123456789');
if ($success != 1) {
print 'Could not prepare the request.' . "\n";
exit;
}
$charIndex = 0;
while (($charIndex < $accountText->Length)) {
if ($allowedAccountChars->Contains($accountText->getRange($charIndex,1,0),1) != 1) {
print 'Storage account names need lowercase letters or digits.' . "\n";
exit;
}
$charIndex = $charIndex + 1;
}
$endpoint = new COM("Chilkat.StringBuilder");
$success = $endpoint->Append($config->stringOf('account'));
if ($success != 1) {
print 'Could not prepare the request.' . "\n";
exit;
}
$success = $endpoint->Append('.table.core.windows.net');
if ($success != 1) {
print 'Could not prepare the request.' . "\n";
exit;
}
// Table names contain only letters and digits and are case-insensitive.
// Entity PartitionKey and RowKey values are case-sensitive.
$resourcePath = '/ExampleCustomers';
$rest = new COM("Chilkat.Rest");
$rest->ConnectTimeoutMs = 20000;
$rest->IdleTimeoutMs = 60000;
$azAuth = new COM("Chilkat.AuthAzureStorage");
$azAuth->Account = $config->stringOf('account');
$azAuth->AccessKey = $config->stringOf('access_key');
$azAuth->Service = 'Table';
$azAuth->Scheme = 'SharedKey';
$azAuth->XMsVersion = '2026-04-06';
$success = $rest->SetAuthAzureStorage($azAuth);
if ($success != 1) {
print 'Could not configure Shared Key signing.' . "\n";
exit;
}
$connection = new COM("Chilkat.Socket");
$connection->RequireSslCertVerify = 1;
$connection->MaxReadIdleMs = 60000;
$connection->MaxSendIdleMs = 60000;
$success = $connection->Connect($endpoint->getAsString(),443,1,20000);
if ($success != 1) {
print 'TLS connection failed; inspect diagnostics privately.' . "\n";
exit;
}
// Do not replay a write automatically after losing its response.
$success = $rest->UseConnection($connection,0);
if ($success != 1) {
print 'Could not prepare the request.' . "\n";
exit;
}
$result = new COM("Chilkat.JsonObject");
$result->Strict = 1;
$success = $rest->AddHeader('DataServiceVersion','3.0;NetFx');
if ($success != 1) {
print 'Could not prepare the request.' . "\n";
exit;
}
$success = $rest->AddHeader('MaxDataServiceVersion','3.0;NetFx');
if ($success != 1) {
print 'Could not prepare the request.' . "\n";
exit;
}
$success = $rest->AddHeader('Content-Type','application/xml');
if ($success != 1) {
print 'Could not prepare the request.' . "\n";
exit;
}
$success = $rest->AddHeader('Accept','application/xml');
if ($success != 1) {
print 'Could not prepare the request.' . "\n";
exit;
}
$success = $rest->AddQueryParam('comp','acl');
if ($success != 1) {
print 'Could not prepare the request.' . "\n";
exit;
}
$responseText = $rest->fullRequestNoBody('GET',$resourcePath);
if ($rest->LastMethodSuccess != 1) {
print 'Request did not complete; inspect the target before retrying a write.' . "\n";
exit;
}
if ($rest->ResponseStatusCode != 200) {
print 'HTTP status: ' . $rest->ResponseStatusCode . "\n";
print 'Request ID: ' . $rest->responseHdrByName('x-ms-request-id') . "\n";
// 404 is missing, 409 is conflict, and 412 is a stale ETag: none are success.
// Keep response bodies private. Reconcile uncertain writes before retrying.
exit;
}
$policies = new COM("Chilkat.Xml");
$policies->Strict = 1;
$success = $policies->LoadXml($responseText);
if ($success != 1) {
print 'Invalid policy XML.' . "\n";
exit;
}
$textCompare->Clear();
$textCompare->SetString($policies->Tag);
$sameText3 = $textCompare->ContentsEqual('SignedIdentifiers',1);
if ($sameText3 != 1) {
print 'Unexpected policy root.' . "\n";
exit;
}
$policyCount = $policies->NumChildrenHavingTag('SignedIdentifier');
if ($policyCount > 5) {
print 'Unexpected access policy count.' . "\n";
exit;
}
$policyIds = new COM("Chilkat.Hashtable");
$policyIndex = 0;
$editIndex = -1;
while (($policyIndex < $policyCount)) {
$policies->I = $policyIndex;
$policyId = $policies->getChildContent('SignedIdentifier[i]|Id');
$textCompare->Clear();
$textCompare->SetString($policyId);
$sameText4 = $textCompare->ContentsEqual('',1);
if ($sameText4 == 1) {
print 'Missing policy ID.' . "\n";
exit;
}
if ($policyIds->Contains($policyId) == 1) {
print 'Duplicate policy ID.' . "\n";
exit;
}
$success = $policyIds->AddStr($policyId,'seen');
if ($success != 1) {
print 'Could not prepare the request.' . "\n";
exit;
}
$textCompare->Clear();
$textCompare->SetString($policyId);
$sameText5 = $textCompare->ContentsEqual('example-read',1);
if ($sameText5 == 1) {
$editIndex = $policyIndex;
}
$policyIndex = $policyIndex + 1;
}
if ($editIndex < 0) {
if ($policyCount >= 5) {
print 'All five policy slots are in use; do not remove an unrelated policy.' . "\n";
exit;
}
$editIndex = $policyCount;
}
$policyStart = new COM("Chilkat.CkDateTime");
$policyExpiry = new COM("Chilkat.CkDateTime");
$success = $policyStart->SetFromCurrentSystemTime();
if ($success != 1) {
print 'Could not prepare the request.' . "\n";
exit;
}
$success = $policyExpiry->SetFromCurrentSystemTime();
if ($success != 1) {
print 'Could not prepare the request.' . "\n";
exit;
}
$success = $policyStart->AddSeconds(-300);
if ($success != 1) {
print 'Could not prepare the request.' . "\n";
exit;
}
$success = $policyExpiry->AddSeconds(900);
if ($success != 1) {
print 'Could not prepare the request.' . "\n";
exit;
}
$policies->I = $editIndex;
$policies->UpdateChildContent('SignedIdentifier[i]|Id','example-read');
$policies->UpdateChildContent('SignedIdentifier[i]|AccessPolicy|Start',$policyStart->getAsTimestamp(0));
$policies->UpdateChildContent('SignedIdentifier[i]|AccessPolicy|Expiry',$policyExpiry->getAsTimestamp(0));
$policies->UpdateChildContent('SignedIdentifier[i]|AccessPolicy|Permission','r');
$rest->Disconnect(10);
$success = $connection->Connect($endpoint->getAsString(),443,1,20000);
if ($success != 1) {
print 'TLS reconnection failed.' . "\n";
exit;
}
$success = $rest->UseConnection($connection,0);
if ($success != 1) {
print 'Could not prepare the request.' . "\n";
exit;
}
$responseText = $rest->fullRequestString('PUT',$resourcePath,$policies->getXml());
if ($rest->LastMethodSuccess != 1) {
print 'Request did not complete; inspect the target before retrying a write.' . "\n";
exit;
}
if ($rest->ResponseStatusCode != 204) {
print 'HTTP status: ' . $rest->ResponseStatusCode . "\n";
print 'Request ID: ' . $rest->responseHdrByName('x-ms-request-id') . "\n";
// 404 is missing, 409 is conflict, and 412 is a stale ETag: none are success.
// Keep response bodies private. Reconcile uncertain writes before retrying.
exit;
}
print 'Stored policy updated; allow time for propagation.' . "\n";
?>