【问题标题】:Get Azure Blob Storage values Java using Proxy使用代理获取 Azure Blob 存储值 Java
【发布时间】:2019-09-14 23:37:02
【问题描述】:

我可以使用代理连接到 Azure Blob 存储。现在我想从 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*********");

当我运行上面的脚本时,我遇到了以下问题:

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

我不知道为什么会发生此错误,但它会抛出异常并且连接被拒绝。

【问题讨论】:

    标签: java azure blob


    【解决方案1】:

    您需要通过此重载将 OperationContext 传递给 container.listBlobs() 调用:

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

    在你的情况下,这意味着

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

    【讨论】:

    • 你节省了我的时间:)
    【解决方案2】:

    在您的for each 循环中,您应该使用blobs 而不是再次使用container.listBlobs()

    您可以检查接收到的可迭代对象中是否有元素。

    无论如何,用完整的堆栈和源代码的行号来回答会更容易。

    【讨论】:

    • 好的,谢谢;然后我同意@Dima 回答谁更快;)
    猜你喜欢
    • 2019-09-15
    • 2018-05-07
    • 1970-01-01
    • 2015-11-04
    • 2018-10-14
    • 2017-07-29
    • 1970-01-01
    • 2020-12-09
    • 1970-01-01
    相关资源
    最近更新 更多