0
votes

I am using below snippets of code to get the list of files from Azure Blob container:

    public static String[] listFolder( String containerURI, String sasToken, String dirPrefix) throws StorageException, URISyntaxException{
    ArrayList<String> files = new ArrayList<String>();      
    // Get a reference to a container 
    CloudBlobContainer container = (new CloudBlockBlob(new URI(containerURI + "?" + sasToken))).getContainer();
    CloudBlobDirectory directory = container.getDirectoryReference(dirPrefix);
    if (directory.listBlobs() != null) {
        for (ListBlobItem item : directory.listBlobs()) {
            CloudBlockBlob blob = (CloudBlockBlob)item;
            files.add(blob.getName());
        }
    }
    return files.toArray(new String[files.size()]);
}

But I am getting an exception

While executing [invoke] encountered [java.util.NoSuchElementException] : [An error occurred while enumerating the result, check the original exception for details. at
com.microsoft.azure.storage.core.LazySegmentedIterator.hasNext(LazySegmentedIterator.java:113)]

Does anyone have any pointer to what is wrong with the code, or does it need any modification?

1
Please edit your question and provide answers to a few things: 1) On which line of code you're getting the error? 2) Where are you running this code from? 3) What kind of storage account is it? (I've seen similar exceptions when dealing with a storage account on IOT Edge). 4) What version of Azure Storage SDK you're using? - Gaurav Mantri

1 Answers

0
votes

Here are the methods i used,

 private static List<String> listBlobs(CloudBlobContainer container) {
        List<ListBlobItem> blobs = new ArrayList<>();
        Iterable<ListBlobItem> items = container.listBlobs();
        for(ListBlobItem item : items) {
            if (! (item instanceof CloudBlobDirectory)) {
                blobs.add(item);
            }
        }
        return Lists.transform(blobs, new Function<ListBlobItem, String>() {
            @Override
            public String apply(ListBlobItem input) {
                String[] segs = input.getUri().getPath().split("/");
                return "<a href='" + input.getUri().toString() + "'>" + segs[segs.length - 1] + "</a>";
            }
        });
    }

    private static List<String> listBlobs(CloudBlobDirectory directory) throws Exception {
        List<ListBlobItem> blobs = new ArrayList<>();
        Iterable<ListBlobItem> items = directory.listBlobs();
        for(ListBlobItem item : items) {
            if (! (item instanceof CloudBlobDirectory)) {
                blobs.add(item);
            }
        }
        List<String> strings = Lists.transform(blobs, new Function<ListBlobItem, String>() {
            @Override
            public String apply(ListBlobItem input) {
                String[] segs = input.getUri().getPath().split("/");
                return "<a href='" + input.getUri().toString() + "'>" + segs[segs.length - 1] + "</a>";
            }
        });
        for(CloudBlobDirectory dir : listDirectories(directory)) {
            strings.addAll(listBlobs(dir));
        }
        return strings;
    }