Azure TableService()在生产中返回python ValueError,但未返回dev

时间:2020-10-16 05:00:25

标签: python azure-table-storage valueerror

我有运行GREAT的开发Python代码,该代码使用...

1 from azure.cosmosdb.table.tableservice import TableService
2 from azure.cosmosdb.table.models import Entity
...
85 table_service = TableService(connection_string = table_storage_connection_string)
86 perform_lookup = table_service.get_entity(mapping_table, 'test', 'foo')

将代码复制到prod并更改...的值后

  • table_storage_connection_string
    • Docs说可以使用connection_string
    • 已验证这是到表存储帐户的连接字符串
    • 它正在使用local.settings.json / Azure Portal App Settings中的Key Vault参考
  • mapping_table

...我收到错误消息:

Exception while executing function: Functions.my-func-prod <--- Result: Failure Exception: ValueError: You need to provide an account name and either an account_key or sas_token when creating a storage service. Stack: File "/azure-functions-host/workers/python/3.8/LINUX/X64/azure_functions_worker/dispatcher.py", line 343, in _handle__invocation_request call_result = await self._loop.run_in_executor( File "/usr/local/lib/python3.8/concurrent/futures/thread.py", line 57, in run result = self.fn(*self.args, **self.kwargs) File "/azure-functions-host/workers/python/3.8/LINUX/X64/azure_functions_worker/dispatcher.py", line 480, in __run_sync_func return func(**params) File "/home/site/wwwroot/my-func-prod/__init__.py", line 85, in main table_service = TableService(connection_string = table_storage_connection_string) File "/home/site/wwwroot/.python_packages/lib/site-packages/azure/cosmosdb/table/tableservice.py", line 173, in __init__ service_params = _TableServiceParameters.get_service_parameters( File "/home/site/wwwroot/.python_packages/lib/site-packages/azure/cosmosdb/table/common/_connection.py", line 110, in get_service_parameters params = _ServiceParameters._from_connection_string(connection_string, service) File "/home/site/wwwroot/.python_packages/lib/site-packages/azure/cosmosdb/table/common/_connection.py", line 153, in _from_connection_string return _ServiceParameters(service, File "/home/site/wwwroot/.python_packages/lib/site-packages/azure/cosmosdb/table/common/_connection.py", line 85, in __init__ raise ValueError(_ERROR_STORAGE_MISSING_INFO)
prop__StartTime 2020-10-16T04:50:07.099Z
prop__Duration  00:00:01.2898150
ProcessId   18
Microsoft.Azure.WebJobs.Host.FunctionInvocationException: Exception while executing function: Functions.my-func-prod

为什么TableService()在带有连接字符串的dev中工作正常,为什么在生产时需要Table Storage帐户名和帐户密钥或sas令牌?

编辑1:

用存储帐户的connection_stringaccount_name替换account_key时,在下一行(89)出现 new 错误:

1 from azure.cosmosdb.table.tableservice import TableService
2 from azure.cosmosdb.table.models import Entity
...
85 table_service = TableService(
86              account_name = table_storage_account_name,
87              account_key =  table_storage_account_key
88 )
89 perform_lookup = table_service.get_entity(mapping_table, 'test', 'foo')

新错误:

Exception while executing function: Functions.my-func-prod <--- Result: Failure Exception: AzureSigningError: Incorrect padding Stack: File "/azure-functions-host/workers/python/3.8/LINUX/X64/azure_functions_worker/dispatcher.py", line 343, in _handle__invocation_request call_result = await self._loop.run_in_executor( File "/usr/local/lib/python3.8/concurrent/futures/thread.py", line 57, in run result = self.fn(*self.args, **self.kwargs) File "/azure-functions-host/workers/python/3.8/LINUX/X64/azure_functions_worker/dispatcher.py", line 480, in __run_sync_func return func(**params) File "/home/site/wwwroot/my-func-prod/__init__.py", line 89, in main perform_lookup = table_service.get_entity(mapping_table, 'test', 'foo') File "/home/site/wwwroot/.python_packages/lib/site-packages/azure/cosmosdb/table/tableservice.py", line 896, in get_entity return self._perform_request(request, _convert_json_response_to_entity, File "/home/site/wwwroot/.python_packages/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 "/home/site/wwwroot/.python_packages/lib/site-packages/azure/cosmosdb/table/common/storageclient.py", line 386, in _perform_request raise ex File "/home/site/wwwroot/.python_packages/lib/site-packages/azure/cosmosdb/table/common/storageclient.py", line 358, in _perform_request raise ex File "/home/site/wwwroot/.python_packages/lib/site-packages/azure/cosmosdb/table/common/storageclient.py", line 302, in _perform_request self.authentication.sign_request(request) File "/home/site/wwwroot/.python_packages/lib/site-packages/azure/cosmosdb/table/_auth.py", line 35, in sign_request self._add_authorization_header(request, string_to_sign) File "/home/site/wwwroot/.python_packages/lib/site-packages/azure/cosmosdb/table/common/_auth.py", line 75, in _add_authorization_header raise _wrap_exception(ex, AzureSigningError)

有人在使用表存储之前解决了这个问题吗?

1 个答案:

答案 0 :(得分:1)

我尝试重现您的错误,但是使用connection_string以及使用account_name和account_key都可以正常工作。

connection_string同时包含AccountNameAccountKey,如下所示:

DefaultEndpointsProtocol=https;AccountName=<your-account-name>;AccountKey=<e60xxxxxxxxxxx==>;EndpointSuffix=core.windows.net

如果connection_string不包含AccountNameAccountKey,则会出现错误。

enter image description here

错误(填充错误)与您的帐户密钥不正确有关。请在门户中重新生成密钥,然后尝试使用新密钥。