如何在执行CloudFormation打包时动态创建S3中的文件夹。

huangapple go评论67阅读模式
英文:

How to dynamically create a folder in s3 when doing cloudformation package

问题

我正在创建一个CI/CD流水线,其中我使用CloudFormation Package来打包部署。现在我想让CloudFormation将构件上传到一个动态创建的文件夹,带有当前的日期时间戳。有没有办法可以做到这一点?我的解决方案如下,但不起作用。

build.sh

其他命令
timestamp=$(date +%s)
aws cloudformation package --template-file template.yaml --output-template-file packaged-template.yaml --s3-bucket bucket name --s3-prefix cfn-deployment/$timestamp

现在我想在deploy.sh shell脚本中使用这个timestamp变量,其中我正在使用CloudFormation Deploy命令。

英文:

I am creating a CI/CD pipeline where I am using cloudformation package to package the deployment . Now I want cloudformation to upload the artifacts inside a dynamically created folder with current datetimestamp. Is there any way I can do that? My solution is given below which doesn't work.

build.sh

other commands
timestamp=$(date +%s)
aws cloudformation package --template-file template.yaml --output-template-file packaged-template.yaml --s3-bucket bucket name --s3-prefix cfn-deployment/$timestamp

Now I want use this timestamp variable inside deploy.sh shell script where cloudformation deploy command I am using

答案1

得分: 1

在Amazon S3中没有“文件夹”或目录的概念,它是一个对象存储。实际上,“路径”只是对象名称。这些路径在名称中有正斜杠,并且Amazon S3控制台会以特殊方式处理它们,以使其看起来像您有目录。但实际上,您并没有。

您正在尝试的操作是没有意义的,也不可能实现。每当您需要将工件上传到S3时,只需指定路径,它就会正常工作,无需通过CloudFormation执行任何操作。

示例

创建一个空的存储桶

aws s3api create-bucket --bucket $MY_BUCKET --region us-east-1

上传包含今天日期的文件:

echo 'hello' > myfile
aws s3 cp myfile s3://$MY_BUCKET/$(date -u +"%Y-%m-%d")/myfile
upload: ./myfile to s3://REDACTED/2023-01-09/myfile

通过s3 ls列出对象(使其看起来像您的Linux列表):

aws s3 ls s3://$MY_BUCKET
                           PRE 2023-01-09/

通过API列出对象(s3api list-objects-v2):

aws s3api list-objects-v2 --bucket $MY_BUCKET
{
    "Contents": [
        {
            "Key": "2023-01-09/myfile",
            "LastModified": "2023-01-09T10:43:20.000Z",
            "ETag": "\"b1946ac92492d2347c6235b4d2611184\"",
            "Size": 6,
            "StorageClass": "STANDARD"
        }
    ]
}
英文:

There is no concept of "folders" or directories in Amazon S3. It's an Object Storage. The "paths" are effectively just the object name. The paths have forward slashes in their name and the Amazon S3 Console will treat that in a special way to make it look like you have directories. In reality, you don't.

What you're trying to do does not make sense and it's not possible. Whenever you need to upload your artifacts to S3, just specify the path and it will work out, no need to do anything through CloudFormation.

Example

Creating an empty bucket

aws s3api create-bucket --bucket $MY_BUCKET --region us-east-1

Uploading a file with key containing today's date:

echo 'hello' > myfile
aws s3 cp myfile s3://$MY_BUCKET/$(date -u +"%Y-%m-%d")/myfile
upload: ./myfile to s3://REDACTED/2023-01-09/myfile

Listing the objects through s3 ls (making it look like your linux list):

 aws s3 ls s3://$MY_BUCKET
                           PRE 2023-01-09/

Listing the objects through the API (s3api list-objects-v2):

aws s3api list-objects-v2 --bucket $MY_BUCKET
{
    "Contents": [
        {
            "Key": "2023-01-09/myfile",
            "LastModified": "2023-01-09T10:43:20.000Z",
            "ETag": "\"b1946ac92492d2347c6235b4d2611184\"",
            "Size": 6,
            "StorageClass": "STANDARD"
        }
    ]
}

huangapple
  • 本文由 发表于 2023年1月9日 16:53:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/75054947.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定