如何在现有AWS存储桶中创建多个文件夹

时间:2018-04-27 16:15:03

标签: amazon-web-services amazon-s3 terraform

如何使用terraform在现有存储桶中创建多个文件夹。 例如:bucket / folder1 / folder2

resource "aws_s3_bucket_object" "folder1" {
    bucket = "${aws_s3_bucket.b.id}"
    acl    = "private"
    key    = "Folder1/"
    source = "/dev/null"
}

3 个答案:

答案 0 :(得分:3)

虽然Nate的答案是正确的,但这会导致大量的代码重复。在我看来,更好的解决方案是使用列表并循环遍历它。

创建一个包含可能文件夹列表的变量(variable.tf文件):

variable "s3_folders" {
  type        = "list"
  description = "The list of S3 folders to create"
  default     = ["folder1", "folder2", "folder3"]
}

然后改变你已有的代码:

resource "aws_s3_bucket_object" "folders" {
    count   = "${length(var.s3_folders)}"
    bucket = "${aws_s3_bucket.b.id}"
    acl    = "private"
    key    = "${var.s3_folders[count.index]}/"
    source = "/dev/null"
}

答案 1 :(得分:1)

应用与创建第一个目录相同的逻辑。

resource "aws_s3_bucket_object" "folder1" {
    bucket = "${aws_s3_bucket.b.id}"
    acl    = "private"
    key    = "Folder1/Folder2/"
    source = "/dev/null"
}

答案 2 :(得分:0)

没有针对Windows用户的提示,但这应该对您有用。

比使用空文件作为“源”要容易

resource "aws_s3_bucket_object" "output_subdir" {
  bucket = "${aws_s3_bucket.file_bucket.id}"
  key    = "output/"
  content_type = "application/x-directory"
}

resource "aws_s3_bucket_object" "input_subdir" {
  bucket = "${aws_s3_bucket.file_bucket.id}"
  key    = "input/"
  content_type = "application/x-directory"
}