我有一个boto3客户端:

boto3.client('kms')

但这发生在新机器上,它们动态地打开和关闭。

    if endpoint is None:
        if region_name is None:
            # Raise a more specific error message that will give
            # better guidance to the user what needs to happen.
            raise NoRegionError()

为什么会这样?为什么只是部分时间?


当前回答

我相信,默认情况下,boto选择在aws cli中设置的区域。您可以运行命令#aws configure并按enter(它会显示您在aws cli with region中设置的信用)两次来确认您的区域。

其他回答

您也可以运行以下命令(aws cli)

aws configure --profile $PROFILE_NAME

它会提示您输入区域。

注意~/。aws /配置是:

[default]
region = ap-southeast-1
output = json

[profile prod]
region = ap-southeast-1
output = json

[profile配置文件名称]

我相信,默认情况下,boto选择在aws cli中设置的区域。您可以运行命令#aws configure并按enter(它会显示您在aws cli with region中设置的信用)两次来确认您的区域。

如果您使用AWS Lambda,您的代码将在部署时工作, 因为Lambda被部署在特定的区域。

如果您正在使用Linux,一种简单的方法是在~/处创建配置文件(您应该将其命名为config而不需要任何扩展名)。Aws(如果. Aws目录不存在,则在用户home ~),并在该配置文件中添加如下所示的区域。

(默认) 地区= whatever-aws-region

如果您正在使用lambda,那么您可能希望使用部署lambda的区域。 你可以使用下面的方法

import boto3
import json
import os

def lambda_handler(event, context):
    region = os.environ['AWS_REGION']
    print('Lambda region: ', region)
    kms = boto3.client('kms', region_name=region)