【发布时间】:2020-09-04 20:04:06
【问题描述】:
我有以下 Python/boto3 脚本,直接取自 AWS documentation:
import boto3
from botocore.exceptions import ClientError
# Replace sender@example.com with your "From" address.
# This address must be verified with Amazon SES.
SENDER = "mailman@sender.com"
# Replace recipient@example.com with a "To" address. If your account
# is still in the sandbox, this address must be verified.
RECIPIENT = "joe@receiver.com"
# If necessary, replace us-west-2 with the AWS Region you're using for Amazon SES.
AWS_REGION = "eu-central-1"
# The subject line for the email.
SUBJECT = "Amazon SES Test (SDK for Python)"
# The email body for recipients with non-HTML email clients.
BODY_TEXT = ("Amazon SES Test (Python)\r\n"
"This email was sent with Amazon SES using the "
"AWS SDK for Python (Boto).")
# The HTML body of the email.
BODY_HTML = """<html>
<head></head>
<body>
<h1>Amazon SES Test (SDK for Python)</h1>
<p>This email was sent with
<a href='https://aws.amazon.com/ses/'>Amazon SES</a> using the
<a href='https://aws.amazon.com/sdk-for-python/'>
AWS SDK for Python (Boto)</a>.</p>
</body>
</html>
"""
# The character encoding for the email.
CHARSET = "UTF-8"
# Create a new SES resource and specify a region.
client = boto3.client(
'ses', aws_access_key_id='AKIA[REDACTED]', aws_secret_access_key='[REDACTED]',
region_name=AWS_REGION
)
# Try to send the email.
try:
# Provide the contents of the email.
response = client.send_email(
Destination={
'ToAddresses': [
RECIPIENT,
],
},
Message={
'Body': {
'Html': {
'Charset': CHARSET,
'Data': BODY_HTML,
},
'Text': {
'Charset': CHARSET,
'Data': BODY_TEXT,
},
},
'Subject': {
'Charset': CHARSET,
'Data': SUBJECT,
},
},
Source=SENDER,
)
# Display an error if something goes wrong.
except ClientError as e:
print(e.response['Error']['Message'])
else:
print("Email sent! Message ID:"),
print(response['MessageId'])
两个域(sender.com 和 receiver.com)都在 SES 中进行了验证; SES 仍处于沙盒模式。我使用的 AWS 凭证适用于附加了以下策略的用户(通过其组):
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": "*",
"Resource": "*"
}
]
}
当我运行脚本时出现以下错误:
User `arn:aws:iam::[REDACTED]:user/[REDACTED]' is not authorized to perform `ses:SendEmail' on resource `arn:aws:ses:eu-central-1:[REDACTED]:identity/receiver.com'.
当从 AWS 控制台使用具有相同参数的“发送测试电子邮件”时,一切都按预期工作。
这里有什么问题?
【问题讨论】:
-
能否请您发布与此用户关联的 IAM 角色/组/策略,并指定您在网络上使用的用户/帐户是否与拥有这些凭证的用户/帐户不同?跨度>
-
@Andre.IDK 上述政策是唯一附加到用户的政策,并且来自其组(我已编辑问题以反映这一点)。
-
我明白了,这看起来像是名为 `` 的托管策略。如果是这种情况,策略应该足够了,但是您可以通过转到 IAM -> 用户 -> {user} -> 访问顾问来验证该用户实际上是否可以通过 Web 控制台访问 SES,在此选项卡中,您可以键入 SES它会告诉您用户是否可以访问它,如果可以,哪个策略/角色正在授予它。或者,您可以手动将策略附加到该用户,在资源上明确授予他们
ses:SendEmail和ses:SendRawEmail。 -
尝试添加另一个收件人,验证并再次测试。请告诉我们您使用的是哪个版本的 boto3?
-
您能否尝试在 CLI 中使用这些相同的凭据,看看您是否可以执行其他操作,例如列出存储桶 (
aws s3 ls)?如果可行,请尝试使用凭据创建一个 s3 客户端并以编程方式尝试它,看看您是否获得相同的列表。这至少应该告诉我们您使用的凭据是否实际上具有管理员访问权限。
标签: amazon-web-services boto3 boto amazon-ses