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

Azure Storage: Create a Short-Lived Account SAS for Blob Writes

See more Azure Cloud Storage Examples

Create an HTTPS-only account SAS for Blob object writes (ss=b, srt=o, sp=w). It does not grant listing or deletion, but it still covers matching objects across the account; it is not constrained to one container or blob. Prefer a narrowly scoped service/user delegation SAS when that is the intended scope. Account SAS cannot refer to a stored access policy.

The token starts five minutes in the past for clock skew and expires fifteen minutes from now; account policy may require a shorter interval. Save it to a private token file and never log it. Generating a token locally does not validate the key, permissions, resource existence or account policy. Protect the signing account key and prefer short-lived delegation.

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 create account sas reference.

Chilkat C++ Downloads

C++
#include <CkStringBuilder.h>
#include <CkJsonObject.h>
#include <CkAuthAzureSAS.h>
#include <CkDateTime.h>
#include <CkFileAccess.h>

void ChilkatSample(void)
    {
    bool success = false;

    //  StringBuilder compares text by value in every generated language.
    CkStringBuilder textCompare;
    //  Assumes Chilkat is unlocked. See Global Unlock Sample.
    success = false;
    CkJsonObject config;
    config.put_Strict(true);
    success = config.LoadFile("qa_data/azure/storage.json");
    if (success != true) {
        std::cout << "Could not load private Azure configuration." << "\r\n";
        return;
    }

    textCompare.Clear();
    textCompare.SetString(config.stringOf("account"));
    bool sameText1 = textCompare.ContentsEqual("",true);
    if ((config.JsonTypeOf("account") != 1) || (sameText1 == true)) {
        std::cout << "Supply the storage account name." << "\r\n";
        return;
    }

    //  access_key is the base64 account key, not a connection string or SAS.
    textCompare.Clear();
    textCompare.SetString(config.stringOf("access_key"));
    bool sameText2 = textCompare.ContentsEqual("",true);
    if ((config.JsonTypeOf("access_key") != 1) || (sameText2 == true)) {
        std::cout << "Supply the base64 account access key." << "\r\n";
        return;
    }

    CkAuthAzureSAS sas;
    sas.put_AccessKey(config.stringOf("access_key"));
    //  Commas represent LF separators; omitted optional fields still occupy their positions.
    //  This layout includes signedEncryptionScope (ses), required since version 2020-12-06.
    sas.put_StringToSign("account,sp,ss,srt,st,se,sip,spr,sv,ses,");
    success = sas.SetNonTokenParam("account",config.stringOf("account"));
    if (success != true) {
        std::cout << "Could not prepare the request." << "\r\n";
        return;
    }

    success = sas.SetTokenParam("ss","ss","b");
    if (success != true) {
        std::cout << "Could not prepare the request." << "\r\n";
        return;
    }

    success = sas.SetTokenParam("srt","srt","o");
    if (success != true) {
        std::cout << "Could not prepare the request." << "\r\n";
        return;
    }

    success = sas.SetTokenParam("sp","sp","w");
    if (success != true) {
        std::cout << "Could not prepare the request." << "\r\n";
        return;
    }

    success = sas.SetTokenParam("spr","spr","https");
    if (success != true) {
        std::cout << "Could not prepare the request." << "\r\n";
        return;
    }

    success = sas.SetTokenParam("sv","sv","2026-04-06");
    if (success != true) {
        std::cout << "Could not prepare the request." << "\r\n";
        return;
    }

    CkDateTime clock;
    success = clock.SetFromCurrentSystemTime();
    if (success != true) {
        std::cout << "Could not prepare the request." << "\r\n";
        return;
    }

    success = clock.AddSeconds(-300);
    if (success != true) {
        std::cout << "Could not prepare the request." << "\r\n";
        return;
    }

    success = sas.SetTokenParam("st","st",clock.getAsTimestamp(false));
    if (success != true) {
        std::cout << "Could not prepare the request." << "\r\n";
        return;
    }

    success = clock.AddSeconds(1200);
    if (success != true) {
        std::cout << "Could not prepare the request." << "\r\n";
        return;
    }

    success = sas.SetTokenParam("se","se",clock.getAsTimestamp(false));
    if (success != true) {
        std::cout << "Could not prepare the request." << "\r\n";
        return;
    }

    //  Start is five minutes ago; expiry is fifteen minutes from now.
    //  Respect any stricter account SAS-expiration policy.
    const char *token = sas.generateToken();
    if (sas.get_LastMethodSuccess() != true) {
        std::cout << "Could not generate SAS." << "\r\n";
        return;
    }

    CkFileAccess files;
    success = files.WriteEntireTextFile("qa_data/azure/account-sas.txt",token,"utf-8",false);
    if (success != true) {
        std::cout << "Could not save token privately." << "\r\n";
        return;
    }

    std::cout << "SAS saved privately; do not print the token." << "\r\n";
    }