gpt4 book ai didi

python - 如何使用 python 连接到 Cosmos DB Emulator 表存储?

转载 作者:行者123 更新时间:2023-12-01 08:04:45 28 4
gpt4 key购买 nike

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

版本号等:

  • Windows 10
  • Python 3.6(CPython)
  • azure-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界面提供帐户名和帐户 key 。其余的都是我通过实验得出的一些猜测。如果没有这些额外的参数,我可以从日志输出中看到 SDK 正在尝试联系远程主机(可能是基于云的 Cosmos 服务的一部分),这当然会失败。 is_emulated 标志也是如此;当 False 时,将连接到远程主机。

为了查看模拟器是否有问题,我尝试使用cosmosdb的SQL/文档接口(interface)的azure-cosmos SDK来访问模拟器。使用以下代码效果很好:

from azure.cosmos.cosmos_client import CosmosClient

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

这一切顺利完成,我可以在模拟器的数据浏览器中看到数据库已创建。

有人可以引用 CosmosDB 模拟器表接口(interface)的正确连接字符串吗?

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

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

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

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

[5] How do I disable the security certificate check in Python requests

最佳答案

A way to disable certificate validation in the python SDK for table storage

深入调试create_table函数后,我发现可以在requests包中的session.py中使用verify属性。

enter image description here

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

A way to get the certificates validated

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

enter image description here

另外,请引用此document中的建议还有这个ssl configuration link .

enter image description here

关于python - 如何使用 python 连接到 Cosmos DB Emulator 表存储?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55592403/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com