【问题标题】:How to copy a file from local machine to a storage account using Terraform如何使用 Terraform 将文件从本地计算机复制到存储帐户
【发布时间】:2020-09-08 22:17:36
【问题描述】:

我已经使用 Terraform 创建了一个存储帐户并在其中创建了一个文件共享。我想使用 Terraform 将一些文件从本地计算机复制到该存储帐户。那可能吗?如果是,那么如何?

这是我的代码-

provider "azurerm" {
  # whilst the `version` attribute is optional, we recommend pinning to a given version of the Provider
  version = "=2.0.0"
  subscription_id = "xxx-xxxx-xxxx-xxxx"
  tenant_id = "xxxxx-xxxxx-xxxx-xxxx"
  features {}
}
resource "azurerm_resource_group" "example" {
  name     = "azuretest"
  location = "West Europe"
}
resource "azurerm_storage_account" "example" {
  name                     = "asdfghjklteststorage"
  resource_group_name      = azurerm_resource_group.example.name
  location                 = azurerm_resource_group.example.location
  account_tier             = "Standard"
  account_replication_type = "LRS"
}


resource "azurerm_storage_share" "example" {
  name                 = "sharename"
  storage_account_name = azurerm_storage_account.example.name
  quota                = 50

  acl {
    id = "xxx-xxx-xxx-xxx"

    access_policy {
      permissions = "rwdl"
      start       = "2020-05-10T09:38:21.0000000Z"
      expiry      = "2020-07-02T10:38:21.0000000Z"
    }
  }
}
resource "null_resource" "uploadfile" {

  provisioner "local-exec" {


  command = <<-EOT
  $storageAcct = Get-AzStorageAccount -ResourceGroupName "${azurerm_resource_group.example.name}" -Name "${azurerm_storage_account.example.name}"
   Set-AzStorageFileContent `
   -Context $storageAcct.Context `
   -ShareName "${azurerm_storage_share.example.name}" `
   -Source "C:\Users\xxx\xxx\xxx\Untitled.png" `
   -Path "Untitled.png"

  EOT

  interpreter = ["PowerShell", "-Command"]
  }
}  

【问题讨论】:

    标签: terraform-provider-azure azure-storage-files terraform0.12+


    【解决方案1】:

    您可以使用local-exec 配置程序在创建资源后调用本地可执行文件。见upload a file to Azure file share with PowerShell。

    例如,

        resource "azurerm_storage_share" "example" {
          name                 = "sharename"
          storage_account_name = azurerm_storage_account.example.name
          quota                = 50
    
          acl {
            id = "xxxxxxxxxxxxxxx"
    
            access_policy {
              permissions = "rwdl"
              start       = "2020-05-10T09:38:21.0000000Z"
              expiry      = "2020-07-02T10:38:21.0000000Z"
            }
          }
        }
    
        resource "null_resource" "uploadfile" {
    
          provisioner "local-exec" {
    
    
          command = <<-EOT
          $storageAcct = Get-AzStorageAccount -ResourceGroupName "${azurerm_resource_group.example.name}" -Name "${azurerm_storage_account.example.name}"
           Set-AzStorageFileContent `
           -Context $storageAcct.Context `
           -ShareName "${azurerm_storage_share.example.name}" `
           -Source "C:\Users\xxx\terraform\test.txt" `
           -Path "test.txt"
    
          EOT
    
          interpreter = ["PowerShell", "-Command"]
          }
    
    }
    

    结果

    更多信息,您可以阅读this blog。

    【讨论】:

    • 你能告诉我 acl 块的 Id 部分会出现什么吗?它指的是哪个id?
    • 它是一个客户端 ID,我用它来部署带有 terraform 的 azure 资源。
    • 我添加了客户端 ID,但出现错误 - 读取 AzureRM 存储帐户“asdfghjklteststorage”的静态网站时出错:accounts.Client#GetServiceProperties:响应请求失败:StatusCode=403 - 原始错误: autorest/azure:服务返回错误。 Status=403 Code="AuthorizationPermissionMismatch" Message="此请求无权使用此权限执行此操作。\nRequestId:7d28c330-c01e-004f-3b31-303003000000\nTime:2020-05-22T12:03:53.5643484Z"
    • 你的服务主体的角色是什么?
    • 你能告诉我需要哪个角色吗?
    【解决方案2】:

    对于来自 Google 的任何人,值得指出的是,在 2022 年初,现在应该可以使用 azurerm_storage_share_file。这对我上传单个文件非常有用。要上传多个文件,您似乎需要多个资源

    https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/storage_share_file

    resource "azurerm_storage_share_file" "example" {
      name             = "test.txt"
      storage_share_id = azurerm_storage_share.example.id
      source           = "${path.root}/TestFiles/test.txt"
    }
    

    【讨论】:

      猜你喜欢
      • 2018-07-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-26
      • 1970-01-01
      • 2021-06-08
      • 2019-12-30
      • 1970-01-01
      相关资源
      最近更新 更多