0

Below is my code to upload a file to the Azure Blob Store using the

com.microsoft.azure.storage

library

public class BlobUploader {
    private CloudBlobContainer blobContainer;
    private static Logger LOGGER = LoggerFactory.getLogger(BlobUploader.class);

    /**
     * Constructor of the BlobUploader
     * 
     * @param storageAccountName The storage account name where the files will be uploaded to.
     * @param storageAccountKey The storage account key of the storage account
     * @param containerName The container name where the files will be uploaded to.
     */
    public BlobUploader( String storageAccountName, String storageAccountKey, String containerName ) {

        String storageConnectionString = "DefaultEndpointsProtocol=http;AccountName=" + storageAccountName + ";AccountKey=" + storageAccountKey;

        CloudStorageAccount storageAccount;
        try {
            storageAccount = CloudStorageAccount.parse( storageConnectionString );
            CloudBlobClient blobClient = storageAccount.createCloudBlobClient();
            // Retrieve reference to a previously created container.
            this.blobContainer = blobClient.getContainerReference( containerName );
        } catch ( Exception e ) {
            LOGGER.error( "failed to construct blobUploader", e );
        }
    }

    public void upload( String filePath ) throws Exception {

        // Define the path to blob in the container
        String blobPath = "/uploads";
        File fileToBeUploaded = new File( filePath );
        String fileName = fileToBeUploaded.getName();

        String blobName = blobPath + fileName;

        // Create or overwrite the blob with contents from a local file.
        CloudBlockBlob blob = blobContainer.getBlockBlobReference( blobName );

        System.out.println( "start uploading file " + filePath + " to blob " + blobName );

        blob.upload( new FileInputStream( fileToBeUploaded ), fileToBeUploaded.length() );
        System.out.println( "upload succeeded." );
    }
}

I am looking for an API, where, given a file path to a file uploaded to the Azure Blob Store, it can return me the properties of that file, specifically, the date and time uploaded.

Is there an API in Java that supports this?

David Makogon
  • 69,407
  • 21
  • 141
  • 189
saltmangotree
  • 171
  • 4
  • 11

1 Answers1

2

I am looking for an API, where, given a file path to a file uploaded to the Azure Blob Store, it can return me the properties of that file, specifically, the date and time uploaded.

The method you're looking for is downloadAttributes() which returns an object of type BlobProperties will set blob's properties that are of type BlobProperties. It will contain information about the blob. The method you would want to use there is getLastModified().

However this will return the date/time when the blob was last updated. So if you create a blob and make no changes to it, this property can be used to find out when it was uploaded. However if you make any changes to the blob after it has been created (like setting properties/metadata etc.), then the value returned is the date/time when it was last changed.

If you're interested in finding about when a blob was created, you may want to store this information as custom metadata along with the blob.

You can get detailed information about the SDK here: http://azure.github.io/azure-storage-java/.

Gaurav Mantri
  • 128,066
  • 12
  • 206
  • 241
  • that was helpful, thanks. I am interested in the upload time, so metadata wasn't really required, thanks however for the info. Small correction : The return type of downloadAttributes() is void, it does not return an object of type BlobProperties explicitly, though internally this might be the case. And so you do blob.downloadAttributes(); followed by System.out.println(blob.getProperties().getLastModified()); – saltmangotree Feb 25 '17 at 18:16
  • Thank you for pointing out my mistake. I corrected my answer. – Gaurav Mantri Feb 25 '17 at 18:20
  • just one more question before we finish off - I am able to get the properties of a file DirA/DirB/file.csv, but am not able to get similar properties for directories, say DirA, or DirB. Aren't directories also blobs? How can I get similar properties for directories? – saltmangotree Feb 26 '17 at 02:51
  • 1
    Reason for this is there are no directories in Azure Blobs. Please see my detailed answer here: http://stackoverflow.com/questions/26718243/how-to-create-empty-folder-in-azure-blob-storage/26719191#26719191. If you need to make use of directories, you would need to use `Azure Files`. HTH. – Gaurav Mantri Feb 26 '17 at 04:13
  • 1
    @saltmangotree There is not real directory concept on Azure Blob Storage, but virtual directory. If you want to get the properties of virtual directory, please refer to the usage of class [`CloudFileDirectory`](https://azure.github.io/azure-storage-java/com/microsoft/azure/storage/file/CloudFileDirectory.html) & [`FileDirectoryProperties`](https://azure.github.io/azure-storage-java/com/microsoft/azure/storage/file/FileDirectoryProperties.html) of Azure Storage SDK for Java. – Peter Pan Feb 28 '17 at 07:58
  • @GauravMantri, thanks once again. Apologies for the late upvote - lying sick the last couple of days. – saltmangotree Mar 02 '17 at 17:37
  • No problem :). Hope you're feeling better now! – Gaurav Mantri Mar 02 '17 at 17:40