Azure Blob: Acquire a Container Deletion Lease
See more Azure Cloud Storage Examples
Acquire a finite 15-second container lease and read the actual lease ID from Azure. A proposed ID belongs in x-ms-proposed-lease-id, not x-ms-lease-id. Acquire requires HTTP 201; renew/change/release use 200 and break uses 202.
A container lease protects container deletion, not writes to blobs within it. Durations are 15–60 seconds or infinite (-1). This sample uses a finite lease that expires naturally; keep its returned ID privately for renew/release/delete operations. A lost acquire response must be reconciled before retrying.
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.
Microsoft lease container reference.
Chilkat Unicode C++ Downloads
#include <CkStringBuilderW.h>
#include <CkJsonObjectW.h>
#include <CkRestW.h>
#include <CkAuthAzureStorageW.h>
#include <CkSocketW.h>
#include <CkXmlW.h>
void ChilkatSample(void)
{
bool success = false;
// StringBuilder compares text by value in every generated language.
CkStringBuilderW textCompare;
// 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 endpoint;
success = endpoint.Append(config.stringOf(L"account"));
if (success != true) {
wprintf(L"Could not prepare the request.\n");
return;
}
success = endpoint.Append(L".blob.core.windows.net");
if (success != true) {
wprintf(L"Could not prepare the request.\n");
return;
}
// resourcePath is already URL-encoded. Encode each path segment once when adapting it.
// Keep slash separators; names and paths are case-sensitive.
const wchar_t *resourcePath = L"/examplecontainer";
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"Blob");
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;
CkXmlW result;
result.put_Strict(true);
success = rest.AddQueryParam(L"restype",L"container");
if (success != true) {
wprintf(L"Could not prepare the request.\n");
return;
}
success = rest.AddQueryParam(L"comp",L"lease");
if (success != true) {
wprintf(L"Could not prepare the request.\n");
return;
}
success = rest.AddHeader(L"x-ms-lease-action",L"acquire");
if (success != true) {
wprintf(L"Could not prepare the request.\n");
return;
}
success = rest.AddHeader(L"x-ms-lease-duration",L"15");
if (success != true) {
wprintf(L"Could not prepare the request.\n");
return;
}
// Let Azure assign the lease ID. To propose one, use x-ms-proposed-lease-id.
responseText = rest.fullRequestNoBody(L"PUT",resourcePath);
if (rest.get_LastMethodSuccess() != true) {
wprintf(L"Request did not complete; outcome may be uncertain.\n");
return;
}
if (rest.get_ResponseStatusCode() != 201) {
wprintf(L"HTTP status: %d\n",rest.get_ResponseStatusCode());
wprintf(L"Azure error code: %s\n",rest.responseHdrByName(L"x-ms-error-code"));
wprintf(L"Request ID: %s\n",rest.responseHdrByName(L"x-ms-request-id"));
// Inspect any error body privately; do not log credentials, SAS URLs or payloads.
return;
}
const wchar_t *leaseId = rest.responseHdrByName(L"x-ms-lease-id");
textCompare.Clear();
textCompare.SetString(leaseId);
bool sameText3 = textCompare.ContentsEqual(L"",true);
if (sameText3 == true) {
wprintf(L"Lease accepted but no lease ID was returned; reconcile privately.\n");
return;
}
wprintf(L"Lease acquired for 15 seconds. Keep leaseId in application state.\n");
// Renew or release with this returned x-ms-lease-id before it expires.
}