简体   繁体   English

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

[英]Download specific files from AWS S3 bucket using boto3

I am trying to download to a local machine specific files from an S3 bucket. 我正在尝试从S3存储桶下载特定于本地计算机的文件。 The Bucket structure is as follow: Bucket结构如下:

BucketName/TT/2019/07/23/files.pdf

I want to download all files under: 我想在以下位置下载所有文件:

BucketName/TT/2019/07/23

How can this be done? 如何才能做到这一点?

Please try this: 请尝试以下方法:

import boto3
s3 = boto3.resource('s3')
bucket = s3.Bucket('BucketName')

for obj in bucket.objects.filter(Prefix='TT/2019/07/23/'):
    filename = obj.key.split("/").pop()
    if filename != "":
        print('Downloading ', obj.key)
        bucket.download_file(obj.key, filename)

Note that you will need to configure aws first by setting up authentication credentials. 请注意,您首先需要通过设置身份验证凭据来配置AWS。 Please refer to the quick start guide to see how to do that. 请参考快速入门指南以了解操作方法。

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

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