amazon-s3 - 如何从包含大量文件的 S3 存储桶下载最新文件

标签 amazon-s3 boto aws-cli

我的 S3 存储桶中有很多文件,我希望下载最近的 1000 个文件(按日期上传)。

如何继续使用 AWS cli 或 s3 boto 执行此操作

最佳答案

您可以使用以下命令:

aws s3api list-objects --bucket <bucket> \
--query 'reverse(sort_by(Contents[].{Key: Key, LastModified: LastModified}, &LastModified))[:1000].[Key]' --output text | \ 
xargs -I {} aws s3 cp s3://<bucket>/{} .

我使用以下 JMESPath 函数:

  • sort_by :对 json 数组进行排序,在本例中,我将内容过滤为仅 (Key, LastModified) 参数,并将过滤 LastModified 属性

  • reverse :需要反转结果,因为您想要最新的结果

  • [:x] :仅接受数组的 x 个参数,在您的情况下您需要 1000 个项目

  • 仅读取 .[Key] 的关键元素放置为数组,以便我们可以将每个元素作为新行输出为文本

  • xargs -I {} aws s3 cp s3://<bucket>/{} .将从您的 s3 存储桶帐户中找到的每个文件复制到本地

关于amazon-s3 - 如何从包含大量文件的 S3 存储桶下载最新文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44040176/

相关文章:

javascript - 将图像上传到 S3 并调整其大小

amazon-web-services - 从远程 Solaris 服务器到 AWS S3 存储桶的 FTP 文件

python - (python/boto sqs) UnicodeDecodeError : 'ascii' codec can't decode byte 0xc3 in position 5: ordinal not in range(128)

bash - 如何临时切换 AWS CLI 的配置文件?

amazon-web-services - 解析参数 '--parameters' : Expected: '=' , 时出错: 'P'

amazon-web-services - 错误: The provided credentials do not have permission to create the service-linked role for EC2 Spot Instances

amazon-web-services - AWS s3 - 触发对象创建,函数被连续调用

node.js - 无服务器部署 : Recoverable error occurred (write ECONNRESET), 休眠约 5 秒。尝试 4 个中的 1 个

python - 如何将用户添加到资格

使用 boto 的 django-storages - 无法上传 mp3,但可以上传图像。另外,还承受着 HTTP 307 的痛苦