Python 使用 Boto3 配置文件覆盖 S3 端点
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32618216/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Overwrite S3 endpoint using Boto3 configuration file
提问by Vor
OVERVIEW:
概述:
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.
我试图在boto3
使用配置文件 ( ~/aws/confg
) 时覆盖某些变量。在我的用例中,我想使用fakes3
服务并将 S3 请求发送到本地主机。
EXAMPLE:
例子:
In boto
(not boto3
), I can create a config in ~/.boto
similar to this one:
在boto
(不是boto3
)中,我可以创建一个~/.boto
类似于这个的配置:
[s3]
host = localhost
calling_format = boto.s3.connection.OrdinaryCallingFormat
[Boto]
is_secure = False
And client can successfully pick up desired changes and instead of sending traffic to real S3 service, it will send it to the localhost.
客户端可以成功获取所需的更改,而不是将流量发送到真正的 S3 服务,而是将其发送到本地主机。
>>> import boto
>>> boto.connect_s3()
S3Connection:localhost
>>>
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
.
我试图使用boto3
库实现类似的结果。通过查看源代码,我发现我可以使用~/aws/config
位置。我还发现,在示例配置unittests
文件夹中botocore
。
I tried to modify config to achieve desired behaviour. But unfortunately it doesn't work.
我试图修改配置以实现所需的行为。但不幸的是它不起作用。
Here is the config:
这是配置:
[default]
aws_access_key_id = XXXXXXXXX
aws_secret_access_key = YYYYYYYYYYYYYY
region = us-east-1
is_secure = False
s3 =
host = localhost
QUESTION:
题:
- How to overwrite
clients
variables using config file? - Where can I find a complete list of allowed variables for the configuration?
- 如何
clients
使用配置文件覆盖变量? - 在哪里可以找到配置的允许变量的完整列表?
采纳答案by ChillarAnand
You cannot set host in config file, however you can override it from your code with boto3.
您不能在配置文件中设置主机,但是您可以使用 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())
回答by Jordon Phillips
boto3
only reads the signature version for s3 from that config file. You may want to open a feature request, but for now here is how you can address a custom endpoint:
boto3
仅从该配置文件中读取 s3 的签名版本。您可能想要打开一个功能请求,但现在您可以通过以下方式处理自定义端点:
import boto3
from botocore.utils import fix_s3_host
resource = boto3.resource(service_name='s3', endpoint_url='http://localhost')
resource.meta.client.meta.events.unregister('before-sign.s3', fix_s3_host)
That bit about the meta is important because boto3
automatically changes the endpoint to your_bucket_name.s3.amazonaws.com
when it sees fit 1. If you'll be working with both your own host and s3, you may wish to override the functionality rather than removing it altogether.
关于元的那一点很重要,因为它会boto3
自动将端点更改为your_bucket_name.s3.amazonaws.com
它认为合适的时间1。如果您将同时使用自己的主机和 s3,您可能希望覆盖该功能而不是完全删除它。
回答by okwap
Another way:
其它的办法:
import boto3
s3client = boto3.client('s3', endpoint_url='http://X.X.x.X:8080/',
aws_access_key_id = 'XXXXXXX',
aws_secret_access_key = 'XXXXXXXX')
bucket_name = 'aaaaa'
s3client.create_bucket(Bucket=bucket_name)
回答by Andi-OTC
using boto3 resource:
使用 boto3 资源:
import boto3
# use third party object storage
s3 = boto3.resource('s3', endpoint_url='https://URL:443',
aws_access_key_id = 'AccessKey',
aws_secret_access_key = 'SecertKey')
# Print out bucket names
for bucket in s3.buckets.all():
print(bucket.name)