下面是我的代碼使用文件屬性
com.microsoft.azure.storage
庫文件上傳到在Azure Blob存儲
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支持?
這是有益的,謝謝。我對上傳時間很感興趣,所以元數據並不是真正需要的,但是要感謝這些信息。小修正:downloadAttributes()的返回類型是void,它不會顯式返回BlobProperties類型的對象,儘管在內部情況可能如此。所以你做blob.downloadAttributes();其次是System.out.println(blob.getProperties()。getLastModified()); – saltmangotree
謝謝你指出我的錯誤。我糾正了我的答案。 –
在我們完成之前還有一個問題 - 我能夠獲得文件DirA/DirB/file.csv的屬性,但無法獲得類似的目錄屬性,比如DirA或DirB。目錄是不是blob?我怎樣才能獲得類似的目錄屬性? – saltmangotree