简体   繁体   English

使用boto3从AWS S3下载特定文件

[英]Download specifc files from AWS S3 using boto3

I am trying to download files from Amazon S3 to my local computer. 我正在尝试将文件从Amazon S3下载到我的本地计算机。

I have about 100,000 jpeg files in my Bucket, like SFADOK_1737372938_26465755_29573736.jpeg . 我的存储桶中大约有100,000个jpeg文件,例如SFADOK_1737372938_26465755_29573736.jpeg

Now I want to download all files with the ending: *_29573736.jpeg 现在,我要下载所有结尾为*_29573736.jpeg

Could anyone please help me? 谁能帮我吗?

One simple way would be to use the AWS Command-Line Interface (CLI) : 一种简单的方法是使用AWS命令行界面(CLI)

aws s3 cp s3://my-bucket/ . --recursive --exclude '*' --include '*_29573736.jpeg'

If you wish to do it via a boto3 program, the program would need to: 如果您希望通过boto3程序进行操作,则该程序需要:

  • Obtain a listing of objects from the bucket using list_objects_v2() 使用list_objects_v2()从存储桶中获取对象列表
  • Loop through each object in the listing that is returned: 循环遍历列表中返回的每个对象:
    • If the Key matches your desired pattern, then... 如果Key与您想要的样式匹配,则...
    • Use download_file() to download the object 使用download_file()下载对象

With so many objects in the bucket, you will either need to loop through list_objects_v2() using the ContinuationToken or use a paginator to automatically loop through the results. 存储桶中有许多对象,您将需要使用ContinuationToken遍历list_objects_v2()或使用分页器自动遍历结果。 This is because a maximum of 1000 objects will be returned per API call. 这是因为每个API调用最多返回1000个对象。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM