【问题标题】:List all files in (sub)directories in Azure列出 Azure 中(子)目录中的所有文件
【发布时间】:2019-04-04 21:55:57
【问题描述】:

我正在使用 Java 开发一个 azure 函数。我需要迭代以下文件夹中的所有文件

aDirectory/aSubdirectoryWithManyFiles/

该路径中有很多文件,:

aDirectory/aSubdirectoryWithManyFiles/file1
aDirectory/aSubdirectoryWithManyFiles/file2
aDirectory/aSubdirectoryWithManyFiles/file3
aDirectory/aSubdirectoryWithManyFiles/file4
aDirectory/aSubdirectoryWithManyFiles/file5

所以我写了以下代码来获取它们:

// myCloudBlobContainer is a CloudBlobContainer
// I expected to get all files thanks to the next row
Iterable<ListBlobItem> blobs = myCloudBlobContainer.listBlobs();
// The only blob found in the container is the directory itself
for (ListBlobItem blob : blobs) {
    //log the current blob URI
    if (blob instanceof CloudBlob) {  // this never happens
        CloudBlob cloudBlob = (CloudBlob) blob;
        //make nice things with every found file
    }
}

for 中迭代的唯一 blob 是目录,没有预期的文件。所以在日志中我只得到以下 URI:

https://blablablabla.blob.core.windows.net/aDirectory/aSubdirectoryWithManyFiles/

我应该怎么做才能访问每个文件?

如果我有多个子目录,如下例所示?

aDirectory/aSubdirectoryWithManyFiles/files(1-5)
aDirectory/anotherSubdirectoryWithManyFiles/files(6-10)

提前致谢


编辑

为了使方法可测试,项目使用包装器和接口,而不是直接使用 CloudBlobContainer;基本上,CloudBlobContainer 由CloudBlobClient.getContainerReference("containername") 给出

在回答完这个问题后,我将代码更改为以下 所以我使用了带有参数myCloudBlobContainer.listBlobs("aDirectory", true) 的listBlobs,我编写了以下代码来获取它们:

// myCloudBlobClient is a CloudBlobClient
CloudBlobContainer myCloudBlobContainer = myCloudBlobClient.getContainerReference("containername")
// I expected to get all files thanks to the next row
Iterable<ListBlobItem> blobs = myCloudBlobContainer.listBlobs("aDirectory", true); // HERE THE CHANGE
// No blob found this time
for (ListBlobItem blob : blobs) { // NEVER IN THE FOR
    //log the current blob URI
    if (blob instanceof CloudBlob) {
        CloudBlob cloudBlob = (CloudBlob) blob;
        //make nice things with every found file
    }
}

但是这一次,for 里根本就不行了...

【问题讨论】:

    标签: java azure azure-blob-storage


    【解决方案1】:

    尝试使用listBlobs 方法的以下覆盖:

    listBlobs(String prefix, boolean useFlatBlobListing)
    

    所以你的代码是:

    Iterable<ListBlobItem> blobs = myCloudBlobContainer.listBlobs("aDirectory", true);
    

    这将列出 Blob 容器中“aDirectory”虚拟文件夹中的所有 Blob。

    【讨论】:

    • 您可以使用 Gaurav 建议的方法,但我建议使用 listBlobsSegmented(String prefix)。此方法将有助于避免从 Blob 存储返回的结果限制。
    • @Harish...listBlobs 方法在内部处理延续令牌,而在使用 listBlobsSegmented 时您需要在代码中处理延续令牌。我的偏好也是在 listBlobs 上使用此方法。在这种特殊情况下,我建议使用azure.github.io/azure-sdk-for-java/com/microsoft/azure/storage/…
    • 如果我理解得很好,你们俩都在listBlobsSegmented,对吧?
    • “dir”是您的子目录的名称吗?
    • container.listBlobs("aDirectory", true) 也没有看到文件 --- 抱歉 Gaurav,我删除了我的评论,因为我无法编辑,然后才注意到你写了一些东西。 aDirectory 是包含文件子目录的目录
    【解决方案2】:

    不得不说,之前的回答让我浪费了时间;问题在于只有一个for 不足以在文件夹中查找文件。第一个for 查找文件夹和子文件夹,以及(也许我没有检查)“根”(我们这样称呼它)中的文件。

    拥有文件夹,我们必须将它们中的每一个都转换为 CloudBlobDirectory 以便查看和迭代所有包含的文件与另一个 for

    这里适合我的解决方案:

    // myCloudBlobClient is a CloudBlobClient
    CloudBlobContainer myCloudBlobContainer = myCloudBlobClient.getContainerReference("containername")
    // I expected to get all files thanks to the next row
    Iterable<ListBlobItem> blobs = myCloudBlobContainer.listBlobs();
    // only directories here, another for needed to scan files
    for (ListBlobItem blob : blobs) {
        if (blob instanceof CloudBlobDirectory) {
            CloudBlobDirectory directory = (CloudBlobDirectory)blob;
            //next is in try/catch
            Iterable<ListBlobItem> fileBlobs = directory.listBlobs();
            for (ListBlobItem fileBlob : fileBlobs) {
                if (fileBlob instanceof CloudBlob) {
                CloudBlob cloudBlob = (CloudBlob) fileBlob;
                //make nice things with every found file
                }
            }
        } // else: may be we found a cloudBlob in root?
    }
    

    这帮助我找到了正确的方法:

    https://social.msdn.microsoft.com/Forums/en-US/1cfdc91f-e588-4839-a878-9650339a0a06/list-all-blobs-in-c?forum=windowsazuredata

    【讨论】:

    • 我很惊讶它在 Azure 存储中如此复杂。有没有更简单的方法,比如在 GCS 中,可以传递文件夹前缀?
    猜你喜欢
    • 2012-11-14
    • 1970-01-01
    • 2011-03-01
    • 2014-03-08
    • 2012-09-02
    • 2017-05-01
    • 1970-01-01
    相关资源
    最近更新 更多