Sharath
Sharath

Reputation: 61

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

Getting below error after running above code. Kindly help on this.. Passing Source and Destination file name as parameter in S3 attributes...

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "C:\Cloudtail\CT\SQL Scripts\python\GRN_S3_dwnld.py", line 17, in except botocore.exception.ClientErrors as e: NameError: name 'botocore' is not defined

Upvotes: 1

Views: 1114

Answers (2)

Sharath
Sharath

Reputation: 61

Figured out an error, its not boto3 error. Filename in Capital letter like 01FEB2020, my source file is taking like 01Feb2020.

Change above date with upper() function

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

Thanks all to your valuable time and responses.

Upvotes: 1

John Rotenstein
John Rotenstein

Reputation: 269101

Add this to the top of your Python program:

from botocore.exceptions import ClientError

Change

except botocore.exception.ClientErrors as e:

into:

except botocore.exception.ClientError as e:

Upvotes: 0

Related Questions