django boto3:NoCredentialsError--用于查找凭据的UBLE

django boto3:NoCredentialsError--用于查找凭据的UBLE,django,amazon-web-services,boto3,iis-10,Django,Amazon Web Services,Boto3,Iis 10,我正在运行django网站,该网站由IIS在AmazonEC2实例(Windows10)上提供服务,我正在使用boto3模块发送电子邮件 我安装了awscli并运行了aws配置和设置aws访问密钥 我的电子邮件发送代码如下所示: import boto3 from botocore.exceptions import ClientError from django.shortcuts import redirect, render from django.http import HttpResp

我正在运行django网站,该网站由IIS在AmazonEC2实例(Windows10)上提供服务,我正在使用boto3模块发送电子邮件

我安装了awscli并运行了aws配置和设置aws访问密钥

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

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')
我曾经在这个网站运行时使用CMD中的“python manage.py runserver 0.0.0.0:80”发送电子邮件,但在我将其部署到IIS上并添加SSL后,它就不再工作了


我发现apache有点类似,但不知道如何适应我的环境。

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


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

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


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

我使用IAM角色启动EC2实例时,NoCredentialsError消失,但现在它返回error.html,可能是什么问题?大概你的应用程序正在决定呈现error.html,因此你应该能够调试该应用程序并了解如何使其执行此操作。我使用IAM角色启动了EC2实例,NoCredentialsError已消失,但现在它返回error.html,可能是什么问题?大概你的应用程序正在决定呈现error.html,因此你应该能够调试应用程序,并了解发生了什么事情使它这样做。
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',