使用Boto3配置文件覆盖S3端点 [英] Overwrite S3 endpoint using Boto3 configuration file
问题描述
我正在尝试使用配置文件(~/aws/confg
)覆盖boto3
中的某些变量.
在我的用例中,我想使用fakes3
服务并将S3请求发送到本地主机.
I'm trying to overwrite certain variables in boto3
using configuration file (~/aws/confg
).
In my usecase I want to use fakes3
service and send S3 requests to the localhost.
在boto
(不是boto3
)中,我可以在~/.boto
中创建与此配置类似的配置:
In boto
(not boto3
), I can create a config in ~/.boto
similar to this one:
[s3]
host = localhost
calling_format = boto.s3.connection.OrdinaryCallingFormat
[Boto]
is_secure = False
并且客户端可以成功获取所需的更改,而不是将流量发送到真实的S3服务,而是将其发送到本地主机.
And client can successfully pick up desired changes and instead of sending traffic to real S3 service, it will send it to the localhost.
>>> import boto
>>> boto.connect_s3()
S3Connection:localhost
>>>
我尝试过的事情:
我正在尝试使用boto3
库获得相似的结果.通过查看源代码,我发现我可以使用~/aws/config
位置.我还在botocore
的unittests
文件夹中找到了一个示例配置.
WHAT I TRIED:
Im trying to achieve similar result using boto3
library. By looking at the source code I found that I can use ~/aws/config
location. I've also found an example config in unittests
folder of botocore
.
我试图修改配置以实现所需的行为.但不幸的是,它不起作用.
I tried to modify config to achieve desired behaviour. But unfortunately it doesn't work.
这是配置:
[default]
aws_access_key_id = XXXXXXXXX
aws_secret_access_key = YYYYYYYYYYYYYY
region = us-east-1
is_secure = False
s3 =
host = localhost
问题:
- 如何使用配置文件覆盖
clients
变量? - 在哪里可以找到配置允许的变量的完整列表?
- How to overwrite
clients
variables using config file? - Where can I find a complete list of allowed variables for the configuration?
推荐答案
您不能在配置文件中设置主机,但是可以使用boto3从代码中覆盖它.
You cannot set host in config file, however you can override it from your code with boto3.
import boto3
session = boto3.session.Session()
s3_client = session.client(
service_name='s3',
aws_access_key_id='aaa',
aws_secret_access_key='bbb',
endpoint_url='http://localhost',
)
然后您就可以像往常一样进行互动了.
Then you can interact as usual.
print(s3_client.list_buckets())
这篇关于使用Boto3配置文件覆盖S3端点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!