I am trying to write a python script that uses watchdog to look for file creation and upload that to s3 using boto3. However, my boto3 credentials expire after every 12hrs, So I need to renew them. I am storing my boto3 credentials in ~/.aws/credentials
. So right now I am trying to catch the S3UploadFailedError
, renew the credentials, and write them to ~/.aws/credentials
. But though the credentials are getting renewed and I am calling boto3.client('s3')
again its throwing exception.
What am I doing wrong? Or how can I resolve it?
Below is the code snippet
try:
s3 = boto3.client('s3')
s3.upload_file(event.src_path,'bucket-name',event.src_path)
except boto3.exceptions.S3UploadFailedError as e:
print(e)
get_aws_credentials()
s3 = boto3.client('s3')
According to the documentation, the client looks in several locations for credentials and there are other options that are also more programmatic-friendly that you might want to consider instead of the
.aws/credentials
file.Quoting the docs:
In your case, since you are already catching the exception and renewing the credentials, I would simply pass the new ones to a new instance of the client like so:
If instead you are using these same credentials elsewhere in the code to create other clients, I'd consider setting them as environment variables:
Again, quoting the docs: