2012-10-15 125 views
9

我需要從列表中進行比較,我需要的是azure的blob存儲中的文件,我需要的僅有一部分是獲取該blob中的文件。有沒有辦法從azure中獲取所有文件

例如

blob azureImages 
files: 
name 
something.jpg 
asd.jpg 
iwda.jpg 
my list: 
name 
something.jpg 
asd.jpg 

當我比較從我的列表中的BLOB文件,我想刪除在我的列表中沒有匹配的文件。

回答

20

你可以得到的斑點的列表中的容器與CloudBlobContainer.ListBlobs()CloudBlobDirectory.ListBlobs()

CloudBlobClient blobClient = new CloudBlobClient(blobEndpoint, new StorageCredentialsAccountAndKey(accountName, accountKey)); 

//Get a reference to the container. 
CloudBlobContainer container = blobClient.GetContainerReference("container"); 

//List blobs and directories in this container 
var blobs = container.ListBlobs(); 

foreach (var blobItem in blobs) 
{ 
    Console.WriteLine(blobItem.Uri); 
} 

目錄你需要從blobItem.Uri解析文件名裏,但你可以使用LINQ的除()方法找到區別:

public string FindFilesToDelete(IEnumerable<string> fromAzure, IEnumerable<string> yourList) 
{ 
    return fromAzure.Except(yourList); 
} 

這將返回fromAzure列表中不在yourList中的所有內容。

最後一點,你可以用this example

+0

刪除斑點我有這樣的結構,容器/資料夾/文件夾2/folder3/myfile.txt的。 Container.ListBlobs僅返回到folder1。有沒有辦法得到所有文件,說folder3? –

相關問題