我正在使用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
中迭代的是目录,没有任何预期的文件。因此,在日志中我只得到了以下 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
循环中完全没有执行...