Praveen Gopal
Praveen Gopal

Reputation: 539

Get Azure Blob Storage values Java using Proxy

I can connect to Azure Blob Storage using Proxy. Now i want to read all images from Azure blob storage.

            // ConnectionString
        String storageConnectionString =
                "DefaultEndpointsProtocol=https;" +
                "AccountName=xxxxxxx;" +
                "AccountKey=xxxxxxddfcfdcddrc==";

        //Authetication
        Authenticator.setDefault(new Authenticator() {
              protected PasswordAuthentication getPasswordAuthentication() {
                return new
                   PasswordAuthentication(proxyName,passowrd.toCharArray());
            }});

        //Set Proxy Host name and Port
        Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("xxxxxxxxx", 8080));
        OperationContext op = new OperationContext();
        op.setProxy(proxy);

        // Retrieve storage account from connection-string.
        CloudStorageAccount storageAccount = CloudStorageAccount.parse(storageConnectionString);

        // Create the blob client.
       CloudBlobClient blobClient = storageAccount.createCloudBlobClient();

       // Get a reference to a container.
       // The container name must be lower case
       CloudBlobContainer container = blobClient.getContainerReference("test");

       // Create the container if it does not exist with public access.
       System.out.println("Creating container: " + container.getName());


       // Create the container if it does not exist.
       //container.createIfNotExists(BlobContainerPublicAccessType.CONTAINER, new BlobRequestOptions(), op);

       // Delete the blob.
       //container.deleteIfExists(null, null, op);
        LinkedList<String> blobNames = new LinkedList<>();
        Iterable<ListBlobItem> blobs = container.listBlobs();
        blobNames = new LinkedList<>();

       **// the line that hit an error**
        for(ListBlobItem blob: blobs) { 
            blobNames.add(((CloudBlockBlob) blob).getName());
        }

        System.out.println(blobNames.size());

        System.out.println("********Success*********");

When i run above script i got following problem:

java.util.NoSuchElementException: An error occurred while enumerating the result, check the original exception for details.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)
at com.microsoft.azure.storage.StorageException: An unknown failure occurred : Connection refused: connect
at com.microsoft.azure.storage.StorageException.translateException(StorageException.java:66)
at com.microsoft.azure.storage.core.ExecutionEngine.executeWithRetry(ExecutionEngine.java:209)
at com.microsoft.azure.storage.core.LazySegmentedIterator.hasNext(LazySegmentedIterator.java:109)
... 1 moreCaused by: java.net.ConnectException: Connection refused: connect

I dono why this error occurs but it throws above exception and Connection refused.

Upvotes: 1

Views: 2100

Answers (2)

Dima
Dima

Reputation: 2909

You need to pass your OperationContext to the container.listBlobs() call via this overload:

public Iterable<ListBlobItem> listBlobs(final String prefix, final boolean useFlatBlobListing, final EnumSet<BlobListingDetails> listingDetails, BlobRequestOptions options, OperationContext opContext)

In your case that would mean

Iterable<ListBlobItem> blobs = container.listBlobs(null, false, EnumSet.noneOf(BlobListingDetails.class), null, op);

Upvotes: 2

Bsquare ℬℬ
Bsquare ℬℬ

Reputation: 4487

In your for each loop, you should use blobs and not container.listBlobs() again.

And you may check if you have elements in the received iterable.

Anyway, it would be easier to answer with the complete stack, and line numbers of your source code.

Upvotes: 0

Related Questions