Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/347.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 boto3客户端NoRegionError:您必须仅在某些时候指定区域错误_Python_Linux_Amazon Web Services_Boto3_Aws Kms - Fatal编程技术网

Python boto3客户端NoRegionError:您必须仅在某些时候指定区域错误

Python boto3客户端NoRegionError:您必须仅在某些时候指定区域错误,python,linux,amazon-web-services,boto3,aws-kms,Python,Linux,Amazon Web Services,Boto3,Aws Kms,我有一个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.

我有一个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()

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

您必须以这样或那样的方式告诉双方您希望在哪个地区创建
kms
客户端。这可以使用
region\u name
参数显式完成,如下所示:

kms=boto3.client('kms',region_name='us-west-2')
或者,您可以在
~/.aws/config
文件中拥有与您的配置文件关联的默认区域,如下所示:

[default]
region=us-west-2
也可以使用环境变量,如中所示:

export AWS_DEFAULT_REGION=us-west-2

但是您确实需要告诉boto使用哪个区域。

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

您还可以在脚本本身中设置环境变量,而不是传递region_name参数

os.environ['AWS\u DEFAULT\u REGION']='your\u REGION\u name'

区分大小写可能很重要

os.environ['AWS_DEFAULT_REGION'] = 'your_region_name'

在我看来,区分大小写很重要。

对于Python 2,我发现boto3库不会从
~/.aws/config
中获取区域,如果该区域是在不同的配置文件中定义为默认的。 因此,您必须在会话创建中定义它

session=bot3.session(
配置文件\u name='NotDefault',
区域名称='ap-东南-2'
)
打印(会话。可用的\u配置文件)
client=session.client(
“ec2”
)
其中我的
~/.aws/config
文件如下所示:

[默认值]
区域=ap-东南-2
[不默认]
区域=ap-东南-2

我这样做是因为我对AWS的不同登录、个人和工作使用不同的配置文件。

对于使用CloudFormation模板的用户。您可以使用UserData和
AWS::REGION
设置
AWS\u DEFAULT\u REGION
环境变量。比如说,

MyInstance1:
    Type: AWS::EC2::Instance                
    Properties:                           
        ImageId: ami-04b9e92b5572fa0d1 #ubuntu
        InstanceType: t2.micro
        UserData: 
            Fn::Base64: !Sub |
                    #!/bin/bash -x

                    echo "export AWS_DEFAULT_REGION=${AWS::Region}" >> /etc/profile

或者,您可以运行以下命令(aws cli)

它会提示您输入区域

请注意
~/.aws/config
中的内容:

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

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

[配置文件名称]放在方括号中,因为boto3客户端无法从默认凭据集方法中找到AWS配置文件:。我如何知道自己要使用哪个区域?如果您试图访问现有资源,请选择这些资源存在的区域。如果要创建新资源,大多数人会选择地理位置最接近的地区,以加快响应速度。并非所有区域都支持同一组服务,因此如果您需要特定的服务,请确保选择支持该服务的区域。要了解哪些区域提供哪些服务:Amazon Web services区域表您知道为什么“export AWS_DEFAULT_region=us-west-2”吗或者~/.aws/config文件不工作,我希望boto3可以只使用运行代码的ec2实例的区域。
aws\u DEFAULT\u region
区分大小写。它应该是:
os.environ['aws\u DEFAULT\u region']=“us-east-1”
(大写)
[default]
region = ap-southeast-1
output = json

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