简体   繁体   English

如何使用 aws cli 获取最新版本的 s3 object?

[英]How do you get the latest version of an s3 object using aws cli?

Using the AWS CLI, how do I output a most recent VersionId string of an S3 object?使用 AWS CLI,我如何 output S3 object 的最新 VersionId 字符串?

简化@rynop 的命令:

aws s3api list-object-versions --bucket us-east-1--aws-blueprint.mydomain.com --prefix my/object/key.ext --query 'Versions[?IsLatest].[VersionId]' --output text

There is another option to get the latest S3 object versionId.还有另一个选项可以获取最新的 S3 对象 versionId。

To get it you have to simply set tag to that object with aws s3api put-object-tagging command.要获得它,您只需使用aws s3api put-object-tagging命令为该对象设置标签。 The output of that operation is only latest object versionId.该操作的输出只是最新的对象 versionId。 By default it is returned as JSON, but with --output text option you will get it as only returned string.默认情况下,它以 JSON 形式返回,但使用--output text选项,您将仅将其作为返回字符串获取。

Sample:样本:

 $ VERSION_ID=$(aws s3api put-object-tagging --bucket bucket-name --key code.zip --tagging 'TagSet=[{Key=Lambda,Value=golang}]' --output text)
 $ echo $VERSION_ID
 6ubT3rNptpcZNGxEA6Cj2kAastuCyNL1

I believe it's best way to achieve it.我相信这是实现它的最佳方式。 The risk with listObjectVersions is that you might have more than pagination limit and then you will have to call it again from "NextToken" value. listObjectVersions的风险在于您可能有超过分页限制,然后您将不得不从“NextToken”值再次调用它。 But with put-object-tagging it is reliable solution fir this usecase.但是使用put-object-tagging是这个用例的可靠解决方案。 Plus you get some more details stored on your object.此外,您还可以在对象上存储更多详细信息。

In my project I keep there details about the team responsible for the lambda, the code runtime for the ZIP and project repository name.在我的项目中,我保留了有关负责 lambda 的团队、ZIP 的代码运行时和项目存储库名称的详细信息。 So one someone spot that ZIP in the bucket it is easy to get such details.因此,有人发现桶中的 ZIP 很容易获得此类详细信息。

aws s3api list-object-versions --bucket aws-blueprint.mybucket.com --prefix nested-stacks/apig/single-lambda-proxy-with-CORS.yaml | jq -r '.Versions[] | select(.IsLatest == true) | .VersionId'

输出看起来像: o65j.nFiJ00D25lNPyzsbJ7rWktOoKmS

I believe an even simpler version of @JohnRotenstein's answer is:我相信@JohnRotenstein 的答案更简单的版本是:

aws s3api head-object --bucket us-east-1--aws-blueprint.mydomain.com --prefix my/object/key.ext --query 'VersionId' --output text

It's not documented, but a HEAD request on an object appears to return the metadata of the latest version.它没有记录,但 object 上的HEAD请求似乎返回最新版本的元数据。

暂无
暂无

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

相关问题 如何获取在 s3 中上传的最新 object 名称并使用 AWS Lambda 将该数据(json)推送到不同的 api(点击 api)? - How do I get the latest object name uploaded in s3 and push that data(json) to a different api (hit an api) using AWS Lambda? 如何使用 AmazonS3EncryptionClientV2 客户端加密从 AWS S3 存储桶下载 object? - How do you download object from AWS S3 bucket using AmazonS3EncryptionClientV2 client side encryption? 如何使用 aws s3 cli 更新目录的元数据? - How do I update the meta-data of a directory using aws s3 cli? 如何将 AWS CLI 升级到最新版本? - How to upgrade AWS CLI to the latest version? 如何使用 CLI 删除 AWS S3 中的版本化存储桶? - How do I delete a versioned bucket in AWS S3 using the CLI? 如何使用 aws-sdk-2.x 从 S3 存储桶中获取 object 的 S3 URL - How to get S3 URL for the object from S3 bucket using aws-sdk-2.x 如何使用 AWS CLI 递归删除大小为 0 的 S3 对象 - How to Recursively Delete S3 Objects of Size 0 using the AWS CLI 如何为 AWS Lambda 或 S3“更改模板中的 object 密钥或版本” - How to "change the object key or version in the template" for AWS Lambda or S3 您如何设置 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?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM