pythonazureazure-blob-storagesizeofazure-sdk-python

How to know the size of an Azure blob object via Python Azure SDK


Following the Microsoft Azure documentation for Python developers. The azure.storage.blob.models.Blob class does have a private method called __sizeof__(). But it returns a constant value of 16, whether the blob is empty (0 byte) or 1 GB. Is there any method/attribute of a blob object with which I can dynamically check the size of the object?

To be clearer, this is how my source code looks like.

for i in blobService.list_blobs(container_name=container, prefix=path):
    if i.name.endswith('.json') and r'CIJSONTM.json/part' in i.name:
        #do some stuffs

However, the data pool contains many empty blobs having legitimate names, and before I #do some stuffs, I want to have an additional check on the size to judge whether I am dealing with an empty blob.

Also, bonus for what exactly does the __sizeof__() method give, if not the size of the blob object?


Solution

  • I want to have an additional check on the size to judge whether I am dealing with an empty blob.

    We could use the BlobProperties().content_length to check whether it is a empty blob.

    BlockBlobService.get_blob_properties(block_blob_service,container_name,blob_name).properties.content_length
    

    The following is the demo code how to get the blob content_length :

    from azure.storage.blob import BlockBlobService
    block_blob_service = BlockBlobService(account_name='accoutName', account_key='accountKey')
    container_name ='containerName'
    block_blob_service.create_container(container_name)
    generator = block_blob_service.list_blobs(container_name)
    for blob in generator:
        length = BlockBlobService.get_blob_properties(block_blob_service,container_name,blob.name).properties.content_length
        print("\t Blob name: " + blob.name)
        print(length)