【发布时间】:2012-10-05 19:21:54
【问题描述】:
我需要将我拥有的列表与 azure 的 blob 存储中的文件进行比较,我需要的唯一部分是获取该 blob 中文件列表的方法。
例如
blob azureImages files: name something.jpg asd.jpg iwda.jpg
my list:
name
something.jpg
asd.jpg
当我将 blob 中的文件与我的列表进行比较时,我想删除列表中不匹配的文件。
【问题讨论】:
我需要将我拥有的列表与 azure 的 blob 存储中的文件进行比较,我需要的唯一部分是获取该 blob 中文件列表的方法。
例如
blob azureImages files: name something.jpg asd.jpg iwda.jpg
my list:
name
something.jpg
asd.jpg
当我将 blob 中的文件与我的列表进行比较时,我想删除列表中不匹配的文件。
【问题讨论】:
您可以使用 CloudBlobContainer.ListBlobs() 获取容器中的 blob 列表,或者使用 CloudBlobDirectory.ListBlobs() 获取目录中的 blob 列表
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 的 except() 方法来查找差异:
public string FindFilesToDelete(IEnumerable<string> fromAzure, IEnumerable<string> yourList)
{
return fromAzure.Except(yourList);
}
这将返回 fromAzure 列表中不在 yourList 中的所有内容。
最后你可以使用this example删除blob
【讨论】:
GetContainerReference("container") 之后执行var blobs = blobContainer.GetDirectoryReference("folder1/folder2/folder3").ListBlobs(); 这将返回文件夹3 中的所有blob。