Devstack 列出 s3 存储桶错误“您提供的 AWS 访问密钥 ID 在我们的记录中不存在”

问题描述

我根据这个https://docs.openstack.org/devstack/latest/按照Devstack的安装指南,然后按照这个配置keystoneauth中间件https://docs.openstack.org/swift/latest/overview_auth.html#keystone-auth 但是,当我尝试使用 boto3 和从 OpenStack ec2 credential create 生成的凭证列出存储桶时,出现错误“您提供的 AWS 访问密钥 ID 在我们的记录中不存在” 将不胜感激任何帮助

我的 boto3 代码

import boto3 s3 = boto3.resource('s3',aws_access_key_id='5d14869948294bb48f9bfe684b8892ca',aws_secret_access_key='ffcbcec69fb54622a0185a5848d7d0d2',) 

for bucket in s3.objects.all():
    print(bucket)

其中 2 个键的位置如下:

| access     | 5d14869948294bb48f9bfe684b8892ca| 
| links      | {'self': '10.180.205.202/identity/v3/users/…'} | 
| project_id | c128ad4f9a154a04832e41a43756f47d |
| secret     | ffcbcec69fb54622a0185a5848d7d0d2 |
| trust_id   | None |
| user_id    | 2abd57c56867482ca6cae5a9a2afda29 

运行@larsks 提供的命令后,我的 swift 端点得到了 public: http://10.180.205.202:8080/v1/AUTH_ed6bbefe5ab44f32b4891fc5e3e55f1f。确保我的 ec2 凭证在用户管理员和项目管理员下。

当我按照 Boto3 代码删除从 v1 开始的所有端点时,我收到错误 botocore.exceptions.ClientError: An error occurred () when calling the ListBuckets operation:

当我保留 AUTH 部分时,我得到了 botocore.exceptions.ClientError: An error occurred (412) when calling the ListBuckets operation: Precondition Failed

通过在local.conf中添加enable_service s3api并再次堆栈解决了之前的问题。这可能是因为 OpenStack 需要知道它正在使用 s3api,从它说的文档中可以看到 Swift will be configured to act as a S3 endpoint for Keystone so effectively replacing the nova-objectstore.

解决方法

您的问题可能是您没有告诉 boto3 如何连接到您的 OpenStack 环境,因此默认情况下它会尝试连接到 Amazon 的 S3 服务(在您的示例中,您也没有传递您的访问权限密钥和秘密密钥,但我假设这只是创建示例时的一个错字)。

如果您想连接到 OpenStack 对象存储服务,您需要先从目录中获取该服务的端点。您可以通过运行 openstack catalog list 从命令行获取此信息。如果您使用 openstack Python 模块,您还可以通过编程方式检索它。

您可以只检查 openstack catalog list 的输出并查找 swift 服务,或者您可以使用例如解析它jq

$ openstack catalog list -f json |
  jq -r '.[]|select(.Name == "swift")|.Endpoints[]|select(.interface == "public")|.url'
https://someurl.example.com/swift/v1

无论如何,您都需要将端点传递给boto3

>>> import boto3
>>> session = boto3.session.Session()
>>> s3 = session.client(service_name='s3',... aws_access_key_id='access_key_id_goes_here',... aws_secret_access_key='secret_key_goes_here',... endpoint_url='endpoint_url_goes_here')
>>> s3.list_buckets()
{'ResponseMetadata': {'RequestId': 'tx0000000000000000d6a8c-0060de01e2-cff1383c-default','HostId': '','HTTPStatusCode': 200,'HTTPHeaders': {'transfer-encoding': 'chunked','x-amz-request-id': 'tx0000000000000000d6a8c-0060de01e2-cff1383c-default','content-type': 'application/xml','date': 'Thu,01 Jul 2021 17:56:51 GMT','connection': 'close','strict-transport-security': 'max-age=16000000; includeSubDomains; preload;'},'RetryAttempts': 0},'Buckets': [{'Name': 'larstest','CreationDate': datetime.datetime(2018,12,5,20,19,4000,tzinfo=tzutc())},{'Name': 'larstest2','CreationDate': datetime.datetime(2019,3,7,21,4,628000,{'Name': 'larstest4','CreationDate': datetime.datetime(2021,18,47,54,510000,tzinfo=tzutc())}],'Owner': {'DisplayName': 'lars','ID': '4bb09e3a56cd451b9d260ad6c111fd96'}}
>>>

请注意,如果来自 openstack catalog list 的端点 url 包含一个版本(例如 .../v1),您可能希望删除它。