django boto3:NoCredentialsError —难于找到凭据

问题描述 投票:0回答:1

我正在django网站上运行,该网站由IIS在amazon ec2实例(Windows 10)上提供,我正在使用boto3模块发送电子邮件。

我安装了awscli并运行了aws配置并设置了aws访问密钥check out attached image

我的电子邮件发送代码如下:

import boto3
from botocore.exceptions import ClientError
from django.shortcuts import redirect, render
from django.http import HttpResponseRedirect

def sendEmail(request):

    if request.method == 'POST':
        SENDER = "Sender Name <xxx>"

        RECIPIENT = "xxx"

        AWS_REGION = "eu-west-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', region_name=AWS_REGION)


        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'])
            return render(request, 'error.html')
        else:
            print("Email sent! Message ID:"),
            print(response['MessageId'])
            return render(request, 'success.html')

其中xxx表示在AWS上已验证的电子邮件。

我的错误看起来像这样:

Django Version: 3.0.3
Exception Type: NoCredentialsError
Exception Value:    
Unable to locate credentials
Exception Location: c:\users\administrator\python38\lib\site-packages\botocore\auth.py in add_auth, line 357
Python Executable:  c:\users\administrator\python38\python.exe
Python Version: 3.8.1
Python Path:    
['.',
 'c:\\users\\administrator\\python38\\python38.zip',
 'c:\\users\\administrator\\python38\\DLLs',
 'c:\\users\\administrator\\python38\\lib',
 'c:\\users\\administrator\\python38',
 'c:\\users\\administrator\\python38\\lib\\site-packages',

[我曾经在该网站仅使用CMD中的“ python manage.py runserver 0.0.0.0:80”运行时发送电子邮件,但在将其部署到IIS并添加SSL后,它不再起作用。

我在Apache上找到了类似的question,但不知道如何适应我的情况。

django amazon-web-services boto3 iis-10
1个回答
2
投票

我怀疑您遇到的问题是IIS和您的应用程序所运行的有效用户不是您为其配置凭据的同一用户(管理员)。因此,您的应用无法找到凭据文件,因为它的主目录不相同。

但是,这不是向EC2上运行的应用程序提供凭据的正确方法。相反,您应该使用IAM角色启动EC2实例,而不是在~/.aws/credentials文件中的实例上手动配置凭据。

© www.soinside.com 2019 - 2024. All rights reserved.