Tutorial :Requires a valid Date or x-amz-date header?



Question:

I'm getting the following error when attempting to upload a file to S3:

S3StorageError: <?xml version="1.0" encoding="UTF-8"?>  <Error><Code>AccessDenied</Code><Message>AWS authentication requires a valid Date or x-amz-date header</Message><RequestId>7910FF83F3FE17E2</RequestId><HostId>EjycXTgSwUkx19YNkpAoY2UDDur/0d5SMvGJUicpN6qCZFa2OuqcpibIR3NJ2WKB</HostId></Error>  

I'm using Django with Django-Storages and Imagekit

My S3 settings in my settings.py looks as follows:

locale.setlocale(locale.LC_TIME, 'en_US')  DEFAULT_FILE_STORAGE = 'backends.s3.S3Storage'  AWS_ACCESS_KEY_ID = '************************'  AWS_SECRET_ACCESS_KEY = '*****************************'  AWS_STORAGE_BUCKET_NAME = 'static.blabla.com'  AWS_HEADERS = {         'x-amz-date': datetime.datetime.utcnow().strftime('%a, %d %b %Y %H:%M:%S GMT'),         'Expires': 'Thu, 15 Apr 2200 20:00:00 GMT',  }  from S3 import CallingFormat  AWS_CALLING_FORMAT = CallingFormat.SUBDOMAIN  

Thanks for any help you can give!


Solution:1

I found that timezone difference seemed to be a factor. I'm an expat from Melbourne who's currently in Malaysia. Changing back to my Melbourne timezone and this error went away (was occuring in all major s3 access tools I was using such as s3fox, s3hub etc.).


Solution:2

I think it's a bad idea having the 'x-amz-date' header in your settings.py as this file only gets run once each time the server is started.

I guess you were using the development server, which gets restarted every time you change some .py code.

Anyway, django-storages already has code to set the relevant header so you don't need that line at all.

I did additionally have a problem with the header as set by django-storages, see this issue on BitBucket for a fix: http://code.welldev.org/django-storages/issue/56/aws-authentication-requires-a-valid-date-or-x-amz-date


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »