简体   繁体   English

使用代理获取Azure Blob存储值Java

[英]Get Azure Blob Storage values Java using Proxy

I can connect to Azure Blob Storage using Proxy. 我可以使用代理连接到Azure Blob存储。 Now i want to read all images from Azure blob storage. 现在,我想从Azure blob存储读取所有图像。

            // 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. 我不知道为什么会发生此错误,但它引发了以上异常并且拒绝了连接。

You need to pass your OperationContext to the container.listBlobs() call via this overload: 您需要通过以下重载将OperationContext传递给container.listBlobs()调用:

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

In your for each loop, you should use blobs and not container.listBlobs() again. for each循环中,应再次使用blobs而不是container.listBlobs()

And you may check if you have elements in the received iterable. 然后,您可以检查所接收的Iterable中是否包含元素。

Anyway, it would be easier to answer with the complete stack, and line numbers of your source code. 无论如何,用完整的堆栈和源代码的行号来回答将更容易。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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