Sarah
Sarah

Reputation: 617

File is not found when I try to upload the files to S3 using boto3

I'm following a simple tutorial on YouTube about how to automatically upload files in S3 using Python, and I'm getting this error shows that:

FileNotFoundError: [WinError 2] The system cannot find the file specified: 'age.csv'

And this does not make sense to me, because files are there. For example, this my code looks like:

client = boto3.client('s3',
                      aws_access_key_id=access_key,
                      aws_secret_access_key=secret_access_key)

path = 'C:/Users/User/Desktop/python/projects/AWS-Data-Processing/example_data'

for file in os.listdir(path):
    upload_file_bucket = 'my-uploaded-data'
    print(file)
    if '.txt' in file:
        upload_file_key_txt = 'txt/' + str(file)
        client.upload_file(file, upload_file_bucket, upload_file_key_txt)
        print("txt")
    elif '.csv' in file:
        upload_file_key_csv = 'csv/' + str(file)
        client.upload_file(file, upload_file_bucket, upload_file_key_csv)
        print("csv")

And when I comment out the part where it says:

client.upload_file(file, upload_file_bucket, upload_file_key_txt)

it prints out either "txt" or "cvs", and I comment out to just read files such as:

for file in os.listdir(path):
    upload_file_bucket = 'my-uploaded-data'
    print(file)

Then it successfully prints out the file names. So I don't understand why I get the error of there is no file existing when there is. It sounds contradicting and I need some help to understand this error. I read a post where I might need to download AWS CLI, so which I did but it didn't help. I'm guessing the problem lies in the function upload_file but I just don't understand how there is no file? Any advice will be appreciated!

Upvotes: 1

Views: 2963

Answers (1)

Dolev Pearl
Dolev Pearl

Reputation: 284

The upload_file function takes a full file path, and not just a name. It cannot guess what is your directory, so you need to prepend it or use a different way of iterating over the files.

Source: https://boto3.amazonaws.com/v1/documentation/api/latest/guide/s3-uploading-files.html

Upvotes: 2

Related Questions