无法使用 terraform 将包含子文件夹和文件的本地文件夹上传到 azure 云中的 blob 存储

问题描述 投票:0回答:1

我一直在尝试创建一个 terraform 文件,该文件将创建资源组、存储帐户和 blob 存储容器,然后将包含子文件夹和文件的本地文件夹上传到给定的 blob 存储容器,但我没有成功这样做。 我将添加 main.tf 代码以及正在显示的错误代码。 请帮帮我。

//this informs the terraform to use azurerm provider
terraform {
  required_providers {
    azurerm = {
      source = "hashicorp/azurerm"
      version = "3.78.0"
    }
  }
}
// configuration option needs credential for the authentication
#which will execute the command
provider "azurerm" {
   subscription_id = "sub_id"
   client_id = "client_id"
   client_secret = "TxK8Q~uFOk03shH0tnzKfPBcqzaxEuhKT0hYuaRN"
   tenant_id = "79edd2b6-7c69-4515-be18-b5e923054c77"
  features {}
}
//resource block to create a resource group
resource "azurerm_resource_group" "app_terraform" {
  location = "North Europe"
  name     = "app-terraform"
}

resource "azurerm_storage_account" "storage_account" {
  name                     = "terraformdemotf"
  resource_group_name      = azurerm_resource_group.app_terraform.name
  location                 = azurerm_resource_group.app_terraform.location
  account_tier             = "Standard"
  account_replication_type = "LRS"

  tags = {
    environment = "staging"
  }
}
resource "azurerm_storage_container" "data1" {
  name                 = "data"
  storage_account_name = azurerm_storage_account.storage_account.name
  container_access_type = "blob"
}
resource "null_resource" "m06sparkbasics" {
  triggers = {
    always_run = timestamp()
  }

  provisioner "local-exec" {
    command = "azcopy copy '${var.local_folder_path}' 'https://${azurerm_storage_account.storage_account.name}.blob.core.windows.net/${azurerm_storage_container.data1.name}/${var.blob_prefix}?sv=2022-11-02&ss=bfqt&srt=sco&sp=rwdlacupiytfx&se=2023-11-01T16:29:56Z&st=2023-11-01T08:29:56Z&spr=https&sig=Ysu7bIPcLVhf4tqUf%2BQSibAXDKTG%2B4zqrW8takl8sLA%3D' --recursive=true --from-to=LocalBlob"
  }
}

产生的错误是 -> [first half of the error][1]

terraform azure-cli azure-storage-account azcopy
1个回答
0
投票

我尝试使用 terraform 将包含子文件夹和文件的本地文件夹上传到 azure 云中的 blob 存储,并且能够成功配置要求。

您提供的 Terraform 代码创建一个 Azure 资源组、一个 Azure 存储帐户和一个 Blob 存储容器,然后尝试使用

null_resource
以及调用
local-exec
azcopy
 配置程序将文件上传到 Blob 容器
.

但是,在满足要求时需要小心,

local-exec
配置程序本身并不提供来自
azcopy
的详细错误输出。您需要确保在脚本中正确管理错误处理。

我的地形配置:

terraform {
  required_providers {
    azurerm = {
      source  = "hashicorp/azurerm"
      version = "3.78.0"
    }
  }
}

provider "azurerm" {
  features {}
  
}

variable "blob_prefix" {
  description = "The prefix for the blob storage"
  default     = "demovk" # Set a default or make sure to provide this value when running Terraform
}

resource "azurerm_resource_group" "app_terraform" {
  name     = "appvk-terraform"
  location = "North Europe"
}

resource "azurerm_storage_account" "storage_account" {
  name                     = "terraformdemotfvkstest"
  resource_group_name      = azurerm_resource_group.app_terraform.name
  location                 = azurerm_resource_group.app_terraform.location
  account_tier             = "Standard"
  account_replication_type = "LRS"
  tags = {
    environment = "staging"
  }
}

resource "azurerm_storage_container" "data1" {
  name                  = "data"
  storage_account_name  = azurerm_storage_account.storage_account.name
  container_access_type = "blob"
}

resource "null_resource" "generate_sas" {
  depends_on = [azurerm_storage_container.data1]

  provisioner "local-exec" {
    command = "az storage container generate-sas --name ${azurerm_storage_container.data1.name} --account-name ${azurerm_storage_account.storage_account.name} --permissions rw --expiry `date -u -d '30 minutes' +%Y-%m-%dT%H:%MZ` --output tsv > ${path.module}/sas_token.txt"
    environment = {
      AZURE_STORAGE_ACCOUNT_KEY = azurerm_storage_account.storage_account.primary_access_key
    }
  }
}

resource "null_resource" "upload_files" {
  depends_on = [null_resource.generate_sas]

  provisioner "local-exec" {
    when    = create
    command = <<EOT
    SAS_TOKEN=$(cat ${path.module}/sas_token.txt)
    azcopy copy "/home/bolli/vinay/sample/November" "https://${azurerm_storage_account.storage_account.name}.blob.core.windows.net/${azurerm_storage_container.data1.name}/${var.blob_prefix}?$SAS_TOKEN" --recursive=true
    EOT
  }
}

输出:

enter image description here

enter image description here

enter image description here

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.