Sample code for 30+ languages & platforms
Unicode C

Set Container ACL

See more Azure Cloud Storage Examples

Azure Storage Blob Service REST API: Sample code to set the permissions of a container.

Chilkat Unicode C Downloads

Unicode C
#include <C_CkRestW.h>
#include <C_CkAuthAzureStorageW.h>
#include <C_CkPrngW.h>
#include <C_CkDateTimeW.h>
#include <C_CkXmlW.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkRestW rest;
    BOOL bTls;
    int port;
    BOOL bAutoReconnect;
    HCkAuthAzureStorageW azAuth;
    HCkPrngW prng;
    const wchar_t *randomId;
    HCkDateTimeW dt;
    BOOL bLocal;
    const wchar_t *curDtStr;
    const wchar_t *expireDtStr;
    HCkXmlW xml;
    HCkXmlW xSignedId;
    HCkXmlW xAccessPolicy;
    const wchar_t *responseStr;

    success = FALSE;

    // Azure Blob Service Example: Set Container ACL
    // See also: https://msdn.microsoft.com/en-us/library/azure/dd179391.aspx

    // This example requires the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    rest = CkRestW_Create();

    // Connect to the Azure Storage Blob Service
    bTls = TRUE;
    port = 443;
    bAutoReconnect = TRUE;
    // In this example, the storage account name is "chilkat".
    success = CkRestW_Connect(rest,L"chilkat.blob.core.windows.net",port,bTls,bAutoReconnect);
    if (success != TRUE) {
        wprintf(L"%s\n",CkRestW_lastErrorText(rest));
        CkRestW_Dispose(rest);
        return;
    }

    // Provide Azure Cloud credentials for the REST call.
    azAuth = CkAuthAzureStorageW_Create();
    CkAuthAzureStorageW_putAccessKey(azAuth,L"AZURE_ACCESS_KEY");
    // The account name used here should match the 1st part of the domain passed in the call to Connect (above).
    CkAuthAzureStorageW_putAccount(azAuth,L"chilkat");
    CkAuthAzureStorageW_putScheme(azAuth,L"SharedKey");
    CkAuthAzureStorageW_putService(azAuth,L"Blob");
    // This causes the "x-ms-version: 2021-08-06" header to be automatically added.
    CkAuthAzureStorageW_putXMsVersion(azAuth,L"2021-08-06");
    success = CkRestW_SetAuthAzureStorage(rest,azAuth);

    // Note: The application does not need to explicitly set the following
    // headers: x-ms-date, Authorization.  These headers
    // are automatically set by Chilkat.

    // The XML body of the request will look like this:

    // <?xml version="1.0" encoding="utf-8"?>
    // <SignedIdentifiers>
    //   <SignedIdentifier> 
    //     <Id>unique-character-value-of-up-to-64-chars</Id>
    //     <AccessPolicy>
    //       <Start>start-time</Start>
    //       <Expiry>expiry-time</Expiry>
    //       <Permission>abbreviated-permission-list</Permission>
    //     </AccessPolicy>
    //   </SignedIdentifier>
    // </SignedIdentifiers>

    // Generate a random 32-character string.
    prng = CkPrngW_Create();
    randomId = CkPrngW_randomString(prng,32,TRUE,TRUE,TRUE);

    // Get the current system date/time in ISO 8061 format
    dt = CkDateTimeW_Create();
    CkDateTimeW_SetFromCurrentSystemTime(dt);
    bLocal = FALSE;
    // Get the current date/time in ISO 8061 UTC format.
    curDtStr = CkDateTimeW_getAsTimestamp(dt,bLocal);
    // The expire time will be 365 days in the future.
    success = CkDateTimeW_AddDays(dt,365);
    expireDtStr = CkDateTimeW_getAsTimestamp(dt,bLocal);

    // Build the request:
    xml = CkXmlW_Create();
    CkXmlW_putTag(xml,L"SignedIdentifiers");
    xSignedId = CkXmlW_NewChild(xml,L"SignedIdentifier",L"");
    CkXmlW_NewChild2(xSignedId,L"Id",randomId);
    xAccessPolicy = CkXmlW_NewChild(xSignedId,L"AccessPolicy",L"");
    CkXmlW_NewChild2(xAccessPolicy,L"Start",curDtStr);
    CkXmlW_NewChild2(xAccessPolicy,L"Expiry",expireDtStr);
    CkXmlW_NewChild2(xAccessPolicy,L"Permission",L"rwd");
    CkXmlW_Dispose(xAccessPolicy);
    CkXmlW_Dispose(xSignedId);

    // Show the XML..
    wprintf(L"%s\n",CkXmlW_getXml(xml));

    // The expected response is a 200 response status code with no response body.
    responseStr = CkRestW_fullRequestString(rest,L"PUT",L"/mycontainer?restype=container&comp=acl",CkXmlW_getXml(xml));
    if (CkRestW_getLastMethodSuccess(rest) != TRUE) {
        wprintf(L"%s\n",CkRestW_lastErrorText(rest));
        CkRestW_Dispose(rest);
        CkAuthAzureStorageW_Dispose(azAuth);
        CkPrngW_Dispose(prng);
        CkDateTimeW_Dispose(dt);
        CkXmlW_Dispose(xml);
        return;
    }

    // When successful, the Azure Storage service will respond with a 200 response status code,
    // with no response body.

    if (CkRestW_getResponseStatusCode(rest) != 200) {
        // Examine the request/response to see what happened.
        wprintf(L"response status code = %d\n",CkRestW_getResponseStatusCode(rest));
        wprintf(L"response status text = %s\n",CkRestW_responseStatusText(rest));
        wprintf(L"response header: %s\n",CkRestW_responseHeader(rest));
        wprintf(L"response body (if any): %s\n",responseStr);
        wprintf(L"---\n");
        wprintf(L"LastRequestStartLine: %s\n",CkRestW_lastRequestStartLine(rest));
        wprintf(L"LastRequestHeader: %s\n",CkRestW_lastRequestHeader(rest));
        CkRestW_Dispose(rest);
        CkAuthAzureStorageW_Dispose(azAuth);
        CkPrngW_Dispose(prng);
        CkDateTimeW_Dispose(dt);
        CkXmlW_Dispose(xml);
        return;
    }

    wprintf(L"Success.\n");


    CkRestW_Dispose(rest);
    CkAuthAzureStorageW_Dispose(azAuth);
    CkPrngW_Dispose(prng);
    CkDateTimeW_Dispose(dt);
    CkXmlW_Dispose(xml);

    }