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 Unicode C++ Downloads
#include <CkStringBuilderW.h>
#include <CkJsonObjectW.h>
#include <CkRestW.h>
#include <CkAuthAzureStorageW.h>
#include <CkSocketW.h>
#include <CkXmlW.h>
#include <CkHashtableW.h>
#include <CkDateTimeW.h>
void ChilkatSample(void)
{
bool success = false;
// StringBuilder compares text by value in every generated language.
CkStringBuilderW textCompare;
bool applyChange = false;
if (applyChange != true) {
wprintf(L"This replaces the stored access-policy collection. Review the target and deliberately enable applyChange.\n");
return;
}
// Assumes Chilkat is unlocked. See Global Unlock Sample.
success = false;
CkJsonObjectW config;
config.put_Strict(true);
success = config.LoadFile(L"qa_data/azure/storage.json");
if (success != true) {
wprintf(L"Could not load private Azure configuration.\n");
return;
}
textCompare.Clear();
textCompare.SetString(config.stringOf(L"account"));
bool sameText1 = textCompare.ContentsEqual(L"",true);
if ((config.JsonTypeOf(L"account") != 1) || (sameText1 == true)) {
wprintf(L"Supply the storage account name.\n");
return;
}
// access_key is the base64 account key, not a connection string or SAS.
textCompare.Clear();
textCompare.SetString(config.stringOf(L"access_key"));
bool sameText2 = textCompare.ContentsEqual(L"",true);
if ((config.JsonTypeOf(L"access_key") != 1) || (sameText2 == true)) {
wprintf(L"Supply the base64 account access key.\n");
return;
}
CkStringBuilderW accountText;
success = accountText.Append(config.stringOf(L"account"));
if (success != true) {
wprintf(L"Could not prepare the request.\n");
return;
}
if ((accountText.get_Length() < 3) || (accountText.get_Length() > 24)) {
wprintf(L"Storage account names need 3-24 lowercase letters or digits.\n");
return;
}
CkStringBuilderW allowedAccountChars;
success = allowedAccountChars.Append(L"abcdefghijklmnopqrstuvwxyz0123456789");
if (success != true) {
wprintf(L"Could not prepare the request.\n");
return;
}
int charIndex = 0;
while ((charIndex < accountText.get_Length())) {
if (allowedAccountChars.Contains(accountText.getRange(charIndex,1,false),true) != true) {
wprintf(L"Storage account names need lowercase letters or digits.\n");
return;
}
charIndex = charIndex + 1;
}
CkStringBuilderW endpoint;
success = endpoint.Append(config.stringOf(L"account"));
if (success != true) {
wprintf(L"Could not prepare the request.\n");
return;
}
success = endpoint.Append(L".table.core.windows.net");
if (success != true) {
wprintf(L"Could not prepare the request.\n");
return;
}
// Table names contain only letters and digits and are case-insensitive.
// Entity PartitionKey and RowKey values are case-sensitive.
const wchar_t *resourcePath = L"/ExampleCustomers";
CkRestW rest;
rest.put_ConnectTimeoutMs(20000);
rest.put_IdleTimeoutMs(60000);
CkAuthAzureStorageW azAuth;
azAuth.put_Account(config.stringOf(L"account"));
azAuth.put_AccessKey(config.stringOf(L"access_key"));
azAuth.put_Service(L"Table");
azAuth.put_Scheme(L"SharedKey");
azAuth.put_XMsVersion(L"2026-04-06");
success = rest.SetAuthAzureStorage(azAuth);
if (success != true) {
wprintf(L"Could not configure Shared Key signing.\n");
return;
}
CkSocketW connection;
connection.put_RequireSslCertVerify(true);
connection.put_MaxReadIdleMs(60000);
connection.put_MaxSendIdleMs(60000);
success = connection.Connect(endpoint.getAsString(),443,true,20000);
if (success != true) {
wprintf(L"TLS connection failed; inspect diagnostics privately.\n");
return;
}
// Do not replay a write automatically after losing its response.
success = rest.UseConnection(connection,false);
if (success != true) {
wprintf(L"Could not prepare the request.\n");
return;
}
const wchar_t *responseText = 0;
CkJsonObjectW result;
result.put_Strict(true);
success = rest.AddHeader(L"DataServiceVersion",L"3.0;NetFx");
if (success != true) {
wprintf(L"Could not prepare the request.\n");
return;
}
success = rest.AddHeader(L"MaxDataServiceVersion",L"3.0;NetFx");
if (success != true) {
wprintf(L"Could not prepare the request.\n");
return;
}
success = rest.AddHeader(L"Content-Type",L"application/xml");
if (success != true) {
wprintf(L"Could not prepare the request.\n");
return;
}
success = rest.AddHeader(L"Accept",L"application/xml");
if (success != true) {
wprintf(L"Could not prepare the request.\n");
return;
}
success = rest.AddQueryParam(L"comp",L"acl");
if (success != true) {
wprintf(L"Could not prepare the request.\n");
return;
}
responseText = rest.fullRequestNoBody(L"GET",resourcePath);
if (rest.get_LastMethodSuccess() != true) {
wprintf(L"Request did not complete; inspect the target before retrying a write.\n");
return;
}
if (rest.get_ResponseStatusCode() != 200) {
wprintf(L"HTTP status: %d\n",rest.get_ResponseStatusCode());
wprintf(L"Request ID: %s\n",rest.responseHdrByName(L"x-ms-request-id"));
// 404 is missing, 409 is conflict, and 412 is a stale ETag: none are success.
// Keep response bodies private. Reconcile uncertain writes before retrying.
return;
}
CkXmlW policies;
policies.put_Strict(true);
success = policies.LoadXml(responseText);
if (success != true) {
wprintf(L"Invalid policy XML.\n");
return;
}
textCompare.Clear();
textCompare.SetString(policies.tag());
bool sameText3 = textCompare.ContentsEqual(L"SignedIdentifiers",true);
if (sameText3 != true) {
wprintf(L"Unexpected policy root.\n");
return;
}
int policyCount = policies.NumChildrenHavingTag(L"SignedIdentifier");
if (policyCount > 5) {
wprintf(L"Unexpected access policy count.\n");
return;
}
CkHashtableW policyIds;
int policyIndex = 0;
int editIndex = -1;
while ((policyIndex < policyCount)) {
policies.put_I(policyIndex);
const wchar_t *policyId = policies.getChildContent(L"SignedIdentifier[i]|Id");
textCompare.Clear();
textCompare.SetString(policyId);
bool sameText4 = textCompare.ContentsEqual(L"",true);
if (sameText4 == true) {
wprintf(L"Missing policy ID.\n");
return;
}
if (policyIds.Contains(policyId) == true) {
wprintf(L"Duplicate policy ID.\n");
return;
}
success = policyIds.AddStr(policyId,L"seen");
if (success != true) {
wprintf(L"Could not prepare the request.\n");
return;
}
textCompare.Clear();
textCompare.SetString(policyId);
bool sameText5 = textCompare.ContentsEqual(L"example-read",true);
if (sameText5 == true) {
editIndex = policyIndex;
}
policyIndex = policyIndex + 1;
}
if (editIndex < 0) {
if (policyCount >= 5) {
wprintf(L"All five policy slots are in use; do not remove an unrelated policy.\n");
return;
}
editIndex = policyCount;
}
CkDateTimeW policyStart;
CkDateTimeW policyExpiry;
success = policyStart.SetFromCurrentSystemTime();
if (success != true) {
wprintf(L"Could not prepare the request.\n");
return;
}
success = policyExpiry.SetFromCurrentSystemTime();
if (success != true) {
wprintf(L"Could not prepare the request.\n");
return;
}
success = policyStart.AddSeconds(-300);
if (success != true) {
wprintf(L"Could not prepare the request.\n");
return;
}
success = policyExpiry.AddSeconds(900);
if (success != true) {
wprintf(L"Could not prepare the request.\n");
return;
}
policies.put_I(editIndex);
policies.UpdateChildContent(L"SignedIdentifier[i]|Id",L"example-read");
policies.UpdateChildContent(L"SignedIdentifier[i]|AccessPolicy|Start",policyStart.getAsTimestamp(false));
policies.UpdateChildContent(L"SignedIdentifier[i]|AccessPolicy|Expiry",policyExpiry.getAsTimestamp(false));
policies.UpdateChildContent(L"SignedIdentifier[i]|AccessPolicy|Permission",L"r");
rest.Disconnect(10);
success = connection.Connect(endpoint.getAsString(),443,true,20000);
if (success != true) {
wprintf(L"TLS reconnection failed.\n");
return;
}
success = rest.UseConnection(connection,false);
if (success != true) {
wprintf(L"Could not prepare the request.\n");
return;
}
responseText = rest.fullRequestString(L"PUT",resourcePath,policies.getXml());
if (rest.get_LastMethodSuccess() != true) {
wprintf(L"Request did not complete; inspect the target before retrying a write.\n");
return;
}
if (rest.get_ResponseStatusCode() != 204) {
wprintf(L"HTTP status: %d\n",rest.get_ResponseStatusCode());
wprintf(L"Request ID: %s\n",rest.responseHdrByName(L"x-ms-request-id"));
// 404 is missing, 409 is conflict, and 412 is a stale ETag: none are success.
// Keep response bodies private. Reconcile uncertain writes before retrying.
return;
}
wprintf(L"Stored policy updated; allow time for propagation.\n");
}