0

I want to use the following method to delete 30 day old blobs. However, it seems that the part of "sourceBlob.getProperties().getLastModified().getTime();" generates an exception. What could be the possible solutions? The exception message shows null only. The Azure Storage type is Storage (general purpose v1)

public static void deleteOldBlobs(String source) {
    try {
        System.out.println("deleteOldBlobs started");
        CloudStorageAccount storageAccount = CloudStorageAccount
                .parse(PropertyUtil.getProperty("storageConnectionString"));
        CloudBlobClient blobClient = storageAccount.createCloudBlobClient();
        CloudBlobContainer sourceContainer = blobClient.getContainerReference(source);
        long daysBack = 30;
        System.out.println(daysBack);
        long cutoff = (daysBack * (24 * 60 * 60 * 1000));
        for (ListBlobItem blobItem : sourceContainer.listBlobs()) {
            String sourceFileName = new File(blobItem.getUri().toString()).getName();
            System.out.println(sourceFileName);
            CloudBlockBlob sourceBlob = sourceContainer.getBlockBlobReference(sourceFileName);
            System.out.println(sourceBlob.getProperties().getLastModified().getTime());
            long diff = new Date().getTime()- sourceBlob.getProperties().getLastModified().getTime();

            if (diff > cutoff) {
                sourceBlob.deleteIfExists();
            }
        }
        System.out.println("deleteOldBlobs ended");
    } catch (Exception ex) {
        System.out.println(ex.getMessage());
    } finally {
    }
}
Learner
  • 69
  • 2
  • 9
  • What's the exception you're getting? Please edit your question and include those details. – Gaurav Mantri May 06 '20 at 04:25
  • Also, please see this if your storage account's type is either General Purpose v2 (GPv2) or Blob Storage: https://stackoverflow.com/questions/56903837/delete-files-older-than-x-number-of-days-from-azure-blob-storage-using-azure-fun/56906611#56906611. – Gaurav Mantri May 06 '20 at 04:26
  • I only got a null for the exception message. The storage account type is Storage (general purpose v1). I will add the above information to my post. – Learner May 06 '20 at 04:31

1 Answers1

2

You'll need to call downloadAttributes() method to populate the properties of the blob.

Your following line of code:

CloudBlockBlob sourceBlob = sourceContainer.getBlockBlobReference(sourceFileName);

is simply creating an instance of CloudBlockBlob with the properties set with default values. When you call downloadAttributes method, a network call will be made and blob's attributes will be fetched.

So your code would be:

CloudBlockBlob sourceBlob = sourceContainer.getBlockBlobReference(sourceFileName);
sourceBlob.downloadAttributes();
System.out.println(sourceBlob.getProperties().getLastModified().getTime());

Considering you have already listed the blobs, you can cast your blobItem as a CloudBlob and then you should not have to fetch the attributes (which makes a network call and will make the entire process much slower and error prone).

Gaurav Mantri
  • 128,066
  • 12
  • 206
  • 241