是否可以在Django项目中使用多个AWS帐户?

时间:2018-08-14 08:40:14

标签: python django amazon-web-services amazon-ses

我想在一个Django项目中使用多个AWS帐户...

对于某些客户,电子邮件必须从一个帐户发送,其余用户必须从另一个帐户获取电子邮件。

1 个答案:

答案 0 :(得分:0)

您可以使用boto3并使用不同的凭据连接到AWS。

安装boto3软件包。

pip install boto3

然后连接到您的AWS账户并发送电子邮件(或更多)。

import boto3
from botocore.exceptions import ClientError

SENDER = "Sender Name <sender@example.com>"
RECIPIENT = "recipient@example.com"
CONFIGURATION_SET = "ConfigSet"
SUBJECT = "Amazon SES Test (SDK for Python)"
BODY_TEXT = ("Test")
BODY_HTML = """<html><head></head><body><h1>Test</h1></body></html>"""            
CHARSET = "UTF-8"

ACCESS_KEY = 'foo'
SECRET_KEY = 'foo'
SESSION_TOKEN = 'foo'

client = boto3.client(
    'ses',
    region_name="us-west-2",
    aws_access_key_id=ACCESS_KEY,
    aws_secret_access_key=SECRET_KEY,
    aws_session_token=SESSION_TOKEN,
)

try:
    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,
        # If you are not using a configuration set, comment or delete the
        # following line
        ConfigurationSetName=CONFIGURATION_SET,
    )
# 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'])

来源: https://docs.aws.amazon.com/ses/latest/DeveloperGuide/send-using-sdk-python.html https://boto3.readthedocs.io/en/latest/guide/quickstart.html#installation