【问题标题】:django boto3: NoCredentialsError -- Ubale to locate credentialsdjango boto3:NoCredentialsError - 无法找到凭据
【发布时间】:2021-06-24 20:47:26
【问题描述】:

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

我安装了 awscli 并运行了 aws configure 并设置了我的 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')

其中 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】:

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

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

    【讨论】:

    • 我使用 IAM 角色启动了 EC2 实例,NoCredentialsError 消失了,但现在返回 error.html,可能是什么问题?
    • 大概是您的应用决定呈现 error.html,因此您应该能够调试应用并了解发生了什么使其执行此操作。
    【解决方案2】:

    如果您不使用 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,
    )
    

    【讨论】:

      猜你喜欢
      • 2017-08-21
      • 2022-12-16
      • 2016-01-22
      • 2022-07-20
      • 2021-09-13
      • 1970-01-01
      • 2020-04-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多