Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/amazon-web-services/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python AWS Lambda:“;参数验证失败";_Python_Amazon Web Services_Aws Lambda_Boto3 - Fatal编程技术网

Python AWS Lambda:“;参数验证失败";

Python AWS Lambda:“;参数验证失败";,python,amazon-web-services,aws-lambda,boto3,Python,Amazon Web Services,Aws Lambda,Boto3,我有一个lambda函数,在这里我发送电子邮件,邮件模板作为一个对象从S3 bucket中获取。我已经在我的计算机上本地运行了代码,我工作得很好。当我将它粘贴到lambda函数中时,它显示以下错误 Response: { "errorMessage": "Parameter validation failed:\nInvalid type for parameter Message.Body.Html.Data, value: b\"<html>\\n<head>&l

我有一个lambda函数,在这里我发送电子邮件,邮件模板作为一个对象从S3 bucket中获取。我已经在我的计算机上本地运行了代码,我工作得很好。当我将它粘贴到lambda函数中时,它显示以下错误

Response:
{
  "errorMessage": "Parameter validation failed:\nInvalid type for parameter Message.Body.Html.Data, value: b\"<html>\\n<head></head>\\n<body>\\n  <h1>Amazon SES Test (SDK for Python)</h1>\\n  <p>This email was sent with\\n    <a href='https://aws.amazon.com/ses/'>Amazon SES</a> using the\\n    <a href='https://aws.amazon.com/sdk-for-python/'>\\n      AWS SDK for Python (Boto)</a>.</p>\\n</body>\\n</html>\", type: <class 'bytes'>, valid types: <class 'str'>",
  "errorType": "ParamValidationError",
  "stackTrace": [
    [
      "/var/task/lambda_function.py",
      65,
      "lambda_handler",
      "Source=SENDER,"
    ],
    [
      "/var/runtime/botocore/client.py",
      314,
      "_api_call",
      "return self._make_api_call(operation_name, kwargs)"
    ],
    [
      "/var/runtime/botocore/client.py",
      586,
      "_make_api_call",
      "api_params, operation_model, context=request_context)"
    ],
    [
      "/var/runtime/botocore/client.py",
      621,
      "_convert_to_request_dict",
      "api_params, operation_model)"
    ],
    [
      "/var/runtime/botocore/validate.py",
      291,
      "serialize_to_request",
      "raise ParamValidationError(report=report.generate_report())"
    ]
  ]
}

Request ID:
"7b13a612-97f6-4278-9825-724abeaa3b51"

Function Logs:
START RequestId: 7b13a612-97f6-4278-9825-724abeaa3b51 Version: $LATEST
Parameter validation failed:
Invalid type for parameter Message.Body.Html.Data, value: b"<html>\n<head></head>\n<body>\n  <h1>Amazon SES Test (SDK for Python)</h1>\n  <p>This email was sent with\n    <a href='https://aws.amazon.com/ses/'>Amazon SES</a> using the\n    <a href='https://aws.amazon.com/sdk-for-python/'>\n      AWS SDK for Python (Boto)</a>.</p>\n</body>\n</html>", type: <class 'bytes'>, valid types: <class 'str'>: ParamValidationError
Traceback (most recent call last):
  File "/var/task/lambda_function.py", line 65, in lambda_handler
    Source=SENDER,
  File "/var/runtime/botocore/client.py", line 314, in _api_call
    return self._make_api_call(operation_name, kwargs)
  File "/var/runtime/botocore/client.py", line 586, in _make_api_call
    api_params, operation_model, context=request_context)
  File "/var/runtime/botocore/client.py", line 621, in _convert_to_request_dict
    api_params, operation_model)
  File "/var/runtime/botocore/validate.py", line 291, in serialize_to_request
    raise ParamValidationError(report=report.generate_report())
botocore.exceptions.ParamValidationError: Parameter validation failed:
Invalid type for parameter Message.Body.Html.Data, value: b"<html>\n<head></head>\n<body>\n  <h1>Amazon SES Test (SDK for Python)</h1>\n  <p>This email was sent with\n    <a href='https://aws.amazon.com/ses/'>Amazon SES</a> using the\n    <a href='https://aws.amazon.com/sdk-for-python/'>\n      AWS SDK for Python (Boto)</a>.</p>\n</body>\n</html>", type: <class 'bytes'>, valid types: <class 'str'>

END RequestId: 7b13a612-97f6-4278-9825-724abeaa3b51
REPORT RequestId: 7b13a612-97f6-4278-9825-724abeaa3b51  Duration: 1608.47 ms    Billed Duration: 1700 ms    Memory Size: 128 MB Max Memory Used: 71 MB
)


它应该在
函数日志中成功返回消息ID
。有什么帮助吗?我看不出这背后有什么逻辑。

很难相信第二个代码可以工作,因为它包含与第一个错误日志中报告的相同的错误。我复制了代码,它以同样的方式失败

通过从错误日志中读取此消息,可以发现问题

参数Message.Body.Html.Data的类型无效,值:b“…”,类型:类字节,有效类型:类“str”:ParamValidationError

正文应为字符串类型,但
object\u content

object_content = s3_response['Body'].read()
是字节类型。您需要将其转换为字符串。例如,
BODY\u HTML=str(object\u content)

重要

不要在lambda代码中存储永久凭据

client = boto3.client('ses',aws_access_key_id=******,
                          aws_secret_access_key=*****,region_name='us-east-1')
s3_client = boto3.client('s3',aws_access_key_id=*********,
                          aws_secret_access_key=***********,region_name='us-east-1')
通过lambda执行角色为lambda函数授予权限

如果您需要提供对lambda功能的跨帐户访问,则在该角色中包括
sts:AssumeRole
权限,然后使用通过调用
sts:AssumeRole

client = boto3.client(
    'ses',
    aws_access_key_id=...,
    aws_secret_access_key=...,
    aws_session_token=... // !!!
)

但同样,从不在函数代码中存储永久凭据。您确定下次将代码放入某个公共存储库时不会忘记将这些值替换为
******
?每次?

谢谢你,马图斯。我刚刚开始使用AWS服务,我真的用******替换了它们,并将它们开源。
client = boto3.client(
    'ses',
    aws_access_key_id=...,
    aws_secret_access_key=...,
    aws_session_token=... // !!!
)