使用Java中的代理从Azure Blob获取映像

问题描述 投票:1回答:1

我需要使用Proxy从Azure blob存储容器中获取Image并将Image保存到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*********");

通过使用URL,我可以通过浏览器(单独)打开图像。

问题:我想直接或通过URI处理blob内容。如果我将图像保存到缓冲图像时运行上面的代码,我会收到以下错误。

Exception in thread "main" javax.imageio.IIOException: Can't get input stream from URL!
at javax.imageio.ImageIO.read(Unknown Source)

提前致谢。

java azure blob
1个回答
1
投票

根据我的经验,您的问题是由没有SAS令牌的blob的url引起的,无法直接访问。

这是我用SAS令牌生成blob url的示例代码。

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);

然后,您可以将urlWithSas值传递给方法ImageIO.read而不使用代理来获取其BufferedImage对象,如下所示。

URL urlOfImage = new URL(urlWithSas);
BufferedImage buffimage = ImageIO.read(urlOfImage );
System.out.println(buffimage.getHeight());

这个对我有用。

对于使用代理,您只需要遵循JDK官方文档qazxsw poi,首先使用qazxsw poi方法启用与JVM代理的联网。

Java Networking and Proxies

更新:

以下代码的结果与上述相同。

System.setProperty
© www.soinside.com 2019 - 2024. All rights reserved.