我正在使用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")
回答此问题后,我将代码更改为以下内容
因此我将listBlobs与参数myCloudBlobContainer.listBlobs("aDirectory", true)
一起使用,并编写了以下代码以获取全部信息:
// 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
中根本没有使用...
答案 0 :(得分:0)
尝试使用以下listBlobs
方法的替代:
listBlobs(String prefix, boolean useFlatBlobListing)
因此您的代码应为:
Iterable<ListBlobItem> blobs = myCloudBlobContainer.listBlobs("aDirectory", true);
这将列出blob容器中“ aDirectory”虚拟文件夹内的所有blob。
答案 1 :(得分:0)
我必须说,先前的回答使我浪费时间;问题在于,只有一个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?
}
这帮助我找到了正确的方法: