Sample code for 30+ languages & platforms
Unicode C++ Requires Chilkat v11.6.0+

Azure Files: Download to a File with Streaming

See more Azure Cloud Storage Examples

Read response headers before writing bytes. Only HTTP 200 is streamed into a staging file, then renamed after completion. The final filename must not already exist; create qa_output first. An interrupted transfer can leave a .part file. This is a fresh download, not a resumable transfer.

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 get file reference.

Chilkat Unicode C++ Downloads

Unicode C++
#include <CkStringBuilderW.h>
#include <CkJsonObjectW.h>
#include <CkRestW.h>
#include <CkAuthAzureStorageW.h>
#include <CkSocketW.h>
#include <CkXmlW.h>
#include <CkFileAccessW.h>
#include <CkStreamW.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".file.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"/examples/files/example.bin";
    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"File");
    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;
    }

    CkXmlW result;
    result.put_Strict(true);
    //  Use a new destination and a staging file. A failed transfer is never a completed output.
    CkFileAccessW files;
    if (files.FileExists(L"qa_output/example.bin") == true) {
        wprintf(L"Choose a new output filename.\n");
        return;
    }

    CkStreamW sink;
    sink.put_SinkFile(L"qa_output/example.bin.part");
    success = rest.SendReqNoBody(L"GET",resourcePath);
    if (success != true) {
        wprintf(L"Could not send download request.\n");
        return;
    }

    int httpStatus = rest.ReadResponseHeader();
    if (httpStatus < 0) {
        wprintf(L"Could not read response headers.\n");
        return;
    }

    if (rest.get_ResponseStatusCode() != 200) {
        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;
    }

    success = rest.ReadRespBodyStream(sink,false);
    if (success != true) {
        wprintf(L"Download incomplete; staging file is not a valid final output.\n");
        return;
    }

    success = files.FileRename(L"qa_output/example.bin.part",L"qa_output/example.bin");
    if (success != true) {
        wprintf(L"Could not publish the downloaded file.\n");
        return;
    }

    wprintf(L"Download completed to qa_output/example.bin.\n");
    }