如何使用boto3连接到Openstack Swift

问题描述 投票:2回答:1

我正在尝试使用boto3连接到OpenStack Swift。我需要列出特定存储桶中的所有对象。使用boto2我可以做到这一点,

parsed = urlparse.urlparse(cloud_url)
conn = boto.connect_s3(aws_access_key_id=cloud_user,
                       aws_secret_access_key=cloud_password,
                       port=parsed.port,
                       host=parsed.hostname,
                       is_secure=False,
                       calling_format=boto.s3.connection.OrdinaryCallingFormat())

buckets = conn.get_all_buckets()
for key in buckets:
    # This prints a list of bucket names.
    print key

但是在boto3中做同样的事情,

session = boto3.Session()
s3 = session.resource(service_name='s3',
                  use_ssl=False,
                  verify=False,
                  endpoint_url=cloud_url,
                  aws_access_key_id=cloud_user,
                  aws_secret_access_key=cloud_password)

print(list(s3.buckets.all()))

我遇到错误,

调用ListBuckets操作时发生错误(401):未经授权

我无法使用boto3在端点上执行任何操作,但是我使用的是相同的访问密钥和秘密密钥。

使用boto3时是否还需要设置其他内容?

boto3 openstack
1个回答
0
投票

这是我的操作方式:

import boto3
import botocore
boto3.set_stream_logger(name='botocore')  # this enables debug tracing
session = boto3.session.Session()
s3_client = session.client(
    service_name='s3',
    aws_access_key_id='ACCESS',
    aws_secret_access_key='SECRET',
    endpoint_url='https://YOUR.ENDPOINT.HOST/',
    # The next option is only required because my provider only offers "version 2"
    # authentication protocol. Otherwise this would be 's3v4' (the default, version 4).
    config=botocore.client.Config(signature_version='s3'),
)

s3_client.list_objects(Bucket='bucket_name')

[为了他人的利益:请注意,ACCESSSECRET与您的Swift用户名和密码相同;相反,它们是在对特定项目进行身份验证之后通过以下命令生成的“ EC2样式”凭据(您的环境变量中需要一个所谓的“作用域令牌”):openstack ec2 credentials create

也请检查此页面以了解OpenStack Swift的S3兼容性层支持哪些功能:https://docs.openstack.org/swift/latest/s3_compat.html
© www.soinside.com 2019 - 2024. All rights reserved.