[英]How to modify the batch file to get two last modified files from S3?
我目前有一個 bat 文件,它將最后修改的文件從 AWS S3 存儲桶復制到本地文件夾。
for /f "delims=" %%i in ('aws s3api list-objects-v2 --bucket example.sftp --prefix data/ --query "sort_by(Contents, &LastModified)[-1].Key" --output text') do set object=%%i
aws s3 cp s3://example.sftp/%object% E:\DATA_S3
我想修改它以在按上次修改而不是一個排序時獲得兩個文件。 將[-1]
更改為[-2]
無效。
JMESPath 使用 Python 語法來引用列表,所以使用:
--query "sort_by(Contents, &LastModified)[-2:].Key"
這意味着“從倒數第二個元素到列表末尾”。
我強烈推薦JMESPath 教程,其中包括命令的交互式測試。 我使用那個頁面測試了這個概念。
您可以使用 JMESPath 查詢通過[-2:]
獲取最近的兩個項目,然后依次對每個項目進行操作。 請注意,在路徑中,不要使用.Key
,而是使用.[Key]
,這會導致 AWS CLI 的文本 output 以換行符而不是制表符分隔,從而允許批處理文件的for
運算符輕松解析:
for /f "delims=" %%i in ('aws s3api list-objects-v2 --bucket example-bucket --prefix target/prefix/ --query "sort_by(Contents, &LastModified)[-2:].[Key]" --output text') do (
aws s3 cp s3://example-bucket/%%i folder\to\copy\to
)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.