【问题标题】:How to connect to Openstack Swift using boto3如何使用 boto3 连接到 Openstack Swift
【发布时间】:2020-03-06 23:06:36
【问题描述】:

我正在尝试使用 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】:

    这是我的做法:

    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

    【讨论】:

      猜你喜欢
      • 2021-10-15
      • 1970-01-01
      • 2023-03-17
      • 1970-01-01
      • 1970-01-01
      • 2018-04-28
      • 1970-01-01
      • 2018-04-28
      • 2020-06-27
      相关资源
      最近更新 更多