gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 01:51:32 25 4
gpt4 key购买 nike

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

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

最佳答案

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

关于java - 使用代理获取 Azure Blob 存储值 Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55850944/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com