Apricot
Apricot

Reputation: 3021

Getting botocore.exceptions.NoCredentialsError: Unable to locate credentials especially when I using CronTab

I understand we have couple of answers for this error. However, my issue is different hence the question.

I am pushing audio files from a raspberry pi to AWS-S3 instance. The upload works without any problem when I run the script manually. However the same script when run through a crontab instance it throws up the above error.

My python code is as follows:

import boto3
import argparse
import os
ap=argparse.ArgumentParser()
ap.add_argument("-f","--filetoupload", required=True, help="file to upload")
args=vars(ap.parse_args())
file = os.path.basename(args['filetoupload'])
client=boto3.client('s3', region_name='ap-south-1')
print("[INFO:] Uploading file to cloud")
client.upload_file(args['filetoupload'],'MyS3Bucket',file)
print("[INFO:] File upload completed successfully")

I am calling this python script in a bash script.

python /home/pi/s3upload.py --filetoupload /home/pi/upload/${FILENAME}.mp3

The mp3 file gets created and as said above, when I run it manually it runs without any trouble.

I checked for the config and credentials files within .aws folder. As indicated in other responses in SO, they start with default and have the right credentials set up.

The complete error message is as follows:

[INFO:] Uploading file to cloud
Traceback (most recent call last):
  File "/home/pi/s3upload.py", line 10, in <module>
    client.upload_file(args['filetoupload'],'MyS3Bucket',file)
  File "/usr/local/lib/python2.7/dist-packages/boto3/s3/inject.py", line 110, in upload_file
    extra_args=ExtraArgs, callback=Callback)
  File "/usr/local/lib/python2.7/dist-packages/boto3/s3/transfer.py", line 279, in upload_file
    future.result()
  File "/usr/local/lib/python2.7/dist-packages/s3transfer/futures.py", line 73, in result
    return self._coordinator.result()
  File "/usr/local/lib/python2.7/dist-packages/s3transfer/futures.py", line 233, in result
    raise self._exception
botocore.exceptions.NoCredentialsError: Unable to locate credentials
script finished

Upvotes: 2

Views: 7858

Answers (1)

jeremycg
jeremycg

Reputation: 24955

By default, when crontab runs a script, it sets the $HOME variable to be /.

AWS looks for its credentials in a location which resolves to $HOME/.aws.

So, you need to either move your .aws directory into the root dir, which is probably not a good idea, or redefine $HOME to point to the folder that contains your .aws directory.

The easiest way to do this, if you are running your python script from a shell script, is to add:

export /HOME=/home/pi/

To the start of your shell script.

Upvotes: 2

Related Questions