paw*_*n19 12 file-upload amazon-s3 terraform-provider-aws
我想将多个文件从本地设备中的特定文件夹上传到 AWS S3。我遇到了以下错误。

这是我的 terraform 代码。
resource "aws_s3_bucket" "testbucket" {
bucket = "test-terraform-pawan-1"
acl = "private"
tags = {
Name = "test-terraform"
Environment = "test"
}
}
resource "aws_s3_bucket_object" "uploadfile" {
bucket = "test-terraform-pawan-1"
key = "index.html"
source = "/home/pawan/Documents/Projects/"
}
Run Code Online (Sandbox Code Playgroud)
我怎么解决这个问题?
meu*_*rus 33
从 Terraform 0.12.8 开始,您可以使用该fileset函数获取给定路径和模式的文件列表。结合for_each,您应该能够将每个文件作为自己的文件上传aws_s3_bucket_object:
resource "aws_s3_bucket_object" "dist" {
for_each = fileset("/home/pawan/Documents/Projects/", "*")
bucket = "test-terraform-pawan-1"
key = each.value
source = "/home/pawan/Documents/Projects/${each.value}"
# etag makes the file update when it changes; see /sf/ask/3927508091/
etag = filemd5("/home/pawan/Documents/Projects/${each.value}")
}
Run Code Online (Sandbox Code Playgroud)
请参阅terraform-providers/terraform-provider-aws :aws_s3_bucket_object:在 GitHub上支持目录上传 #3020。
注意:这不会设置像 那样的元数据content_type,据我所知,Terraform 没有内置方法来推断文件的内容类型。此元数据对于诸如从浏览器正常工作的 HTTP 访问之类的事情很重要。如果这对您很重要,您应该考虑手动指定每个文件,而不是尝试自动从文件夹中抓取所有内容。
Fla*_*air 18
自 2020 年 6 月 9 日起,terraform 提供了一种内置方法来推断您在上传到 S3 存储桶时可能需要的文件的内容类型(以及一些其他属性)
HCL 格式:
module "template_files" {
source = "hashicorp/dir/template"
base_dir = "${path.module}/src"
template_vars = {
# Pass in any values that you wish to use in your templates.
vpc_id = "vpc-abc123"
}
}
resource "aws_s3_bucket_object" "static_files" {
for_each = module.template_files.files
bucket = "example"
key = each.key
content_type = each.value.content_type
# The template_files module guarantees that only one of these two attributes
# will be set for each file, depending on whether it is an in-memory template
# rendering result or a static file on disk.
source = each.value.source_path
content = each.value.content
# Unless the bucket has encryption enabled, the ETag of each object is an
# MD5 hash of that object.
etag = each.value.digests.md5
}
Run Code Online (Sandbox Code Playgroud)
JSON 格式:
{
"resource": {
"aws_s3_bucket_object": {
"static_files": {
"for_each": "${module.template_files.files}"
#...
}}}}
#...
}
Run Code Online (Sandbox Code Playgroud)
来源: https: //registry.terraform.io/modules/hashicorp/dir/template/latest
Vik*_*yol 14
您正在尝试上传目录,而 Terraform 需要源字段中的单个文件。尚不支持将文件夹上传到 S3 存储桶。
但是,您可以按照此处的建议使用 null_resource 配置程序调用 awscli 命令。
resource "null_resource" "remove_and_upload_to_s3" {
provisioner "local-exec" {
command = "aws s3 sync ${path.module}/s3Contents s3://${aws_s3_bucket.site.id}"
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8928 次 |
| 最近记录: |