2017-06-02 24 views
0

我正在使用Java測試blob中存在的項目。我已經有了一個我期望出現在BLOB中的文件列表。以下是我的maven依賴項。如何使用Java從azure blob獲取特定文件的屬性?

<dependency> 
    <groupId>com.microsoft.azure</groupId> 
    <artifactId>azure-storage</artifactId> 
    <version>1.2.0</version> 
</dependency> 

我沒有得到適當的參考/代碼示例從blob獲取特定文件的屬性。唯一的辦法是列出所有文件並確定你需要的文件。這是非常昂貴的操作。

我指的是下面的文檔。

https://docs.microsoft.com/en-us/azure/storage/storage-java-how-to-use-blob-storage#download-a-blob

請幫我一個代碼示例,展示如何下載特定文件。

回答

0

好吧,我得到了一個方法,使其工作。這沒有直接的實現。您首先必須導航到該目錄並以確切名稱檢索文件。

/** 
* This method will help you retrieve properties of a file or a list of files from particular folder in Azure Blob storage 
* @param containerName - Pass the name of the container 
* @param path - Location of your file 
* @param fileName - You can pass complete file name to retrieve one file or you can pass prefix of the file. 
* @return It returns List<BlobProperties> if you pass complete file name, it will return one file or it will find file with supplied prefix. 
* @throws Exception 
*/ 

public List<BlobProperties> retriveBlobFilesProperties(String containerName, String path, String fileName) throws Exception{ 
    List<BlobProperties> blobFilesProperties = new ArrayList<BlobProperties>(); 
    CloudStorageAccount cloudStorageAccount = CloudStorageAccount.parse(this.storageConnectionString); 
    CloudBlobClient cloudBlobClient = cloudStorageAccount.createCloudBlobClient(); 
    CloudBlobContainer cloudBlobContainer = cloudBlobClient.getContainerReference(containerName); 
    CloudBlobDirectory directory = cloudBlobContainer.getDirectoryReference(path); 
    Iterable<ListBlobItem> blobItems = directory.listBlobs(fileName); 
    for(ListBlobItem item : blobItems){ 
     CloudBlob blob = (CloudBlob)item; 
     blobFilesProperties.add(blob.getProperties()); 
    } 
    return blobFilesProperties; 
} 

源:https://softwaretestingboard.com/qna/2197/how-to-download-particular-file-from-azure-blob-using-java

+0

上面的代碼將只能得到斑點的屬性。我以爲你想下載它們。 –

+0

不,我只是想獲得blob的屬性。謝謝你的指針。 – Mayur

+0

啊,我明白了。從問題標題我想你想下載blob,因此評論:)。 –

相關問題