Search code examples
restazure-storage

Using List Blobs is it possible to get blob size instead of blob length?


I'm using the List Blob function with the REST API, according to documentation this previously gave you blob size, for example 2mb, but now gives you blob length, 3000 characters. Is it possible to revert to the earlier version and retrive blob size instead?

I've tried using the additional filters, and looking at the problem using different services like cloudshell, but no luck so far. Hoping there might be a simple answer i'm overlooking like using a different api-version?

Currently the code request i'm sending to the API is just:

https://<storage>.blob.core.windows.net/<container>?restype=container&comp=list&x-ms-version=2018-03-28

Solution

  • I am seeing that, but wondering if it's possible to get it as blob size expressed in MB/KB instead of content length?

    I agree with Gaurav Mantri's comment, REST API will return with size in bytes. If you need to get in kb or mb of blob content you can use python SDK.

    Here is the code to get the blob size in mb or kb.

    Code:

    from azure.storage.blob import BlobServiceClient
    
    
    connection_string = "your-storage-connection-string"
    blob_service_client = BlobServiceClient.from_connection_string(connection_string)
    container_client = blob_service_client.get_container_client("test")
    
    # List the blobs in the container
    blobs = container_client.list_blobs()
    
    for blob in blobs:
        blob_client = container_client.get_blob_client(blob.name)
        blob_properties = blob_client.get_blob_properties()
        blob_size_bytes = blob_properties.size
        blob_size_kb = blob_size_bytes / 1000
        blob_size_mb = blob_size_bytes / 1000000
        print("Blob name: {}, size: {} bytes, {} KB, {} MB".format(blob.name, blob_size_bytes, blob_size_kb, blob_size_mb))
    

    Output:

    Blob name: demo.csv, size: 2265 bytes, 2.265 KB, 0.002265 MB
    Blob name: xmlfile.xml, size: 4548 bytes, 4.548 KB, 0.004548 MB
    

    enter image description here

    Reference:

    List blobs with Python - Azure Storage | Microsoft Learn