4

I'm trying to learn how to use the Azure Table Storage service. I'm following this tutorial: https://learn.microsoft.com/en-us/azure/storage/storage-python-how-to-use-table-storage and I am simply copy pasting the code into a jupyter notebook. I've setup a storage account and am successfully using the blob storage. Also from a notebook.

Code from the tutorial:

from azure.storage.table import TableService, Entity
table_service = TableService(account_name='myaccount', account_key='mykey')
table_service.create_table('tasktable')

When I run the last line I get the following error and I'm not sure what I am doing wrong to cause it

---------------------------------------------------------------------------
Error                                     Traceback (most recent call last)
/usr/local/lib/python3.5/site-packages/azure/storage/storageclient.py in _perform_request(self, request, parser, parser_args, operation_context)
    205                     _add_date_header(request)
--> 206                     self.authentication.sign_request(request)
    207 

/usr/local/lib/python3.5/site-packages/azure/storage/_auth.py in sign_request(self, request)
     96 
---> 97         self._add_authorization_header(request, string_to_sign)
     98 

/usr/local/lib/python3.5/site-packages/azure/storage/_auth.py in _add_authorization_header(self, request, string_to_sign)
     50     def _add_authorization_header(self, request, string_to_sign):
---> 51         signature = _sign_string(self.account_key, string_to_sign)
     52         auth_string = 'SharedKey ' + self.account_name + ':' + signature

/usr/local/lib/python3.5/site-packages/azure/storage/_common_conversion.py in _sign_string(key, string_to_sign, key_is_base64)
     87     if key_is_base64:
---> 88         key = _decode_base64_to_bytes(key)
     89     else:

/usr/local/lib/python3.5/site-packages/azure/storage/_common_conversion.py in _decode_base64_to_bytes(data)
     77         data = data.encode('utf-8')
---> 78     return base64.b64decode(data)
     79 

/usr/local/Cellar/python3/3.5.1/Frameworks/Python.framework/Versions/3.5/lib/python3.5/base64.py in b64decode(s, altchars, validate)
     89         raise binascii.Error('Non-base64 digit found')
---> 90     return binascii.a2b_base64(s)
     91 

Error: Incorrect padding

During handling of the above exception, another exception occurred:

AzureException                            Traceback (most recent call last)
<ipython-input-17-192b23ba629f> in <module>()
----> 1 table_service.create_table('tasktable')

/usr/local/lib/python3.5/site-packages/azure/storage/table/tableservice.py in create_table(self, table_name, fail_on_exist, timeout)
    520         if not fail_on_exist:
    521             try:
--> 522                 self._perform_request(request)
    523                 return True
    524             except AzureHttpError as ex:

/usr/local/lib/python3.5/site-packages/azure/storage/table/tableservice.py in _perform_request(self, request, parser, parser_args, operation_context)
   1087     def _perform_request(self, request, parser=None, parser_args=None, operation_context=None):
   1088         _update_storage_table_header(request)
-> 1089         return super(TableService, self)._perform_request(request, parser, parser_args, operation_context)

/usr/local/lib/python3.5/site-packages/azure/storage/storageclient.py in _perform_request(self, request, parser, parser_args, operation_context)
    264                     sleep(retry_interval)
    265                 else:
--> 266                     raise ex
    267             finally:
    268                 # If this is a location locked operation and the location is not set,

/usr/local/lib/python3.5/site-packages/azure/storage/storageclient.py in _perform_request(self, request, parser, parser_args, operation_context)
    240                     if sys.version_info >= (3,):
    241                         # Automatic chaining in Python 3 means we keep the trace
--> 242                         raise AzureException(ex.args[0])
    243                     else:
    244                         # There isn't a good solution in 2 for keeping the stack trace

AzureException: Incorrect padding
Christian Hagel
  • 147
  • 1
  • 5
  • I'm sure this is a silly question, but you're using your information for `myaccount` and `mykey`, right? – Scovetta Jan 17 '17 at 08:12
  • Yes :-) The connection is not a problem. I just din't want to post my keys :-) – Christian Hagel Jan 17 '17 at 08:16
  • 1
    The stack trace implies that the `account_key` you're passing in isn't Base64-encoded. Any chance you're using a connection string or something else? – Scovetta Jan 17 '17 at 08:26
  • @ChristianHagel It seems that your issue was caused by the incorrect account key which should be copied at [here](https://i.stack.imgur.com/aDCcM.png), and there was a similar SO thread http://stackoverflow.com/questions/28105954/azure-storage-queues-create-queue-getting-binascii-error-incorrect-padding with yours, please check them. Any update, please feel free to let me know. – Peter Pan Jan 17 '17 at 10:19
  • @ChristianHagel I tried to reproduce your issue, but works fine with correct account information on my own linux environment & Azure Jupyter notebook. Just when my code with the key missing the last `=` symbol or adding more `=` symbol, it throwed the incorrect padding exception. – Peter Pan Jan 17 '17 at 10:31
  • @PeterPan-MSFT I go to Azure portal and to storage access keys. I have two keys there. I've used them both to connect to blob storage. Here is an example of a key (with *** instead of real characters): KW4Fd**********************************************************************Jwz0Ad5LddQ== It's 89 chars long. I get the error using the exact key. – Christian Hagel Jan 18 '17 at 06:07
  • 1
    @PeterPan-MSFT I'm an idiot. I've figure out the problem. I had the variable names in quotes. Sorry about the confusion. Late nights... – Christian Hagel Jan 18 '17 at 06:24
  • @ChristianHagel Thanks for your feedback and congratulation. The length of the correct account key is **88**. – Peter Pan Jan 18 '17 at 06:34

2 Answers2

3

As a summary, the issue was caused by the variable name for account key within some mistake. Accroding to the error information Error: Incorrect padding, as @Scovetta said, it seems to be not BASE64 encoding. Some changes for the key like missing the last = symbol or adding more = symbol will cause the error. And the length of correct account key of Azure Storage is 88.

Peter Pan
  • 23,476
  • 4
  • 25
  • 43
1

Shameless necro after putting "incorrect padding azure" in my favorite search engine.
Turns out I was passing arguments like so : --account-key "$ACCOUNT_KEY", and Azure didn't understand the quotes.
Since it's supposed to be base64 encoded, all characters in there should be shell-safe, so if your input is OK there shouldn't be any problem putting it like that.

ubarbaxor
  • 142
  • 6