如何使用terraform在亚马逊S3桶中创建文件夹

时间:2021-10-05 10:41:40

I was able to create a bucket in an amazon S3 using this link.

我可以使用此链接在亚马逊S3中创建一个桶。

I used the following code to create a bucket :

我使用以下代码创建一个存储桶:

resource "aws_s3_bucket" "b" {
    bucket = "my_tf_test_bucket"
    acl = "private"
}

Now I wanted to create folders inside the bucket, say Folder1.

现在我想在存储桶中创建文件夹,比如说Folder1。

I found the link for creating an S3 object. But this has a mandatory parameter source. I am not sure what this value have to , since my intent is to create a folder inside the S3 bucket.

我找到了创建S3对象的链接。但这有一个强制参数源。我不确定这个值是什么,因为我的意图是在S3桶中创建一个文件夹。

3 个解决方案

#1


13  

S3 doesn't support folders. Objects can have prefix names with slashes that look like folders, but that's just part of the object name. So there's no way to create a folder in terraform or anything else, because there's no such thing as a folder in S3.

S3不支持文件夹。对象的前缀名称可以带有看似文件夹的斜杠,但这只是对象名称的一部分。所以没有办法在terraform或其他任何东西中创建一个文件夹,因为在S3中没有文件夹这样的东西。

http://docs.aws.amazon.com/AmazonS3/latest/dev/UsingMetadata.html http://docs.aws.amazon.com/AWSImportExport/latest/DG/ManipulatingS3KeyNames.html

http://docs.aws.amazon.com/AmazonS3/latest/dev/UsingMetadata.html http://docs.aws.amazon.com/AWSImportExport/latest/DG/ManipulatingS3KeyNames.html

If you want to pretend, you could create a zero-byte object in the bucket named "Folder1/" but that's not required. You can just create objects with key names like "Folder1/File1" and it will work.

如果你想假装,你可以在名为“Folder1 /”的存储桶中创建一个零字节对象,但这不是必需的。你可以使用像“Folder1 / File1”这样的键名创建对象,它可以工作。

#2


20  

For running terraform on Mac or Linux, the following will do what you want

要在Mac或Linux上运行terraform,以下将执行您想要的操作

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

If you're on windows you can use an empty file.

如果您在Windows上,则可以使用空文件。

While folks will be pedantic about s3 not having folders, there are a number of operations where having an object placeholder for a key prefix (otherwise called a folder) make life easier. Like s3 sync for example.

虽然人们会对s3没有文件夹感到迂腐,但是有很多操作都有一个对象占位符作为键前缀(也称为文件夹),这会使生活更轻松。比如s3 sync。

#3


2  

old answer but if you specify the key with the folder (that doesn't exist yet) terraform will create the folder automatically for you

旧答案,但如果您指定了文件夹的密钥(尚不存在),terraform将自动为您创建文件夹

terraform {
  backend "s3" {
    bucket = "mysql-staging"
    key    = "rds-mysql-state/terraform.tfstate"
    region = "us-west-2"
    encrypt = true
  }
}

#1


13  

S3 doesn't support folders. Objects can have prefix names with slashes that look like folders, but that's just part of the object name. So there's no way to create a folder in terraform or anything else, because there's no such thing as a folder in S3.

S3不支持文件夹。对象的前缀名称可以带有看似文件夹的斜杠,但这只是对象名称的一部分。所以没有办法在terraform或其他任何东西中创建一个文件夹,因为在S3中没有文件夹这样的东西。

http://docs.aws.amazon.com/AmazonS3/latest/dev/UsingMetadata.html http://docs.aws.amazon.com/AWSImportExport/latest/DG/ManipulatingS3KeyNames.html

http://docs.aws.amazon.com/AmazonS3/latest/dev/UsingMetadata.html http://docs.aws.amazon.com/AWSImportExport/latest/DG/ManipulatingS3KeyNames.html

If you want to pretend, you could create a zero-byte object in the bucket named "Folder1/" but that's not required. You can just create objects with key names like "Folder1/File1" and it will work.

如果你想假装,你可以在名为“Folder1 /”的存储桶中创建一个零字节对象,但这不是必需的。你可以使用像“Folder1 / File1”这样的键名创建对象,它可以工作。

#2


20  

For running terraform on Mac or Linux, the following will do what you want

要在Mac或Linux上运行terraform,以下将执行您想要的操作

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

If you're on windows you can use an empty file.

如果您在Windows上,则可以使用空文件。

While folks will be pedantic about s3 not having folders, there are a number of operations where having an object placeholder for a key prefix (otherwise called a folder) make life easier. Like s3 sync for example.

虽然人们会对s3没有文件夹感到迂腐,但是有很多操作都有一个对象占位符作为键前缀(也称为文件夹),这会使生活更轻松。比如s3 sync。

#3


2  

old answer but if you specify the key with the folder (that doesn't exist yet) terraform will create the folder automatically for you

旧答案,但如果您指定了文件夹的密钥(尚不存在),terraform将自动为您创建文件夹

terraform {
  backend "s3" {
    bucket = "mysql-staging"
    key    = "rds-mysql-state/terraform.tfstate"
    region = "us-west-2"
    encrypt = true
  }
}