3

I have azure blob container named azure-con inside that have folder test-data inside this folder multiple folders gets created based on date year and minutes 0 , 1 inside this 21 ,22 and so on.

I want to get the file from latest folder path would be-

azure-con/1/22/333.json

I tried this code but does not work for me unable to get the latest folder and file inside that using LastModified -

 var containerName = "azure-con";  

  var latestBlob = containerName
    .OfType<CloudBlockBlob>()
    .OrderByDescending(m => m.Properties.LastModified).FirstOrDefault();



            //var blobName = "azure-con/1/22/333.json";
            I want to pass this above blobName from latestBlob code which gives latest file path

            var storageAccount = CloudStorageAccount.Parse(connectionString);
            var blobClient = storageAccount.CreateCloudBlobClient();
            var container = blobClient.GetContainerReference(containerName);
            var blob = container.GetBlockBlobReference(blobName);
Neo
  • 15,491
  • 59
  • 215
  • 405
  • 1
    Possible duplicate of [How to retrieve azure blob storage file attributes such as create data and audit trail in c#](https://stackoverflow.com/questions/33518637/how-to-retrieve-azure-blob-storage-file-attributes-such-as-create-data-and-audit) – Murray Foxcroft Jan 29 '18 at 15:24
  • no answer provider for the same question you shared here hopfuly will get it for this question – Neo Jan 29 '18 at 15:27
  • Even though there is no answer, the approach is correct, you need to create a mechanism to track it yourself. https://learn.microsoft.com/en-us/azure/storage/blobs/storage-properties-metadata - can you not get away with using the LastModified property? – Murray Foxcroft Jan 29 '18 at 15:31
  • nope i don't able to get the latest folder and file inside that. – Neo Jan 29 '18 at 15:33
  • It is not clear to me if you want to get the latest *created* file or the latest *modified* file. And is the path build using azure-con/{year}/{month}/{day}.json or something else? – Peter Bons Jan 29 '18 at 15:35
  • 1
    There is no physical folder in blobs, the "folder" is just part of the path to the blob. The closest you can get is https://stackoverflow.com/questions/36266792/getting-the-latest-file-modified-from-azure-blob – Murray Foxcroft Jan 29 '18 at 15:40
  • got you but i always get empty as result :/ I tried with simple path still – Neo Jan 29 '18 at 16:51

1 Answers1

3

Here is a way to get the latest file name using Pandas and Python. Definitely not the best for large scale, but interpretable.

 def get_latest_file_name_in_blob(conn_url):
        """
        Retrieves the file name of the latest file in a specific blob in Azure Blob store.
        """
        from azure.storage.blob import ContainerClient
        import pandas as pd
    
        container = ContainerClient.from_container_url(container_url=conn_url)
    
        file_date_df = pd.DataFrame(columns=['blob_name', 'last_modified'])
        count = 0
    
        for blob in container.list_blobs():
            file_date_df.loc[count] = [f'{blob.name}', f'{blob.last_modified}']
            count = count + 1
    
        file_date_df['last_modified'] = pd.to_datetime(file_date_df['last_modified'], format='%Y-%m-%d %H:%M:%S')
    
        return file_date_df.loc[file_date_df['last_modified'] == file_date_df['last_modified'].max()]['blob_name']
Charl
  • 41
  • 5