AWS-使用python在s3存储桶中的多个文件夹中下载文件

时间:2020-02-06 23:52:34

标签: python amazon-s3 boto3

import boto3
import os

S3_Object = boto3.client('s3', region_name='us-west-1' aws_access_key_id='access_key', aws_secret_access_key='secret_access_key')

s3 = boto3.resource('s3')
bucket = 'bucket_name'
#response = s3.Bucket(bucket).objects.all()
# If you want to search only specific path of bucket,
response = s3.Bucket(bucket).objects.filter(Prefix='folder_name')

path = '/tmp/logs'
if not os.path.exists(path):
    os.makedirs(path)

for item in response:
    filename = item.key.rsplit('/', 1)[-1]
    if filename.endswith('.csv.gz'):
        s3.Object(bucket, item.key).download_file(path + filename)
        print("success")

我遇到以下错误 raise NoCredentialsError botocore.exceptions.NoCredentialsError: Unable to locate credentials

所以我已经更新了.aws/config.aws/credentials

.aws/config上:

[profile 1]
region=us-west-1

.aws/credentials上:

aws_access_key_id='access_key'
aws_secret_access_key='secret_access_key'

没有运气。任何帮助都非常感谢。

0 个答案:

没有答案