繁体   English   中英

使用 ARM 模板将文件上传到存储帐户

[英]upload file to storage account using ARM template

我是天蓝色的新手。 我在 azure 门户中创建了一个存储帐户。 我需要使用 ARM 模板将文件上传到存储帐户。 任何人都可以让我知道如何做到这一点吗?

正如 Bruno Faria 提到的,我们不能使用 ARM 模板来做到这一点。 使用 Azure ARM 模板。 您可以在单个协调操作中部署、更新或删除解决方案的所有资源。 有关 ARM 模板的更多详细信息,请参阅文档

使用资源管理器,您可以创建定义 Azure 解决方案的基础结构和配置的模板(JSON 格式)。 通过使用模板,您可以在整个生命周期内重复部署您的解决方案,并确信您的资源以一致的状态部署

我们可以使用Microsoft Azure Storage Explorer轻松做到这一点。

在此处输入图像描述

如果我们尝试使用程序来做到这一点,我们可以从 Azure 官方文档中获取演示代码。

// 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);
}

你可以使用AzCopy:

AzCopy /Source:C:\myfolder /Dest:https://myaccount.blob.core.windows.net/mycontainer /DestKey:key /S

更多信息: https//docs.microsoft.com/en-us/azure/storage/storage-use-azcopy

您还可以使用powershell将文件上载到存储帐户:

Get-ChildItem –Path C:\Images\* | Set-AzureStorageBlobContent -Container "yourcontainername"

更多信息: https//docs.microsoft.com/en-us/azure/storage/storage-powershell-guide-full

当前不支持使用 ARM 模板将文件上传到 Blob 存储。

这背后的逻辑是 ARM 模板用于管理 Azure 资源(执行创建、删除和更改资源属性等操作),而不是管理您对这些资源的使用。

文件上传到 Blob 存储的方式有:

  1. 通过存储资源管理器工具手动-存储资源管理器
  2. 通过 Az Powershell 命令以编程方式- az storage blob upload

也就是说,部署Blob 存储容器(资源本身)可以如以下模板所示完成:(感谢 @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')]"
          ]
        }
      ]
    }
  ]
}

你可以做一个依赖部署脚本

这是@Aaron M. 答案的延伸。 这假设您正在上传图像,但我相信您可以针对其他文件类型进行修改。

{
  "$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"
        }
      ]
    }
  ]
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM