使用Proxy获取Azure Blob存储值Java

问题描述 投票:1回答:2

我可以使用Proxy连接到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

我不知道为什么会出现这个错误,但它会抛出异常并且Connection被拒绝。

java azure blob
2个回答
2
投票

您需要通过此重载将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);

0
投票

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

您可以检查收到的可迭代中是否包含元素。

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

© www.soinside.com 2019 - 2024. All rights reserved.