Azure Blob: Upload Using a Service SAS
See more Azure Cloud Storage Examples
Read the private query token produced by the corresponding SAS-generation example and upload a small binary file to its matching account/path. Do not add a Shared Key Authorization header. If-None-Match: * prevents accidental overwrite; remove it only for intentional replacement. The same 5,000 MiB single-request service limit applies, but this example buffers input and is intended for small files.
Keep query values encoded exactly once. A 403 can mean expiry, start-time skew, wrong path, insufficient permissions, network restrictions or Shared Key being disabled. SAS is a bearer credential; do not print the URL or token.
Uses direct Chilkat REST calls with HTTPS certificate verification. Shared Key examples load a private qa_data/azure/storage.json file containing string fields account and access_key (the base64 account key). SAS consumers need only account there. Create local input/output directories before running. Examples target Azure public-cloud endpoints; adapt hostnames for other clouds or custom endpoints.
REST examples pin x-ms-version: 2026-04-06, and SAS generation uses the same service version. Complete-URL downloads use the supplied SAS version. This is a fully deployed version documented by Microsoft at review time. Older supported versions are not automatically obsolete. Transport completion and HTTP success are checked separately; writes are not automatically replayed after a lost response. Keep diagnostic dumps, keys and SAS URLs private.
Shared Key remains supported where allowed by account policy. Microsoft recommends Microsoft Entra ID, preferably managed identities where available, and user delegation SAS for delegated Blob access. Account-key SAS and Shared Key requests can be rejected when Shared Key access is disabled; these samples do not bypass that policy. See Azure data authorization.
Chilkat Delphi DLL Downloads
var
success: Boolean;
textCompare: HCkStringBuilder;
config: HCkJsonObject;
sameText1: Boolean;
endpoint: HCkStringBuilder;
resourcePath: PWideChar;
rest: HCkRest;
connection: HCkSocket;
responseBody: HCkStringBuilder;
result: HCkXml;
token: HCkStringBuilder;
data: HCkBinData;
begin
success := False;
// StringBuilder compares text by value in every generated language.
textCompare := CkStringBuilder_Create();
// Assumes Chilkat is unlocked. See Global Unlock Sample.
success := False;
config := CkJsonObject_Create();
CkJsonObject_putStrict(config,True);
success := CkJsonObject_LoadFile(config,'qa_data/azure/storage.json');
if (success <> True) then
begin
Memo1.Lines.Add('Could not load private Azure configuration.');
Exit;
end;
CkStringBuilder_Clear(textCompare);
CkStringBuilder_SetString(textCompare,CkJsonObject__stringOf(config,'account'));
sameText1 := CkStringBuilder_ContentsEqual(textCompare,'',True);
if ((CkJsonObject_JsonTypeOf(config,'account') <> 1) or (sameText1 = True)) then
begin
Memo1.Lines.Add('Supply the storage account name.');
Exit;
end;
endpoint := CkStringBuilder_Create();
success := CkStringBuilder_Append(endpoint,CkJsonObject__stringOf(config,'account'));
if (success <> True) then
begin
Memo1.Lines.Add('Could not prepare the request.');
Exit;
end;
success := CkStringBuilder_Append(endpoint,'.blob.core.windows.net');
if (success <> True) then
begin
Memo1.Lines.Add('Could not prepare the request.');
Exit;
end;
// 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 := CkRest_Create();
CkRest_putConnectTimeoutMs(rest,20000);
CkRest_putIdleTimeoutMs(rest,60000);
connection := CkSocket_Create();
CkSocket_putRequireSslCertVerify(connection,True);
CkSocket_putMaxReadIdleMs(connection,60000);
CkSocket_putMaxSendIdleMs(connection,60000);
success := CkSocket_Connect(connection,CkStringBuilder__getAsString(endpoint),443,True,20000);
if (success <> True) then
begin
Memo1.Lines.Add('TLS connection failed; inspect diagnostics privately.');
Exit;
end;
// Do not replay a write automatically after losing its response.
success := CkRest_UseConnection(rest,connection,False);
if (success <> True) then
begin
Memo1.Lines.Add('Could not prepare the request.');
Exit;
end;
responseBody := CkStringBuilder_Create();
result := CkXml_Create();
CkXml_putStrict(result,True);
success := CkRest_AddHeader(rest,'x-ms-version','2026-04-06');
if (success <> True) then
begin
Memo1.Lines.Add('Could not prepare the request.');
Exit;
end;
token := CkStringBuilder_Create();
success := CkStringBuilder_LoadFile(token,'qa_data/azure/blob-sas.txt','utf-8');
if (success <> True) then
begin
Memo1.Lines.Add('Could not load private SAS.');
Exit;
end;
success := CkStringBuilder_Trim(token);
if (success <> True) then
begin
Memo1.Lines.Add('Could not prepare the request.');
Exit;
end;
if (CkStringBuilder_getLength(token) = 0) then
begin
Memo1.Lines.Add('SAS file is empty.');
Exit;
end;
// Load the raw query token; do not pre-encode it or add Shared Key authorization.
success := CkRest_AddQueryParams(rest,CkStringBuilder__getAsString(token));
if (success <> True) then
begin
Memo1.Lines.Add('Could not prepare the request.');
Exit;
end;
success := CkRest_AddHeader(rest,'x-ms-blob-type','BlockBlob');
if (success <> True) then
begin
Memo1.Lines.Add('Could not prepare the request.');
Exit;
end;
success := CkRest_AddHeader(rest,'If-None-Match','*');
if (success <> True) then
begin
Memo1.Lines.Add('Could not prepare the request.');
Exit;
end;
success := CkRest_AddHeader(rest,'Content-Type','application/octet-stream');
if (success <> True) then
begin
Memo1.Lines.Add('Could not prepare the request.');
Exit;
end;
data := CkBinData_Create();
success := CkBinData_LoadFile(data,'qa_data/bin/sample.bin');
if (success <> True) then
begin
Memo1.Lines.Add('Could not read upload input.');
Exit;
end;
CkStringBuilder_Clear(responseBody);
success := CkRest_FullRequestBd(rest,'PUT',resourcePath,data,responseBody);
if (success <> True) then
begin
Memo1.Lines.Add('Request did not complete; outcome may be uncertain.');
Exit;
end;
if (CkRest_getResponseStatusCode(rest) <> 201) then
begin
Memo1.Lines.Add('HTTP status: ' + IntToStr(CkRest_getResponseStatusCode(rest)));
Memo1.Lines.Add('Azure error code: ' + CkRest__responseHdrByName(rest,'x-ms-error-code'));
Memo1.Lines.Add('Request ID: ' + CkRest__responseHdrByName(rest,'x-ms-request-id'));
// Inspect any error body privately; do not log credentials, SAS URLs or payloads.
Exit;
end;
Memo1.Lines.Add('Blob created using SAS.');
CkStringBuilder_Dispose(textCompare);
CkJsonObject_Dispose(config);
CkStringBuilder_Dispose(endpoint);
CkRest_Dispose(rest);
CkSocket_Dispose(connection);
CkStringBuilder_Dispose(responseBody);
CkXml_Dispose(result);
CkStringBuilder_Dispose(token);
CkBinData_Dispose(data);