我可以使用代理连接到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
我不知道为什么会发生此错误,但是它引发了异常并且连接被拒绝。
答案 0 :(得分: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);
答案 1 :(得分:0)
在您的for each
循环中,您应该使用blobs
而不是container.listBlobs()
。
然后您可以检查所接收的可迭代对象中是否有元素。
无论如何,使用完整的堆栈以及源代码的行号将更容易回答。