1

我在本地运行 Azure CosmosDB 模拟器,我希望使用 Python 中的 Table 接口。Microsoft 提供了用于连接到 Table API [1] 的 azure-cosmosdb-table python sdk。即使我能够连接到云中的实际 CosmosDB 帐户,我也无法成功连接到模拟器。

版本号等:

  • 视窗 10
  • Python 3.6 (CPython)
  • 天蓝色 cosmosdb 表 1.0.5

我用于尝试访问模拟器的代码:

import logging
from azure.cosmosdb.table import TableService

from nossl import no_ssl_verification

logging.basicConfig(level=logging.DEBUG)

connection = {
    'AccountName': 'devstoreaccount1',
    # 'AccountEndpoint': 'https://localhost:8081/',
    'AccountKey': 'C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw=="',
    # 'EndpointSuffix': 'localhost:8081',
    # 'DefaultEndpointsProtocol': 'https',
    'TableEndpoint': 'https://localhost:8081',
}
connection_string = ';'.join('{k}={v}'.format(k=k, v=v) for k, v in connection.items())
print(connection_string)

with no_ssl_verification():
    ts = TableService(
        endpoint_suffix = "https://localhost:8081",
        connection_string= connection_string,
        is_emulated=True)
    ts.create_table('mytable')

no_ssl_verification上下文管理器禁用 SDK 使用的请求库中的所有 SSL 验证。它是从 Stack Overflow [5] 上的答案复制而来的。

上面的示例代码产生以下错误:

AccountName=devstoreaccount1;AccountKey=C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==";TableEndpoint=https://localhost:8081
DEBUG:azure.cosmosdb.table._auth:String_to_sign=POST

application/json
Thu, 18 Apr 2019 13:50:42 GMT
/devstoreaccount1/Tables
INFO:azure.storage.common.storageclient:Client-Request-ID=f52a8b36-61e0-11e9-8fc0-a44cc8cee022 Outgoing request: Method=POST, Path=/Tables, Query={'timeout': None}, Headers={'Content-Type': 'application/json', 'Prefer': 'return-no-content', 'Accept': 'application/json;odata=minimalmetadata', 'DataServiceVersion': '3.0;NetFx', 'MaxDataServiceVersion': '3.0', 'Content-Length': '24', 'x-ms-version': '2018-03-28', 'User-Agent': 'Azure-Storage/1.4.0-None (Python CPython 3.6.4;
Windows 10)', 'x-ms-client-request-id': 'f52a8b36-61e0-11e9-8fc0-a44cc8cee022', 'x-ms-date': 'Thu, 18 Apr 2019 13:50:42 GMT', 'Authorization': 'REDACTED'}.
DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): localhost:8081
DEBUG:urllib3.connectionpool:https://localhost:8081 "POST /Tables HTTP/1.1" 400 None
INFO:azure.storage.common.storageclient:Client-Request-ID=f52a8b36-61e0-11e9-8fc0-a44cc8cee022 Receiving Response: Server-Timestamp=Thu, 18 Apr 2019 13:50:42 GMT, HTTP Status Code=400, Message=Bad Request, Headers={'transfer-encoding': 'chunked', 'content-type': 'application/json', 'server': 'Microsoft-HTTPAPI/2.0',
'x-ms-activity-id': '00000000-0000-0000-0000-000000000000', 'x-ms-gatewayversion': 'version=2.2.0.0', 'date': 'Thu, 18 Apr 2019 13:50:42 GMT'}.
INFO:azure.storage.common.storageclient:Client-Request-ID=f52a8b36-61e0-11e9-8fc0-a44cc8cee022 Operation failed: checking if the operation should be retried. Current retry count=0, Server-Timestamp=Thu, 18 Apr 2019 13:50:42 GMT, HTTP status code=400, Exception=Bad Request{"code":"BadRequest","message":"Request url is invalid., Microsoft.Azure.Documents.Common/2.2.0.0"}.
ERROR:azure.storage.common.storageclient:Client-Request-ID=f52a8b36-61e0-11e9-8fc0-a44cc8cee022 Retry policy did not allow for a retry: Server-Timestamp=Thu, 18 Apr 2019 13:50:42 GMT, HTTP status code=400, Exception=Bad Request{"code":"BadRequest","message":"Request url is invalid., Microsoft.Azure.Documents.Common/2.2.0.0"}.
Traceback (most recent call last):
  File "c:/Users/josteb/sandbox/cosmoskvstore/examples.py", line 24, in <module>
    ts.create_table('mytable')
  File "C:\Users\josteb\.virtualenvs\cosmoskvstore-cPSP36p_\lib\site-packages\azure\cosmosdb\table\tableservice.py", line 539, in create_table
    _dont_fail_on_exist(ex)
  File "C:\Users\josteb\.virtualenvs\cosmoskvstore-cPSP36p_\lib\site-packages\azure\storage\common\_error.py", line 88, in _dont_fail_on_exist
    raise error
  File "C:\Users\josteb\.virtualenvs\cosmoskvstore-cPSP36p_\lib\site-packages\azure\cosmosdb\table\tableservice.py", line 536, in create_table
    self._perform_request(request)
  File "C:\Users\josteb\.virtualenvs\cosmoskvstore-cPSP36p_\lib\site-packages\azure\cosmosdb\table\tableservice.py", line 1106, in _perform_request
    return super(TableService, self)._perform_request(request, parser, parser_args, operation_context)
  File "C:\Users\josteb\.virtualenvs\cosmoskvstore-cPSP36p_\lib\site-packages\azure\storage\common\storageclient.py", line 430, in _perform_request
    raise ex
  File "C:\Users\josteb\.virtualenvs\cosmoskvstore-cPSP36p_\lib\site-packages\azure\storage\common\storageclient.py", line 358, in _perform_request
    raise ex
  File "C:\Users\josteb\.virtualenvs\cosmoskvstore-cPSP36p_\lib\site-packages\azure\storage\common\storageclient.py", line 344, in _perform_request
    HTTPError(response.status, response.message, response.headers, response.body))
  File "C:\Users\josteb\.virtualenvs\cosmoskvstore-cPSP36p_\lib\site-packages\azure\storage\common\_error.py", line 115, in _http_error_handler
    raise ex
azure.common.AzureHttpError: Bad Request
{"code":"BadRequest","message":"Request url is invalid., Microsoft.Azure.Documents.Common/2.2.0.0"}

我无法在任何地方找到正确记录的用于仿真器的连接参数。模拟器的 Web 界面提供帐户名称和帐户密钥。剩下的就是一些猜测,这是我通过实验得出的。如果没有这些额外参数,我可以从日志输出中看到 SDK 正在尝试联系远程主机(可能是基于云的 Cosmos 服务的一部分),这当然会失败。is_emulated旗帜也是如此;时False,连接到远程主机。

为了查看模拟器是否有问题,我尝试使用 azure-cosmos SDK for the SQL/document interface for cosmosdb 来访问模拟器。使用以下代码效果很好:

from azure.cosmos.cosmos_client import CosmosClient

cc = CosmosClient('https://localhost:8081', auth={'masterKey': 'C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw=='})
cc.CreateDatabase({'id': 'mydb'})

这完全没有问题,我可以在模拟器的数据浏览器中看到数据库已经创建。

有没有人参考 CosmosDB 模拟器的表接口的正确连接字符串?

[1] https://docs.microsoft.com/en-us/python/api/azure-cosmosdb-table/?view=azure-python

[2] https://docs.microsoft.com/en-us/python/api/azure-cosmos/?view=azure-python

[3] https://docs.microsoft.com/en-us/azure/cosmos-db/local-emulator-export-ssl-certificates

[4] https://bugs.python.org/issue28547

[5]如何在 Python 请求中禁用安全证书检查

4

1 回答 1

2

一种在用于表存储的 python SDK 中禁用证书验证的方法

经过深度调试create_table,发现可以verify在requests包中的session.py中设置属性。

在此处输入图像描述

默认值为 True,您可以将其设置为 False 以跳过 SSL 验证。

一种验证证书的方法

另一个名为 cert 的属性,可以配置为证书路径。

在此处输入图像描述

此外,请参考本文档中的建议和此ssl 配置链接

在此处输入图像描述

于 2019-04-12T06:53:34.257 回答