简体   繁体   English

如何使用 aws cdk 创建 s3 子目录?

[英]how do you create s3 subdirectories using aws cdk?

if I wanted to create a bucket with a layout like this:如果我想创建一个布局如下的桶:

bucket/
├─ subdir-1/
│  ├─ subsubdir-1/
├─ subdir-2/
├─ subdir-3/

how could I do this using the cdk?我怎么能用 cdk 做到这一点? Or even a yml cf template?甚至是 yml cf 模板?

As mentioned in the comment, you do not need to create directories/template.如评论中所述,您不需要创建目录/模板。 When you use SDK/CLI to upload an object, it will automatically create the folder/structure matching the key used when uploading the object. You may be able to create empty directories through CLI/SDK but I don't think aws-cdk would do that as it is not supposed to managed the content of S3.当您使用 SDK/CLI 上传 object 时,它会自动创建与上传 object 时使用的密钥匹配的文件夹/结构。您可以通过 CLI/SDK 创建空目录,但我认为 aws-cdk 不会这样做是因为它不应该管理 S3 的内容。

If you really want to maintain a certain layout for the bucket, you could use bucket policy to only allow certain directory when the SDK/CLI put your object to S3.如果您真的想为存储桶维护特定布局,您可以使用存储桶策略在 SDK/CLI 将您的 object 放入 S3 时仅允许特定目录。 This policy could be added on the aws-cdk (Ref: aws-cdk-s3-docs-addPolicy ).可以在 aws-cdk 上添加此策略(参考: aws-cdk-s3-docs-addPolicy )。 Policy example as follows.策略示例如下。

{
  "Sid": "Layout",
  "Effect": "Deny",
  "Principal": "*",
  "Action": "s3:PutObject*",
  "NotResource": [
    "arn:aws:s3:::{BUCKET_NAME}/subdir-1/*",
    "arn:aws:s3:::{BUCKET_NAME}/subdir-1/subdir-1/*",
    "arn:aws:s3:::{BUCKET_NAME}/subdir-2/*",
    "arn:aws:s3:::{BUCKET_NAME}/subdir-3/*",
  ]
}

More detail on s3 bucket policy: https://docs.aws.amazon.com/AmazonS3/latest/userguide/example-bucket-policies.html有关 s3 存储桶策略的更多详细信息: https://docs.aws.amazon.com/AmazonS3/latest/userguide/example-bucket-policies.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法在 CDK 应用程序中为 AWS S3 Glacier 创建 CDK 堆栈? - Cannot create CDK stack for AWS S3 Glacier in CDK app? 使用带有源代码的现有 S3 存储桶通过 AWS CDK 进行部署 - Using existing S3 bucket with source code to deploy with AWS CDK 您如何设置 AWS Cloudfront 以使用通配符提供对带有签名 cookies 的 S3 存储桶的自定义访问 - How do you setup AWS Cloudfront to provide custom access to S3 bucket with signed cookies using wildcards 如何使用 AWS SDK V2 为 Amazon S3 配置终端节点? - How do you configure the endpoint for Amazon S3 by using the AWS SDK V2? 如何使用 aws cli 获取最新版本的 s3 object? - How do you get the latest version of an s3 object using aws cli? 如何使用 AmazonS3EncryptionClientV2 客户端加密从 AWS S3 存储桶下载 object? - How do you download object from AWS S3 bucket using AmazonS3EncryptionClientV2 client side encryption? AWS CDK - 如何有条件地创建 ECR 存储库 - AWS CDK - How do I create ECR repo conditionally 如何使用 cdk 创建其他类型的 AWS 机密 - How to create an AWS secret of type other using cdk AWS CDK:如何在没有代码的情况下创建 lambda - AWS CDK: How to create lambda without code 使 Quicksight 资源依赖于 AWS CDK 中的 s3 存储桶创建 - Make Quicksight resource depend on an s3 bucket creation in AWS CDK
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM