下面是我使用以下代码将文件上传到 Azure Blob 存储的代码
com.microsoft.azure.storage
图书馆
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." );
}
}
我正在寻找一个 API,如果给定上传到 Azure Blob 存储的文件的文件路径,它可以返回该文件的属性,特别是上传的日期和时间。
Java 中是否有支持此功能的 API?
我正在寻找一个 API,如果给定上传到 Azure Blob 存储的文件的文件路径,它可以返回该文件的属性,特别是上传的日期和时间。
您正在寻找的方法是downloadAttributes()
哪些返回一个类型的对象 将设置 blob 类型的属性BlobProperties
BlobProperties
。它将包含有关 blob 的信息。您想在那里使用的方法是getLastModified()
.
但是,这将返回 blob 上次更新的日期/时间。因此,如果您创建一个 blob 并且不对其进行任何更改,则可以使用此属性来确定它的上传时间。但是,如果在创建 blob 后对其进行任何更改(例如设置属性/元数据等),则返回的值是上次更改的日期/时间。
如果您有兴趣了解 blob 的创建时间,您可能希望将此信息作为自定义元数据与 blob 一起存储。
您可以在此处获取有关 SDK 的详细信息:http : //azure.github.io/azure-storage-java/。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句