[英]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.