s

Python For Absolute Beginner

python get last modified file from blob storage container edit button Edit

author
Murugan Andezuthu Dharmaratnam | calendar 21 September 2021 | 3663

To get the last modified file from a blob storage container you can use the ContainerClient. In the below code I am getting the full list of files from container / path using the list_blobs method & then I am sorting using the sorted function in descending order. Then I am returning the first element from the sorted list.

Sample Code

To get the last modified file from a blob storage container you can use the ContainerClient. In the below code I am getting the full list of files from container / path using the list_blobs method & then I am sorting using the sorted function in descending order. Then I am returning the first element from the sorted list.

def get_latest_file_from_blob():
    AZURE_STORAGE_CONNECTION_STRING = 'DefaultEndpointsProtocol=https;AccountName=yourstorageaccountname;AccountKey=your storage account key==;EndpointSuffix=core.windows.net'
    container = ContainerClient.from_connection_string(conn_str=AZURE_STORAGE_CONNECTION_STRING, 
                                                container_name="yourcontainerpath")
    blob_list = container.list_blobs("your/path")
    sorted_blob_list = sorted(blob_list,key=lambda x: x.creation_time, reverse=True)
    return sorted_blob_list[0].name