繁体   English   中英

使用 Python 从 S3 下载 CSV 文件

[英]Download CSV files from S3 using Python

import os
import boto3
import json
from datetime import datetime,timedelta

s3 = boto3.client('s3', aws_access_key_id="<access_key>",
                    aws_secret_access_key="<secret_key>")
##my_bucket = s3.Bucket('grn-amazon')
filename_withdate=(datetime.now()-timedelta(days=6)).strftime ("%d%b%Y")
filename_withdate = filename_withdate+'_Consolidated.csv'
Source_filename = filename_withdate
dest_filename = filename_withdate
try:
##    s3.download_file('grn-amazon',complt_filename,'01FEB2020_Consolidated.csv')
    s3.download_file('grn-amazon',Source_filename,dest_filename)
    print("Download Completed")
except botocore.exception.ClientErrors as e:
    if e.response['Error']['Code'] == '404':
        print('The Object does not exists!!')
    else:
        raise

运行以上代码后出现以下错误。 请对此提供帮助.. 将源和目标文件名作为 S3 属性中的参数传递...

在处理上述异常的过程中,又发生了一个异常:

回溯(最近一次调用):文件“C:\\Cloudtail\\CT\\SQL Scripts\\python\\GRN_S3_dwnld.py”,第 17 行,除了 botocore.exception.ClientErrors 作为 e: NameError: name 'botocore' is not defined

找出一个错误,它不是 boto3 错误。 大写字母的文件名,如 01FEB2020,我的源文件是 01Feb2020。

使用upper()函数更改上面的日期

filename_withdate=((datetime.now()-timedelta(days=6)).strftime ("%d%b%Y").upper())

感谢大家的宝贵时间和回复。

将此添加到 Python 程序的顶部:

from botocore.exceptions import ClientError

改变

except botocore.exception.ClientErrors as e:

进入:

except botocore.exception.ClientError as e:

暂无
暂无

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

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