下面是我的代码使用文件属性
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