简体   繁体   English

如何为 AWS SDK for Python boto3 指定区域

[英]How do you specify region for AWS SDK for Python boto3

From cli I can execute the command: aws s3api list-objects –-bucket BUCKETNAME -—region REGIONAME从 cli 我可以执行命令: aws s3api list-objects –-bucket BUCKETNAME -—region REGIONAME

How do I equivalently specify the region for botocore3 list_objects_v2 ?如何等效地指定botocore3 list_objects_v2的区域?

It can be added while setting params可以在设置参数时添加

s3_client = boto3.client(
             's3',
             aws_access_key_id='access_key_here',
             aws_secret_access_key='access_key_secret_here',
             config=boto3.session.Config(signature_version='s3v4'),
             region_name='region_here'
            )

Then your request to get object lists:然后您请求获取对象列表:

response = s3_client.list_objects_v2(Bucket=bucket_name, Prefix='', Delimiter='/')

In the Python Docs there are a lot of examples of how to do this.在 Python 文档中有很多关于如何做到这一点的示例。 WHen you set a Service Client using the AWS SDK, all languages let you set a region.当您使用 AWS 开发工具包设置服务客户端时,所有语言都允许您设置区域。 The AWS SDK for Python is no different.适用于 Python 的 AWS 开发工具包也不例外。

Here is a code example that shows you how to set the region when creating a Service Client.这是一个代码示例,向您展示如何在创建服务客户端时设置区域。

import boto3
from botocore.config import Config

proxy_definitions = {
    'http': 'http://proxy.amazon.com:6502',
    'https': 'https://proxy.amazon.org:2010'
}

my_config = Config(
    region_name='us-east-2',
    signature_version='v4',
    proxies=proxy_definitions,
    proxies_config={
        'proxy_client_cert': '/path/of/certificate'
    }
)

client = boto3.client('kinesis', config=my_config)

More information can be found here:更多信息可以在这里找到:

https://boto3.amazonaws.com/v1/documentation/api/latest/guide/configuration.html#guide-configuration https://boto3.amazonaws.com/v1/documentation/api/latest/guide/configuration.html#guide-configuration

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

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