InvalidToken Error Occurred when calling the PutObject operation

1

I am hosting a Django webapp on Vercel and storing the Django media files in an S3 bucket. I established a bucket policy to allow for get/put capabilities and created an IAM user for keys. When testing from my local host this working fine and I was able to get images and put new images (for user profiles) without issue. Now that I have implemented the same process within Vercel; I am receiving the following error: botocore.exceptions.ClientError: An error occurred (InvalidToken) when calling the PutObject operation: The provided token is malformed or otherwise invalid. It should be noted the default profile image is* being served properly and without issue from this bucket, so I know the connection is at least established. My AWS settings within Django are as follows:

MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = "/media/"
DEFAULT_FILE_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'
# AWS
AWS_ACCESS_KEY_ID = 'xxxxxxxxxxxxxxxxxxxxxxxxxx'
AWS_SECRET_ACCESS_KEY = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
AWS_STORAGE_BUCKET_NAME = 'xxxxxxxxxxxxxx'
AWS_S3_REGION_NAME = 'us-east-1'
AWS_QUERYSTRING_AUTH = False
AWS_SECURITY_TOKEN_IGNORE_ENVIRONMENT = True
AWS_IGNORE_ENVIRONMENT_CREDENTIALS = True

I was under the impression if I was using long term programmatic keys, a session token wouldn't matter.
The AWS_SECURITY_TOKEN_IGNORE_ENVIRONMENT and AWS_IGNORE_ENVIRONMENT_CREDENTIALS were not originally set and only used for troubleshooting at the suggestion of a forum. I also opened a support ticket with Vercel to see if there is a `/.aws credentials or config file where the app is being hosted. I have spent hours troubleshooting this an have yet to come up with a solution. Any assistance with this would be greatly appreciated.

  • I'm getting exactly the same problem as you. Also using Django & React & Vercel. It's strange that it works in localhost, but fails on vercel. I've also spent one day on this issue.

2 Answers
0

Hi, this issues seems to happen to others: https://github.com/aws-amplify/amplify-js/issues/9773 This ticket proposes a solution (see toward the end) that you may try in your case

profile pictureAWS
EXPERT
answered a year ago
  • Thanks for the response. I reviewed the post and it is specific to React and the user has defined his own methods for upload. I am using Django's built in save function and tested that it worked prior to deploying to a live env. That leads me to believe there is some sort of hangup outside of my code.

0

Thanks to your post, I realized that it should be a problem in Vercel, not Django. (Before that my google key word was always "Django + error message"). Then I find this article: https://vercel.com/guides/how-can-i-use-aws-s3-with-vercel It says we should first configure the env variables in project settings on the Vercel dashboard. I added AWS credentials and region, redeployed it (this is important!) , and, it finally works. I hope this can help you a little bit.

Cxy
answered 9 months ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions