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 Delphi ActiveX Downloads
var
success: Integer;
textCompare: TChilkatStringBuilder;
applyChange: Integer;
config: TChilkatJsonObject;
sameText1: Integer;
sameText2: Integer;
accountText: TChilkatStringBuilder;
allowedAccountChars: TChilkatStringBuilder;
charIndex: Integer;
endpoint: TChilkatStringBuilder;
resourcePath: WideString;
rest: TChilkatRest;
azAuth: TChilkatAuthAzureStorage;
connection: TChilkatSocket;
responseText: WideString;
result: TChilkatJsonObject;
policies: TChilkatXml;
sameText3: Integer;
policyCount: Integer;
policyIds: TChilkatHashtable;
policyIndex: Integer;
editIndex: Integer;
policyId: WideString;
sameText4: Integer;
sameText5: Integer;
policyStart: TCkDateTime;
policyExpiry: TCkDateTime;
begin
success := 0;
// StringBuilder compares text by value in every generated language.
textCompare := TChilkatStringBuilder.Create(Self);
applyChange := 0;
if (applyChange <> 1) then
begin
Memo1.Lines.Add('This replaces the stored access-policy collection. Review the target and deliberately enable applyChange.');
Exit;
end;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
success := 0;
config := TChilkatJsonObject.Create(Self);
config.Strict := 1;
success := config.LoadFile('qa_data/azure/storage.json');
if (success <> 1) then
begin
Memo1.Lines.Add('Could not load private Azure configuration.');
Exit;
end;
textCompare.Clear();
textCompare.SetString(config.StringOf('account'));
sameText1 := textCompare.ContentsEqual('',1);
if ((config.JsonTypeOf('account') <> 1) or (sameText1 = 1)) then
begin
Memo1.Lines.Add('Supply the storage account name.');
Exit;
end;
// 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)) then
begin
Memo1.Lines.Add('Supply the base64 account access key.');
Exit;
end;
accountText := TChilkatStringBuilder.Create(Self);
success := accountText.Append(config.StringOf('account'));
if (success <> 1) then
begin
Memo1.Lines.Add('Could not prepare the request.');
Exit;
end;
if ((accountText.Length < 3) or (accountText.Length > 24)) then
begin
Memo1.Lines.Add('Storage account names need 3-24 lowercase letters or digits.');
Exit;
end;
allowedAccountChars := TChilkatStringBuilder.Create(Self);
success := allowedAccountChars.Append('abcdefghijklmnopqrstuvwxyz0123456789');
if (success <> 1) then
begin
Memo1.Lines.Add('Could not prepare the request.');
Exit;
end;
charIndex := 0;
while (charIndex < accountText.Length) do
begin
if (allowedAccountChars.Contains(accountText.GetRange(charIndex,1,0),1) <> 1) then
begin
Memo1.Lines.Add('Storage account names need lowercase letters or digits.');
Exit;
end;
charIndex := charIndex + 1;
end;
endpoint := TChilkatStringBuilder.Create(Self);
success := endpoint.Append(config.StringOf('account'));
if (success <> 1) then
begin
Memo1.Lines.Add('Could not prepare the request.');
Exit;
end;
success := endpoint.Append('.table.core.windows.net');
if (success <> 1) then
begin
Memo1.Lines.Add('Could not prepare the request.');
Exit;
end;
// Table names contain only letters and digits and are case-insensitive.
// Entity PartitionKey and RowKey values are case-sensitive.
resourcePath := '/ExampleCustomers';
rest := TChilkatRest.Create(Self);
rest.ConnectTimeoutMs := 20000;
rest.IdleTimeoutMs := 60000;
azAuth := TChilkatAuthAzureStorage.Create(Self);
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.ControlInterface);
if (success <> 1) then
begin
Memo1.Lines.Add('Could not configure Shared Key signing.');
Exit;
end;
connection := TChilkatSocket.Create(Self);
connection.RequireSslCertVerify := 1;
connection.MaxReadIdleMs := 60000;
connection.MaxSendIdleMs := 60000;
success := connection.Connect(endpoint.GetAsString(),443,1,20000);
if (success <> 1) then
begin
Memo1.Lines.Add('TLS connection failed; inspect diagnostics privately.');
Exit;
end;
// Do not replay a write automatically after losing its response.
success := rest.UseConnection(connection.ControlInterface,0);
if (success <> 1) then
begin
Memo1.Lines.Add('Could not prepare the request.');
Exit;
end;
result := TChilkatJsonObject.Create(Self);
result.Strict := 1;
success := rest.AddHeader('DataServiceVersion','3.0;NetFx');
if (success <> 1) then
begin
Memo1.Lines.Add('Could not prepare the request.');
Exit;
end;
success := rest.AddHeader('MaxDataServiceVersion','3.0;NetFx');
if (success <> 1) then
begin
Memo1.Lines.Add('Could not prepare the request.');
Exit;
end;
success := rest.AddHeader('Content-Type','application/xml');
if (success <> 1) then
begin
Memo1.Lines.Add('Could not prepare the request.');
Exit;
end;
success := rest.AddHeader('Accept','application/xml');
if (success <> 1) then
begin
Memo1.Lines.Add('Could not prepare the request.');
Exit;
end;
success := rest.AddQueryParam('comp','acl');
if (success <> 1) then
begin
Memo1.Lines.Add('Could not prepare the request.');
Exit;
end;
responseText := rest.FullRequestNoBody('GET',resourcePath);
if (rest.LastMethodSuccess <> 1) then
begin
Memo1.Lines.Add('Request did not complete; inspect the target before retrying a write.');
Exit;
end;
if (rest.ResponseStatusCode <> 200) then
begin
Memo1.Lines.Add('HTTP status: ' + IntToStr(rest.ResponseStatusCode));
Memo1.Lines.Add('Request ID: ' + rest.ResponseHdrByName('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.
Exit;
end;
policies := TChilkatXml.Create(Self);
policies.Strict := 1;
success := policies.LoadXml(responseText);
if (success <> 1) then
begin
Memo1.Lines.Add('Invalid policy XML.');
Exit;
end;
textCompare.Clear();
textCompare.SetString(policies.Tag);
sameText3 := textCompare.ContentsEqual('SignedIdentifiers',1);
if (sameText3 <> 1) then
begin
Memo1.Lines.Add('Unexpected policy root.');
Exit;
end;
policyCount := policies.NumChildrenHavingTag('SignedIdentifier');
if (policyCount > 5) then
begin
Memo1.Lines.Add('Unexpected access policy count.');
Exit;
end;
policyIds := TChilkatHashtable.Create(Self);
policyIndex := 0;
editIndex := -1;
while (policyIndex < policyCount) do
begin
policies.I := policyIndex;
policyId := policies.GetChildContent('SignedIdentifier[i]|Id');
textCompare.Clear();
textCompare.SetString(policyId);
sameText4 := textCompare.ContentsEqual('',1);
if (sameText4 = 1) then
begin
Memo1.Lines.Add('Missing policy ID.');
Exit;
end;
if (policyIds.Contains(policyId) = 1) then
begin
Memo1.Lines.Add('Duplicate policy ID.');
Exit;
end;
success := policyIds.AddStr(policyId,'seen');
if (success <> 1) then
begin
Memo1.Lines.Add('Could not prepare the request.');
Exit;
end;
textCompare.Clear();
textCompare.SetString(policyId);
sameText5 := textCompare.ContentsEqual('example-read',1);
if (sameText5 = 1) then
begin
editIndex := policyIndex;
end;
policyIndex := policyIndex + 1;
end;
if (editIndex < 0) then
begin
if (policyCount >= 5) then
begin
Memo1.Lines.Add('All five policy slots are in use; do not remove an unrelated policy.');
Exit;
end;
editIndex := policyCount;
end;
policyStart := TCkDateTime.Create(Self);
policyExpiry := TCkDateTime.Create(Self);
success := policyStart.SetFromCurrentSystemTime();
if (success <> 1) then
begin
Memo1.Lines.Add('Could not prepare the request.');
Exit;
end;
success := policyExpiry.SetFromCurrentSystemTime();
if (success <> 1) then
begin
Memo1.Lines.Add('Could not prepare the request.');
Exit;
end;
success := policyStart.AddSeconds(-300);
if (success <> 1) then
begin
Memo1.Lines.Add('Could not prepare the request.');
Exit;
end;
success := policyExpiry.AddSeconds(900);
if (success <> 1) then
begin
Memo1.Lines.Add('Could not prepare the request.');
Exit;
end;
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) then
begin
Memo1.Lines.Add('TLS reconnection failed.');
Exit;
end;
success := rest.UseConnection(connection.ControlInterface,0);
if (success <> 1) then
begin
Memo1.Lines.Add('Could not prepare the request.');
Exit;
end;
responseText := rest.FullRequestString('PUT',resourcePath,policies.GetXml());
if (rest.LastMethodSuccess <> 1) then
begin
Memo1.Lines.Add('Request did not complete; inspect the target before retrying a write.');
Exit;
end;
if (rest.ResponseStatusCode <> 204) then
begin
Memo1.Lines.Add('HTTP status: ' + IntToStr(rest.ResponseStatusCode));
Memo1.Lines.Add('Request ID: ' + rest.ResponseHdrByName('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.
Exit;
end;
Memo1.Lines.Add('Stored policy updated; allow time for propagation.');