This article is contributed. See the original author and article here.

This article provides a python sample code for put block blob list.

The maximum size for a block blob created via Put Blob is 256 MiB for version 2016-05-31 and later, and 64 MiB for older versions. If your blob is larger than 256 MiB for version 2016-05-31 and later, or 64 MiB for older versions, you must upload it as a set of blocks. Therefore, if you want to upload a blob larger than 256 MiB, you may need to use Put Block List operations

This sample code need to use azure-storage-blob sdk version 2.1.0 .

Install the sdk as following:

pip install azure-storage-blob==2.1.0

 

Then refer the below sample code:

 

 

from azure.storage.blob import BlockBlobService, PublicAccess, BlobBlock
import random, string
from random import randint
 
# Block Blob Operations
def block_blob_operations():
    # Define the upload file
    file_to_upload = "largerthan256MBfile.mp4"
    # Define the block size
    block_size = 1024*1024*4 #4MB
 
    # Create blockblob_service object
    blockblob_service = BlockBlobService(
        account_name="youraccountname", account_key="youraccountkey")
 
    # Define container name
    container_name = 'blockblobcontainer' + get_random_name(6)
    print("Container name:" + container_name)
 
    try:
        # Create a new container
        print('1. Create a container with name - ' + container_name)
        blockblob_service.create_container(container_name)
 
        blocks = []
 
        # Read the file
        print('2. Upload file to block blob')
        with open(file_to_upload, "rb") as file:
            file_bytes = file.read(block_size)
            while len(file_bytes) > 0:
                block_id = get_random_name(32)
                blockblob_service.put_block(container_name, file_to_upload, file_bytes, block_id)
 
                blocks.append(BlobBlock(id=block_id))
 
                file_bytes = file.read(block_size)
 
        blockblob_service.put_block_list(container_name, file_to_upload, blocks)
 
        print('3. Get the block list')
        blockslist = blockblob_service.get_block_list(container_name, file_to_upload, None, 'all')
        blocks = blockslist.committed_blocks
 
        print('4. Enumerate blocks in block blob')
        for block in blocks:
            print('Block ' + block.id)
    finally:
        print('5. Completed')
        #if blockblob_service.exists(container_name):
         #   blockblob_service.delete_container(container_name)
def get_random_name(length):
    return ''.join(random.choice(string.ascii_lowercase) for i in range(length)) 
# Main method.
if __name__ == '__main__':
    block_blob_operations()

 

 

 

Tested above python code, following is run result:

pythonrunresult.png

 

 

Brought to you by Dr. Ware, Microsoft Office 365 Silver Partner, Charleston SC.