Sample code for 30+ languages & platforms
PureBasic

Azure Create Storage Account

See more Azure Storage Accounts Examples

Demonstrates how to create an Azure storage account.

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkHttpResponse.pb"
IncludeFile "CkHttp.pb"
IncludeFile "CkJsonObject.pb"

Procedure ChilkatExample()

    success.i = 0

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

    http.i = CkHttp::ckCreate()
    If http.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    ; Load an OAuth2 access token previously fetched by this example:  Get Azure OAuth2 Access Token
    jsonToken.i = CkJsonObject::ckCreate()
    If jsonToken.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkJsonObject::ckLoadFile(jsonToken,"qa_data/tokens/azureToken.json")
    ; Assuming success..
    CkHttp::setCkAuthToken(http, CkJsonObject::ckStringOf(jsonToken,"access_token"))
    Debug "AuthToken: " + CkHttp::ckAuthToken(http)

    CkHttp::setCkAccept(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.i = CkJsonObject::ckCreate()
    If jsonRequestBody.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkJsonObject::ckUpdateString(jsonRequestBody,"sku.name","Standard_GRS")
    CkJsonObject::ckUpdateString(jsonRequestBody,"kind","StorageV2")
    CkJsonObject::ckUpdateString(jsonRequestBody,"location","eastus2")

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

    resp.i = CkHttpResponse::ckCreate()
    If resp.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkHttp::ckHttpJson(http,"PUT",url,jsonRequestBody,"application/json",resp)
    If success = 0
        Debug CkHttp::ckLastErrorText(http)
        CkHttp::ckDispose(http)
        CkJsonObject::ckDispose(jsonToken)
        CkJsonObject::ckDispose(jsonRequestBody)
        CkHttpResponse::ckDispose(resp)
        ProcedureReturn
    EndIf

    Debug "Response Status Code: " + Str(CkHttpResponse::ckStatusCode(resp))

    json.i = CkJsonObject::ckCreate()
    If json.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkJsonObject::ckLoad(json,CkHttpResponse::ckBodyStr(resp))
    CkJsonObject::setCkEmitCompact(json, 0)
    Debug CkJsonObject::ckEmit(json)

    If CkHttpResponse::ckStatusCode(resp) >= 300
        Debug "Failed."
        CkHttp::ckDispose(http)
        CkJsonObject::ckDispose(jsonToken)
        CkJsonObject::ckDispose(jsonRequestBody)
        CkHttpResponse::ckDispose(resp)
        CkJsonObject::ckDispose(json)
        ProcedureReturn
    EndIf

    ; 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::ckStatusCode(resp) = 202
        Debug "Azure-AsyncOperation: " + CkHttpResponse::ckGetHeaderField(resp,"Azure-AsyncOperation")
    EndIf

    If CkHttpResponse::ckStatusCode(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.s
        skuTier.s
        kind.s
        id.s
        name.s
        v_type.s
        location.s
        propertiesNetworkAclsBypass.s
        propertiesNetworkAclsDefaultAction.s
        propertiesSupportsHttpsTrafficOnly.i
        propertiesEncryptionServicesFileEnabled.i
        propertiesEncryptionServicesFileLastEnabledTime.s
        propertiesEncryptionServicesBlobEnabled.i
        propertiesEncryptionServicesBlobLastEnabledTime.s
        propertiesEncryptionKeySource.s
        propertiesAccessTier.s
        propertiesProvisioningState.s
        propertiesCreationTime.s
        propertiesPrimaryEndpointsDfs.s
        propertiesPrimaryEndpointsWeb.s
        propertiesPrimaryEndpointsBlob.s
        propertiesPrimaryEndpointsQueue.s
        propertiesPrimaryEndpointsTable.s
        propertiesPrimaryEndpointsFile.s
        propertiesPrimaryLocation.s
        propertiesStatusOfPrimary.s
        propertiesSecondaryLocation.s
        propertiesStatusOfSecondary.s
        i.i
        count_i.i

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

    Debug "Success."


    CkHttp::ckDispose(http)
    CkJsonObject::ckDispose(jsonToken)
    CkJsonObject::ckDispose(jsonRequestBody)
    CkHttpResponse::ckDispose(resp)
    CkJsonObject::ckDispose(json)


    ProcedureReturn
EndProcedure