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?
2 Answers
As Bruno Faria mentioned that we could not use ARM template to do that. With Azure ARM template. You can deploy, update, or delete all the resources for your solution in a single,coordinated operation. More detail about ARM template please refer to the document
With Resource Manager, you can create a template (in JSON format) that defines the infrastructure and configuration of your Azure solution. By using a template, you can repeatedly deploy your solution throughout its lifecycle and have confidence your resources are deployed in a consistent state
We could use Microsoft Azure Storage Explorer to do that easily.
If we try to use program to do that we could get the demo code from Azure official document.
// Retrieve storage account from connection string.
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
CloudConfigurationManager.GetSetting("StorageConnectionString"));
// Create the blob client.
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
// Retrieve reference to a previously created container.
CloudBlobContainer container = blobClient.GetContainerReference("mycontainer");
// Retrieve reference to a blob named "myblob".
CloudBlockBlob blockBlob = container.GetBlockBlobReference("myblob");
// Create or overwrite the "myblob" blob with contents from a local file.
using (var fileStream = System.IO.File.OpenRead(@"path\myfile"))
{
blockBlob.UploadFromStream(fileStream);
}
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:
- Manually via the Storage Explorer Tool - Storage Explorer
- 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')]"
]
}
]
}
]
}