upload file to storage account using ARM template

Viewed 5832

I am new to azure. I have a storage account created in the azure portal. I need to upload files to the storage account using ARM templates. Can any one please let me know how to do this?

3 Answers

Uploading a file to Blob Storage using an ARM Template is currently not supported.

The logic behind this is that ARM templates are there to manage the Azure Resources (to do actions such as creation, deletion and changing resource properties) and not to manage your usage of these resources.

The ways files can be uploaded to Blob Storage are:

  1. Manually via the Storage Explorer Tool - Storage Explorer
  2. Programatically via the Az Powershell command - az storage blob upload

That said, deploying a Blob Storage container (the Resource itself) can be done as seen in the below template: (credit to @johndownshere)

{
  "$schema": "http://schema.management.azure.com/schemas/2014-04-01-preview/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "storageAccountName": {
      "type": "string"
    }
  },
  "variables": {
    "containerName": "logs"
  },
  "resources": [
    {
      "name": "[parameters('storageAccountName')]",
      "type": "Microsoft.Storage/storageAccounts",
      "apiVersion": "2018-02-01",
      "location": "[resourceGroup().location]",
      "kind": "StorageV2",
      "sku": {
        "name": "Standard_LRS",
        "tier": "Standard"
      },
      "properties": {
        "accessTier": "Hot"
      },
      "resources": [
        {
          "name": "[concat('default/', variables('containerName'))]",
          "type": "blobServices/containers",
          "apiVersion": "2018-03-01-preview",
          "dependsOn": [
            "[parameters('storageAccountName')]"
          ]
        }
      ]
    }
  ]
}

You could do a dependent deployment script

Here's an extension of @Aaron M.'s answer. This assumes you're uploading an image, but I'm sure you could modify for other file types.

{
  "$schema": "http://schema.management.azure.com/schemas/2014-04-01-preview/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "storageAccountName": {
      "type": "string"
    }
  },
  "variables": {
    "containerName": "logs"
  },
  "resources": [
    {
      "name": "[parameters('storageAccountName')]",
      "type": "Microsoft.Storage/storageAccounts",
      "apiVersion": "2018-02-01",
      "location": "[resourceGroup().location]",
      "kind": "StorageV2",
      "sku": {
        "name": "Standard_LRS",
        "tier": "Standard"
      },
      "properties": {
        "accessTier": "Hot"
      },
      "resources": [
        {
          "name": "[concat('default/', variables('containerName'))]",
          "type": "blobServices/containers",
          "apiVersion": "2018-03-01-preview",
          "dependsOn": [
            "[parameters('storageAccountName')]"
          ]
        },{
          "type": "Microsoft.Resources/deploymentScripts",
          "apiVersion": "2020-10-01",
          "name": "runPowerShellInlineWithOutput",
          "location": "[resourceGroup().location]",
          "kind": "AzurePowerShell",
          "dependsOn": [
            "[concat('default/', variables('containerName'))]"
          ],
          "scriptContent": "
            $b64      = 'AAAAAA...' // Your image in base64
            $filename = 'C:\path\to\file'

            $bytes = [Convert]::FromBase64String($b64)
            [IO.File]::WriteAllBytes($filename, $bytes)
            $storageAccount = Get-AzStorageAccount -ResourceGroupName \"[resourceGroup().name]\" -Name \"[parameters('storageAccountName')]\"
            $ctx = $storageAccount.Context 
            $Blob1HT = @{
               File             = $filename
               Container        = [concat('default/', variables('containerName'))]
               Blob             = \"ImageName.jpg\"
               Context          = $ctx
               StandardBlobTier = 'Hot'
            }
            Set-AzStorageBlobContent @Blob1HT
          ",
          "arguments": "[concat('-name', ' ', parameters('name'))]",
          "timeout": "PT1H",
          "cleanupPreference": "OnSuccess",
          "retentionInterval": "P1D"
        }
      ]
    }
  ]
}
Related