Reputation: 171
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?
Upvotes: 0
Views: 3168
Reputation: 136386
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 will set blob's properties that are of type BlobProperties
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/.
Upvotes: 2