在 Java 中读取 Azure Blob 存储中文件的文件属性
Reading file properties of a file in Azure Blob Store in Java
下面是我使用
将文件上传到 Azure Blob Store 的代码
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 Store 的文件的文件路径,它可以 return 我获取该文件的属性,特别是日期和上传时间。
Java中有支持这个的API吗?
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.
您要找的方法是downloadAttributes()
which returns an object of type BlobProperties
will set blob's properties that are of type BlobProperties
。它将包含有关 blob 的信息。您想要使用的方法是 getLastModified()
.
然而,这将 return 上次更新 blob 时的 date/time。因此,如果您创建了一个 blob 并且未对其进行任何更改,则此 属性 可用于查明它的上传时间。但是,如果您在创建 blob 后对其进行任何更改(如设置 properties/metadata 等),则值 returned 是上次更改时的 date/time。
如果您有兴趣了解 blob 的创建时间,您可能希望将此信息作为自定义元数据与 blob 一起存储。
您可以在此处获取有关 SDK 的详细信息:http://azure.github.io/azure-storage-java/。
下面是我使用
将文件上传到 Azure Blob Store 的代码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 Store 的文件的文件路径,它可以 return 我获取该文件的属性,特别是日期和上传时间。
Java中有支持这个的API吗?
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.
您要找的方法是downloadAttributes()
which returns an object of type will set blob's properties that are of type BlobProperties
BlobProperties
。它将包含有关 blob 的信息。您想要使用的方法是 getLastModified()
.
然而,这将 return 上次更新 blob 时的 date/time。因此,如果您创建了一个 blob 并且未对其进行任何更改,则此 属性 可用于查明它的上传时间。但是,如果您在创建 blob 后对其进行任何更改(如设置 properties/metadata 等),则值 returned 是上次更改时的 date/time。
如果您有兴趣了解 blob 的创建时间,您可能希望将此信息作为自定义元数据与 blob 一起存储。
您可以在此处获取有关 SDK 的详细信息:http://azure.github.io/azure-storage-java/。