Search code examples
pythonazure-table-storagevalueerror

Azure TableService() returns python ValueError in prod, but not dev


I've got dev Python code running GREAT that uses...

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')

After copying the code to prod and changing the values of...

  • table_storage_connection_string
    • Docs say using connection_string is ok.
    • Validated this is a connection string to the Table Storage Account
    • It IS using a Key Vault reference in local.settings.json/Azure Portal App Settings
  • mapping_table

...I get the error:

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

Why would TableService() work just fine in dev with a connection string, require the Table Storage account name and account key or sas token when in prod?

EDIT 1:

When connection_string is substituted with account_name and account_key of the storage account, I get a new error on the subsequent line (89):

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')

New Error:

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)

Anyone overcome this issue before with Table Storage?


Solution

  • I tried to reproduce your error, but both using connection_string and using account_name and account_key all worked well.

    The connection_string contains both AccountName and AccountKey, looks like this:

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

    If the connection_string does not include the AccountName or AccountKey, the error will appear.

    enter image description here

    The error(Incorrect padding) is related to your account key being incorrect. Please regenerate the key in the portal and try with the new one.