7

I follow the details given for putting the message into an azure queue from Azure Python SDK. When I try to put a message into queue,

from azure.storage import QueueService
queue_service = QueueService(account_name, account_key)
queue_service.put_message('taskqueue', 'Hello world!')

a message is put in the queue but it is empty. Any help will be appreciated.

Selva
  • 976
  • 1
  • 10
  • 23

3 Answers3

5

Seems like the official documentation needs an update. We need to encode the string which is missing from the docs.

Below code, I tested and worked for me:

    from azure.storage.queue import (
           QueueClient,
           BinaryBase64EncodePolicy,
           BinaryBase64DecodePolicy
    )
    ...
    queue_client = QueueClient.from_connection_string(
        AZURE_STORAGE_CONNECTION_STRING,
        QUEUE_NAME
        )

    # Setup Base64 encoding and decoding functions
    queue_client.message_encode_policy = BinaryBase64EncodePolicy()
    queue_client.message_decode_policy = BinaryBase64DecodePolicy()

    message = 'Hello World'
    message_bytes = message.encode('ascii')
    queue_client.send_message(
      queue_client.message_encode_policy.encode(content=message_bytes)
      )
  • We can not use the string directly with neither queue_client.message_encode_policy.encode nor even plain base64.b64encode('hello') methods because a byte liked object is needed.

      In [6]: base64.b64encode('hello')
      ---------------------------------------------------------------------------
      TypeError                                 Traceback (most recent call last)
      <ipython-input-6-b1f43373737a> in <module>
      ----> 1 base64.b64encode('hello')
      /usr/local/Cellar/python@3.9/3.9.0_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/base64.py in b64encode(s, altchars)
      ...
      TypeError: a bytes-like object is required, not 'str'
    
  • Plus the above code is using the latest SDK the method name changed.

  • Everything is tested under Python 3.9

samtoddler
  • 8,463
  • 2
  • 26
  • 21
4

I ran into a case of this, it worked like this.

message = base64.b64encode(json.dumps(custom_logentry).encode('utf-8')).decode('utf-8')

Later on I found an equivalent method of setting the QueueService instance's encoding function

from azure.storage.queue import QueueService, QueueMessageFormat
queue_service = QueueService(account_name, account_key)
queue_service.encode_function = QueueMessageFormat.text_base64encode
queue_service.put_message('taskqueue', base64.b64encode('Hello world!'))

Source is here. https://github.com/Azure/azure-storage-python/blob/master/azure-storage-queue/azure/storage/queue/models.py

Other options include binary_base64encode, and text_xmlencode etc., depending on what encoding your data use.

xiaochuanQ
  • 190
  • 5
  • Setting the encode function was correct but after that I simply needed to call put_message with a string: `queue_service.put_message('taskqueue', 'Hello world!')` and allow the queue_service to handling encoding. – Will Jul 24 '18 at 13:58
  • Your first approach works, but introduces unnecessary compute cycles (note the encode/decode cycle). The correct way - and correct answer - for the original question is to set the encode_function of the of the QueueService to the correct encoding. – Tobias Apr 23 '19 at 19:16
  • can you help on this post? https://stackoverflow.com/questions/62649998/poison-queue-content-into-main-queue/62653061#62653061 – Andrew Jul 02 '20 at 08:38
3

The problem here is encoding format. The default encoding format of python is "ASCII", whereas the message needs to be encoded in base-64 format before putting into queue. The following code did the work.

from azure.storage import QueueService
import base64
queue_service = QueueService(account_name, account_key)
queue_service.put_message('taskqueue', base64.b64encode('Hello world!'))
Selva
  • 976
  • 1
  • 10
  • 23
  • 1
    This doesn't work for me, it gives the error: TypeError: a bytes-like object is required, not 'str'. – Arklur Oct 02 '17 at 11:51
  • 1
    @Arklur This wont work because in python 3 the default encoding is Utf-8. I post a solution as another answer. – xiaochuanQ Jul 02 '18 at 18:53
  • Can you help on this issue? https://stackoverflow.com/questions/62649998/poison-queue-content-into-main-queue/62653061#62653061 – Andrew Jul 02 '20 at 08:38