简体   繁体   中英

Error occured while list the files from azure blob container

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?

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;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM