[英]Get Azure Blob Storage values Java using 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
我不知道為什么會發生此錯誤,但它引發了以上異常並且拒絕了連接。
您需要通過以下重載將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);
在for each
循環中,應再次使用blobs
而不是container.listBlobs()
。
然后,您可以檢查所接收的Iterable中是否包含元素。
無論如何,用完整的堆棧和源代碼的行號來回答將更容易。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.