简体   繁体   English

django boto3: NoCredentialsError -- Ubale 来定位凭证

[英]django boto3: NoCredentialsError -- Ubale to locate credentials

I am running django website which is served by IIS on an amazon ec2 instance(windows 10) and I'm using boto3 module to send an email.我正在亚马逊 ec2 实例(windows 10)上运行由 IIS 提供的 django 网站,我正在使用 boto3 模块发送电子邮件。

I installed awscli and ran aws configure and set up my aws access keys我安装了 awscli 并运行了 aws configure 并设置了我的 aws 访问密钥查看附加的图像

My email sending code looks like this:我的电子邮件发送代码如下所示:

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')

Where xxx indicates verified emails on AWS.其中 xxx 表示 AWS 上经过验证的电子邮件。

My error looks like this:我的错误是这样的:

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',

I used to send emails when this website was just running using "python manage.py runserver 0.0.0.0:80" in CMD but after I deployed it on IIS and also added SSL it doesn't work anymore.我曾经在该网站刚刚运行时使用 CMD 中的“python manage.py runserver 0.0.0.0:80”发送电子邮件,但在我将其部署在 IIS 上并添加 SSL 后,它不再起作用。

I found kinda similar question for apache but don't know how to adjust to my circumstances.我发现 apache 有点类似的问题,但不知道如何适应我的情况。

I suspect that the problem you are having is that the effective user that IIS and your app are running under is not the same user (Administrator) that you configured the credentials for.我怀疑您遇到的问题是 IIS 和您的应用程序在其下运行的有效用户与您为其配置凭据的用户(管理员)不同。 Hence your app cannot find the credentials file because it's home directory is not the same.因此,您的应用程序找不到凭据文件,因为它的主目录不相同。

However, this is not the correct way to provide credentials to applications running on EC2.但是,这不是向 EC2 上运行的应用程序提供凭据的正确方法。 Instead, you should launch EC2 instances with an IAM role, rather than manually configuring credentials on the instance in the ~/.aws/credentials file.相反,您应该使用 IAM 角色启动 EC2 实例,而不是在~/.aws/credentials文件中的实例上手动配置凭证。

If you are not using aws cli then directly on python file you can specify aws access keys in boto3 object like:如果您不使用 aws cli,则直接在 python 文件上,您可以在 boto3 对象中指定 aws 访问键,例如:

client = boto3.client(
           'ses', 
            region_name=AWS_REGION,
            aws_access_key_id=aws_public_key_paste_here, 
            aws_secret_access_key=aws_secret_key_paste_here,
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM