问题
I need get Image from Azure blob storage container using Proxy and save the Image to BufferedImage.
System.out.println("********Initiated******");
//Set Proxy Host name and Port
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("xx-xx-xxxxx", 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("images");
//call via this overload
Iterable<ListBlobItem> blobs = container.listBlobs(null, false, EnumSet.noneOf(BlobListingDetails.class), new BlobRequestOptions(), op);
URL urlOfImage = null;
//Listing contents of container
for(ListBlobItem blob: blobs) {
/*Process the Image. Sample URL from Azure: **https://someWebsite.blob.core.windows.net/images/00001.png***/
if(((CloudBlockBlob) blob).getName().toLowerCase().contains(".png")) {
urlOfImage = blob.getUri().toURL();
BufferedImage buffimage = ImageIO.read(urlOfImage);
}
}
System.out.println("********Success*********");
By using the URI, i can open the image via browser(seperatly).
Question: I want to process the blob content directly or via URI. If i run my above code when i save Image to buffered Image, I get the following Error.
Exception in thread "main" javax.imageio.IIOException: Can't get input stream from URL!
at javax.imageio.ImageIO.read(Unknown Source)
Thanks in advance.
回答1:
Per my experience, your issue was caused by the url of blob without SAS token which can not be accessed directly.
Here is my sample code to generate a blob url with SAS token.
String connectionString = "<your storage connection string>"
String containerName = "<your container name>";
String blobName = "<your blob name>";
CloudStorageAccount account = CloudStorageAccount.parse(connectionString);
CloudBlobClient client = account.createCloudBlobClient();
CloudBlobContainer container = client.getContainerReference(containerName);
CloudBlockBlob blob = container.getBlockBlobReference(blobName);
SharedAccessBlobPolicy policy = new SharedAccessBlobPolicy();
policy.setPermissions(EnumSet.allOf(SharedAccessBlobPermissions.class));
policy.setSharedAccessStartTime(Date.valueOf(LocalDate.now().minusYears(2)));
policy.setSharedAccessExpiryTime(Date.valueOf(LocalDate.now().plusYears(2)));
String sas = blob.generateSharedAccessSignature(policy, null);
String urlWithSas = String.format("%s?%s", blob.getUri(), sas);
Then, you can pass the urlWithSas value to the method ImageIO.read without proxy to get its BufferedImage object, as below.
URL urlOfImage = new URL(urlWithSas);
BufferedImage buffimage = ImageIO.read(urlOfImage );
System.out.println(buffimage.getHeight());
It works for me.
For using proxy, you just need to follow the JDK offical document Java Networking and Proxies to use System.setProperty method to enable networking with proxy for JVM at first.
System.setProperty("http.proxyHost", "<your proxy host>");
System.setProperty("http.proxyPort", "<your proxy port>");
Update:
The result of the code below is the same as the above.
HttpURLConnection conn = (HttpURLConnection) urlOfImage.openConnection();
conn.connect();
InputStream input = conn.getInputStream();
BufferedImage buffimage = ImageIO.read(input);
来源:https://stackoverflow.com/questions/55863696/get-image-from-azure-blob-using-proxy-in-java