Swift
Swift
Azure Create Storage Account
See more Azure Storage Accounts Examples
Demonstrates how to create an Azure storage account.Chilkat Swift Downloads
func chilkatTest() {
var success: Bool = false
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
let http = CkoHttp()!
// Load an OAuth2 access token previously fetched by this example: Get Azure OAuth2 Access Token
let jsonToken = CkoJsonObject()!
success = jsonToken.loadFile(path: "qa_data/tokens/azureToken.json")
// Assuming success..
http.authToken = jsonToken.string(of: "access_token")
print("AuthToken: \(http.authToken!)")
http.accept = "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
let jsonRequestBody = CkoJsonObject()!
jsonRequestBody.updateString(jsonPath: "sku.name", value: "Standard_GRS")
jsonRequestBody.updateString(jsonPath: "kind", value: "StorageV2")
jsonRequestBody.updateString(jsonPath: "location", value: "eastus2")
var url: String? = "https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Storage/storageAccounts/{accountName}?api-version=2018-02-01"
let resp = CkoHttpResponse()!
success = http.httpJson(verb: "PUT", url: url, json: jsonRequestBody, contentType: "application/json", response: resp)
if success == false {
print("\(http.lastErrorText!)")
return
}
print("Response Status Code: \(resp.statusCode.intValue)")
let json = CkoJsonObject()!
json.load(json: resp.bodyStr)
json.emitCompact = false
print("\(json.emit()!)")
if resp.statusCode.intValue >= 300 {
print("Failed.")
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 resp.statusCode.intValue == 202 {
print("Azure-AsyncOperation: \(resp.getHeaderField(fieldName: "Azure-AsyncOperation")!)")
}
if resp.statusCode.intValue == 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
var skuName: String?
var skuTier: String?
var kind: String?
var id: String?
var name: String?
var v_type: String?
var location: String?
var propertiesNetworkAclsBypass: String?
var propertiesNetworkAclsDefaultAction: String?
var propertiesSupportsHttpsTrafficOnly: Bool
var propertiesEncryptionServicesFileEnabled: Bool
var propertiesEncryptionServicesFileLastEnabledTime: String?
var propertiesEncryptionServicesBlobEnabled: Bool
var propertiesEncryptionServicesBlobLastEnabledTime: String?
var propertiesEncryptionKeySource: String?
var propertiesAccessTier: String?
var propertiesProvisioningState: String?
var propertiesCreationTime: String?
var propertiesPrimaryEndpointsDfs: String?
var propertiesPrimaryEndpointsWeb: String?
var propertiesPrimaryEndpointsBlob: String?
var propertiesPrimaryEndpointsQueue: String?
var propertiesPrimaryEndpointsTable: String?
var propertiesPrimaryEndpointsFile: String?
var propertiesPrimaryLocation: String?
var propertiesStatusOfPrimary: String?
var propertiesSecondaryLocation: String?
var propertiesStatusOfSecondary: String?
var i: Int
var count_i: Int
skuName = json.string(of: "sku.name")
skuTier = json.string(of: "sku.tier")
kind = json.string(of: "kind")
id = json.string(of: "id")
name = json.string(of: "name")
v_type = json.string(of: "type")
location = json.string(of: "location")
propertiesNetworkAclsBypass = json.string(of: "properties.networkAcls.bypass")
propertiesNetworkAclsDefaultAction = json.string(of: "properties.networkAcls.defaultAction")
propertiesSupportsHttpsTrafficOnly = json.bool(of: "properties.supportsHttpsTrafficOnly")
propertiesEncryptionServicesFileEnabled = json.bool(of: "properties.encryption.services.file.enabled")
propertiesEncryptionServicesFileLastEnabledTime = json.string(of: "properties.encryption.services.file.lastEnabledTime")
propertiesEncryptionServicesBlobEnabled = json.bool(of: "properties.encryption.services.blob.enabled")
propertiesEncryptionServicesBlobLastEnabledTime = json.string(of: "properties.encryption.services.blob.lastEnabledTime")
propertiesEncryptionKeySource = json.string(of: "properties.encryption.keySource")
propertiesAccessTier = json.string(of: "properties.accessTier")
propertiesProvisioningState = json.string(of: "properties.provisioningState")
propertiesCreationTime = json.string(of: "properties.creationTime")
propertiesPrimaryEndpointsDfs = json.string(of: "properties.primaryEndpoints.dfs")
propertiesPrimaryEndpointsWeb = json.string(of: "properties.primaryEndpoints.web")
propertiesPrimaryEndpointsBlob = json.string(of: "properties.primaryEndpoints.blob")
propertiesPrimaryEndpointsQueue = json.string(of: "properties.primaryEndpoints.queue")
propertiesPrimaryEndpointsTable = json.string(of: "properties.primaryEndpoints.table")
propertiesPrimaryEndpointsFile = json.string(of: "properties.primaryEndpoints.file")
propertiesPrimaryLocation = json.string(of: "properties.primaryLocation")
propertiesStatusOfPrimary = json.string(of: "properties.statusOfPrimary")
propertiesSecondaryLocation = json.string(of: "properties.secondaryLocation")
propertiesStatusOfSecondary = json.string(of: "properties.statusOfSecondary")
}
print("Success.")
}