Sample code for 30+ languages & platforms
C

Azure Create Storage Account

See more Azure Storage Accounts Examples

Demonstrates how to create an Azure storage account.

Chilkat C Downloads

C
#include <C_CkHttp.h>
#include <C_CkJsonObject.h>
#include <C_CkHttpResponse.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkHttp http;
    HCkJsonObject jsonToken;
    HCkJsonObject jsonRequestBody;
    const char *url;
    HCkHttpResponse resp;
    HCkJsonObject json;
    const char *skuName;
    const char *skuTier;
    const char *kind;
    const char *id;
    const char *name;
    const char *v_type;
    const char *location;
    const char *propertiesNetworkAclsBypass;
    const char *propertiesNetworkAclsDefaultAction;
    BOOL propertiesSupportsHttpsTrafficOnly;
    BOOL propertiesEncryptionServicesFileEnabled;
    const char *propertiesEncryptionServicesFileLastEnabledTime;
    BOOL propertiesEncryptionServicesBlobEnabled;
    const char *propertiesEncryptionServicesBlobLastEnabledTime;
    const char *propertiesEncryptionKeySource;
    const char *propertiesAccessTier;
    const char *propertiesProvisioningState;
    const char *propertiesCreationTime;
    const char *propertiesPrimaryEndpointsDfs;
    const char *propertiesPrimaryEndpointsWeb;
    const char *propertiesPrimaryEndpointsBlob;
    const char *propertiesPrimaryEndpointsQueue;
    const char *propertiesPrimaryEndpointsTable;
    const char *propertiesPrimaryEndpointsFile;
    const char *propertiesPrimaryLocation;
    const char *propertiesStatusOfPrimary;
    const char *propertiesSecondaryLocation;
    const char *propertiesStatusOfSecondary;
    int i;
    int count_i;

    success = FALSE;

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

    http = CkHttp_Create();

    //  Load an OAuth2 access token previously fetched by this example:  Get Azure OAuth2 Access Token
    jsonToken = CkJsonObject_Create();
    success = CkJsonObject_LoadFile(jsonToken,"qa_data/tokens/azureToken.json");
    //  Assuming success..
    CkHttp_putAuthToken(http,CkJsonObject_stringOf(jsonToken,"access_token"));
    printf("AuthToken: %s\n",CkHttp_authToken(http));

    CkHttp_putAccept(http,"application/json");

    //  Create the following JSON:

    //  {
    //    "sku": {
    //      "name": "Standard_GRS"
    //    },
    //    "kind": "StorageV2",
    //    "location": "eastus2",
    //  }

    //  Use this online tool to generate the code from sample JSON: 
    //  Generate Code to Create JSON

    jsonRequestBody = CkJsonObject_Create();
    CkJsonObject_UpdateString(jsonRequestBody,"sku.name","Standard_GRS");
    CkJsonObject_UpdateString(jsonRequestBody,"kind","StorageV2");
    CkJsonObject_UpdateString(jsonRequestBody,"location","eastus2");

    url = "https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Storage/storageAccounts/{accountName}?api-version=2018-02-01";

    resp = CkHttpResponse_Create();
    success = CkHttp_HttpJson(http,"PUT",url,jsonRequestBody,"application/json",resp);
    if (success == FALSE) {
        printf("%s\n",CkHttp_lastErrorText(http));
        CkHttp_Dispose(http);
        CkJsonObject_Dispose(jsonToken);
        CkJsonObject_Dispose(jsonRequestBody);
        CkHttpResponse_Dispose(resp);
        return;
    }

    printf("Response Status Code: %d\n",CkHttpResponse_getStatusCode(resp));

    json = CkJsonObject_Create();
    CkJsonObject_Load(json,CkHttpResponse_bodyStr(resp));
    CkJsonObject_putEmitCompact(json,FALSE);
    printf("%s\n",CkJsonObject_emit(json));

    if (CkHttpResponse_getStatusCode(resp) >= 300) {
        printf("Failed.\n");
        CkHttp_Dispose(http);
        CkJsonObject_Dispose(jsonToken);
        CkJsonObject_Dispose(jsonRequestBody);
        CkHttpResponse_Dispose(resp);
        CkJsonObject_Dispose(json);
        return;
    }

    //  Successful requests to create a new account return a 202 status code with an empty response body. The storage account is created asynchronously. 
    //  If the account already exists or is being provisioned, the request response has a 200 return code with the configuration of the existing storage account in the response body.
    if (CkHttpResponse_getStatusCode(resp) == 202) {
        printf("Azure-AsyncOperation: %s\n",CkHttpResponse_getHeaderField(resp,"Azure-AsyncOperation"));
    }

    if (CkHttpResponse_getStatusCode(resp) == 200) {

        //  Parse a response like this:

        //  	{
        //  	  "sku": {
        //  	    "name": "Standard_GRS",
        //  	    "tier": "Standard"
        //  	  },
        //  	  "kind": "StorageV2",
        //  	  "id": "/subscriptions/6c42643b-ebef-45f0-b917-b3583b84a57f/resourceGroups/gChilkat/providers/Microsoft.Storage/storageAccounts/chilkatsoftware",
        //  	  "name": "chilkatsoftware",
        //  	  "type": "Microsoft.Storage/storageAccounts",
        //  	  "location": "eastus2",
        //  	  "tags": {},
        //  	  "properties": {
        //  	    "networkAcls": {
        //  	      "bypass": "AzureServices",
        //  	      "virtualNetworkRules": [
        //  	      ],
        //  	      "ipRules": [
        //  	      ],
        //  	      "defaultAction": "Allow"
        //  	    },
        //  	    "supportsHttpsTrafficOnly": false,
        //  	    "encryption": {
        //  	      "services": {
        //  	        "file": {
        //  	          "enabled": true,
        //  	          "lastEnabledTime": "2019-05-14T22:18:33.2246670Z"
        //  	        },
        //  	        "blob": {
        //  	          "enabled": true,
        //  	          "lastEnabledTime": "2019-05-14T22:18:33.2246670Z"
        //  	        }
        //  	      },
        //  	      "keySource": "Microsoft.Storage"
        //  	    },
        //  	    "accessTier": "Hot",
        //  	    "provisioningState": "Succeeded",
        //  
        //  	    "creationTime": "2019-05-14T22:18:33.1309165Z",
        //  	    "primaryEndpoints": {
        //  	      "dfs": "https://chilkatsoftware.dfs.core.windows.net/",
        //  	      "web": "https://chilkatsoftware.z20.web.core.windows.net/",
        //  	      "blob": "https://chilkatsoftware.blob.core.windows.net/",
        //  	      "queue": "https://chilkatsoftware.queue.core.windows.net/",
        //  	      "table": "https://chilkatsoftware.table.core.windows.net/",
        //  	      "file": "https://chilkatsoftware.file.core.windows.net/"
        //  	    },
        //  	    "primaryLocation": "eastus2",
        //  	    "statusOfPrimary": "available",
        //  	    "secondaryLocation": "centralus",
        //  	    "statusOfSecondary": "available"
        //  	  }
        //  	}

        //  Use this online tool to generate parsing code from sample JSON: 
        //  Generate Parsing Code from JSON

        skuName = CkJsonObject_stringOf(json,"sku.name");
        skuTier = CkJsonObject_stringOf(json,"sku.tier");
        kind = CkJsonObject_stringOf(json,"kind");
        id = CkJsonObject_stringOf(json,"id");
        name = CkJsonObject_stringOf(json,"name");
        v_type = CkJsonObject_stringOf(json,"type");
        location = CkJsonObject_stringOf(json,"location");
        propertiesNetworkAclsBypass = CkJsonObject_stringOf(json,"properties.networkAcls.bypass");
        propertiesNetworkAclsDefaultAction = CkJsonObject_stringOf(json,"properties.networkAcls.defaultAction");
        propertiesSupportsHttpsTrafficOnly = CkJsonObject_BoolOf(json,"properties.supportsHttpsTrafficOnly");
        propertiesEncryptionServicesFileEnabled = CkJsonObject_BoolOf(json,"properties.encryption.services.file.enabled");
        propertiesEncryptionServicesFileLastEnabledTime = CkJsonObject_stringOf(json,"properties.encryption.services.file.lastEnabledTime");
        propertiesEncryptionServicesBlobEnabled = CkJsonObject_BoolOf(json,"properties.encryption.services.blob.enabled");
        propertiesEncryptionServicesBlobLastEnabledTime = CkJsonObject_stringOf(json,"properties.encryption.services.blob.lastEnabledTime");
        propertiesEncryptionKeySource = CkJsonObject_stringOf(json,"properties.encryption.keySource");
        propertiesAccessTier = CkJsonObject_stringOf(json,"properties.accessTier");
        propertiesProvisioningState = CkJsonObject_stringOf(json,"properties.provisioningState");
        propertiesCreationTime = CkJsonObject_stringOf(json,"properties.creationTime");
        propertiesPrimaryEndpointsDfs = CkJsonObject_stringOf(json,"properties.primaryEndpoints.dfs");
        propertiesPrimaryEndpointsWeb = CkJsonObject_stringOf(json,"properties.primaryEndpoints.web");
        propertiesPrimaryEndpointsBlob = CkJsonObject_stringOf(json,"properties.primaryEndpoints.blob");
        propertiesPrimaryEndpointsQueue = CkJsonObject_stringOf(json,"properties.primaryEndpoints.queue");
        propertiesPrimaryEndpointsTable = CkJsonObject_stringOf(json,"properties.primaryEndpoints.table");
        propertiesPrimaryEndpointsFile = CkJsonObject_stringOf(json,"properties.primaryEndpoints.file");
        propertiesPrimaryLocation = CkJsonObject_stringOf(json,"properties.primaryLocation");
        propertiesStatusOfPrimary = CkJsonObject_stringOf(json,"properties.statusOfPrimary");
        propertiesSecondaryLocation = CkJsonObject_stringOf(json,"properties.secondaryLocation");
        propertiesStatusOfSecondary = CkJsonObject_stringOf(json,"properties.statusOfSecondary");
    }

    printf("Success.\n");


    CkHttp_Dispose(http);
    CkJsonObject_Dispose(jsonToken);
    CkJsonObject_Dispose(jsonRequestBody);
    CkHttpResponse_Dispose(resp);
    CkJsonObject_Dispose(json);

    }