【问题标题】:Is there a way to get all files from a blob of azure有没有办法从一团天蓝色中获取所有文件
【发布时间】: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 中的文件与我的列表进行比较时,我想删除列表中不匹配的文件。

【问题讨论】:

    标签: c# azure blob


    【解决方案1】:

    您可以使用 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

    【讨论】:

    • 我有这个结构container/folder1/folder2/folder3/myfile.txt。 Container.ListBlobs 仅返回至 folder1。有没有办法获取所有文件,比如在文件夹 3 中?
    • @AllenKing 在GetContainerReference("container") 之后执行var blobs = blobContainer.GetDirectoryReference("folder1/folder2/folder3").ListBlobs(); 这将返回文件夹3 中的所有blob。
    猜你喜欢
    • 2021-04-14
    • 1970-01-01
    • 1970-01-01
    • 2020-09-29
    • 2021-01-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多