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

心不动则不痛 提交于 2021-02-08 07:33:29

问题


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?


回答1:


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)


来源:https://stackoverflow.com/questions/53369766/how-to-know-the-size-of-an-azure-blob-object-via-python-azure-sdk

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!