我正在开发一个在美国使用的django项目,特别是纽约时区,该系统在AWS上托管,SES发送电子邮件。电子邮件后端使用django-anymail,它是SES的简单包装器,系统使用django core的send_mail
。
为了支持这一点,我选择了以下Django设置;
EMAIL_BACKEND = "anymail.backends.amazon_ses.EmailBackend"
LANGUAGE_CODE = 'en'
TIME_ZONE = 'America/New_York'
USE_I18N = False
USE_L10N = True
USE_TZ = True
ANYMAIL = {
"AMAZON_SES_CLIENT_PARAMS": {
"region_name": AWS_SES_REGION_NAME,
},
}
通过上述设置,django在启动时调用tzset()
来修改系统时区。这意味着botocore
用于签署SES请求的时间戳不是UTC,因为从消息发送中收到以下错误;
调用SendRawEmail操作时发生错误(ExpiredToken):请求中包含的安全令牌已过期
通过将设置更改为TIME_ZONE = 'UTC'
,可以成功发送电子邮件。
我只能假设这些请求是以UTC-4签署的,然后以UTC格式登录AWS。
django如何在特定时区运行,但是boto是否以UTC时间戳运行?
系统在docker容器中运行(预生产);
LocaleMiddleware
已装载我无法使用您所描述的设置重现您所看到的错误,但我可以通过额外的日志记录向您展示正常工作的内容,您可以将其与失败案例进行比较,以尝试查看什么是不同。
我在Django shell(python manage.py shell
)中运行此代码只是为了方便,但您可以将它放在调试视图或其他适合您的地方。
我们的工作理论是boto使用错误的时区来计算签署API请求的时间戳,所以让我们启用一些涵盖该区域的详细boto3 logging:
import boto3
boto3.set_stream_logger('botocore.auth') # log the signature logic
boto3.set_stream_logger('botocore.endpoint') # log the API request
# boto3.set_stream_logger('botocore.parsers') # log the API response (if you want)
现在尝试发送消息:
from django.core.mail import send_mail
send_mail("Test", "testing", None, ['[email protected]'])
您应该看到如下所示的日志输出:
2019-03-19 20:48:32,321 botocore.endpoint [DEBUG] Setting email timeout as (60, 60)
2019-03-19 20:48:32,580 botocore.endpoint [DEBUG] Making request for OperationModel(name=SendRawEmail) with params: {'body': {'Action': u'SendRawEmail', 'Version': u'2010-12-01', 'RawMessage.Data': [base64 message omitted]'}, 'url': u'https://email.us-east-1.amazonaws.com/', 'headers': {'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8', 'User-Agent': 'Boto3/1.9.117 Python/2.7.15 Darwin/18.2.0 Botocore/1.12.117 django-anymail/3.0-amazon-ses'}, 'context': {'auth_type': None, 'client_region': 'us-east-1', 'has_streaming_input': False, 'client_config': <botocore.config.Config object at 0x10dadd1d0>}, 'query_string': '', 'url_path': '/', 'method': u'POST'}
2019-03-19 20:48:32,581 botocore.auth [DEBUG] Calculating signature using v4 auth.
2019-03-19 20:48:32,581 botocore.auth [DEBUG] CanonicalRequest:
POST
/
content-type:application/x-www-form-urlencoded; charset=utf-8
host:email.us-east-1.amazonaws.com
x-amz-date:20190320T064832Z
content-type;host;x-amz-date
[redacted]
2019-03-19 20:48:32,582 botocore.auth [DEBUG] StringToSign:
AWS4-HMAC-SHA256
20190320T064832Z
20190320/us-east-1/ses/aws4_request
[redacted]
2019-03-19 20:48:32,582 botocore.auth [DEBUG] Signature:
[redacted]
2019-03-19 20:48:32,582 botocore.endpoint [DEBUG] Sending http request: <AWSPreparedRequest stream_output=False, method=POST, url=https://email.us-east-1.amazonaws.com/, headers={'Content-Length': '437', 'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8', 'Authorization': 'AWS4-HMAC-SHA256 Credential=[key id redacted]/20190320/us-east-1/ses/aws4_request, SignedHeaders=content-type;host;x-amz-date, Signature=[redacted]', 'X-Amz-Date': '20190320T064832Z', 'User-Agent': 'Boto3/1.9.117 Python/2.7.15 Darwin/18.2.0 Botocore/1.12.117 django-anymail/3.0-amazon-ses'}>
这里的重要部分是日期:
2019-03-19 20:48:32,581 botocore.auth [DEBUG] CanonicalRequest:
...
x-amz-date:20190320T064832Z
2019-03-19 20:48:32,582 botocore.auth [DEBUG] StringToSign:
...
20190320T064832Z
20190320/...
2019-03-19 20:48:32,582 botocore.endpoint [DEBUG] Sending http request: <AWSPreparedRequest ...
headers={
'Authorization': '.../20190320/...',
'X-Amz-Date': '20190320T064832Z', ...}>
请注意,签名计算均基于UTC日期(2019-03-20) - 而不是我的Django时区(2019-03-19)中的当前本地日期。
所以看起来boto3确实使用UTC进行签名计算,尽管有Django /环境时区。事实上,发送对我没有错误。
所以问题是,当你看到问题时有什么不同?
Authorization
和X-Amz-Date
? (如果你看到Date
标题而不是X-Amz-Date
,那也会很有趣。)希望能帮助您更接近解决方案,或者至少弄清楚哪些细节对于重现问题至关重要。