使用 client.listContainers()和 container.listBlobs()函数并获得低于JAVA异常的问题。尝试使用参考方法单独访问它们,它工作正常。不确定为什么会这样,因为没有访问限制,即公共访问和获取客户端引用而没有任何连接字符串问题。
例外:
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.company.test.core.handler.AzureHeirarchyGenerator.getAzureFileList(AzureHeirarchyGenerator.java:69)
at com.company.test.core.handler.AzureHeirarchyGenerator.main(AzureHeirarchyGenerator.java:18)
Caused by: 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)
... 2 more
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:85)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at sun.security.ssl.SSLSocketImpl.connect(SSLSocketImpl.java:673)
at sun.net.NetworkClient.doConnect(NetworkClient.java:175)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:463)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:558)
at sun.net.www.protocol.https.HttpsClient.<init>(HttpsClient.java:264)
at sun.net.www.protocol.https.HttpsClient.New(HttpsClient.java:367)
at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.getNewHttpClient(AbstractDelegateHttpsURLConnection.java:191)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect0(HttpURLConnection.java:1156)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:1050)
at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:177)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1564)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1492)
at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:480)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java:347)
at com.microsoft.azure.storage.core.ExecutionEngine.executeWithRetry(ExecutionEngine.java:115)
... 3 more
代码段JAVA:
CloudStorageAccount account = CloudStorageAccount.parse(azureConnectionString);
CloudBlobClient client = account.createCloudBlobClient();
// List all containers of a storage account
Iterable<CloudBlobContainer> containers = client.listContainers();
for (CloudBlobContainer cloudBlobContainer : containers) {
Iterable<ListBlobItem> blobs = cloudBlobContainer.lisBlobs();
System.out.println("Code to fetch blobs inside container");
}
答案 0 :(得分:0)
我已经测试了您的代码,它在我的环境中运行良好,如下所示:
以下是我的完整代码供您参考:
import java.net.URISyntaxException;
import java.security.InvalidKeyException;
import com.microsoft.azure.storage.CloudStorageAccount;
import com.microsoft.azure.storage.blob.CloudBlobClient;
import com.microsoft.azure.storage.blob.CloudBlobContainer;
import com.microsoft.azure.storage.blob.ListBlobItem;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
CloudStorageAccount account;
try {
account = CloudStorageAccount.parse("DefaultEndpointsProtocol=https;AccountName=leeliublob;AccountKey=O7xxxxxxxxxxxxxxx/x/e9l9FhLqayXcbz1R+E0mIcJ5Wjkly1DsQPYY5dF2JrAVHtBozbJo29ZrrGJA==;EndpointSuffix=core.windows.net");
CloudBlobClient client = account.createCloudBlobClient();
Iterable<CloudBlobContainer> containers = client.listContainers();
for (CloudBlobContainer cloudBlobContainer : containers) {
Iterable<ListBlobItem> blobs = cloudBlobContainer.listBlobs();
System.out.println("BlobContainer : "+ cloudBlobContainer.getName());
System.out.println(" blobs inside container '"+cloudBlobContainer.getName()+"' are: ");
int x=1;
for (ListBlobItem blobItem : cloudBlobContainer.listBlobs()) {
String blobName = blobItem.getUri().toString();
System.out.println(" Number "+x+": url="+ blobItem.getUri().toString());
x++;
}
}
} catch (InvalidKeyException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (URISyntaxException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
我想可能有两个原因会导致您的错误:
请确保连接字符串正确无误。
请确保使用最新的Java SDK 以及我使用的Java SDK:azure-storage-java