【问题标题】:Get Image from Azure Blob using Proxy In Java使用 Java 中的代理从 Azure Blob 获取图像
【发布时间】:2019-09-15 17:40:57
【问题描述】:

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

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

问题:我想直接或通过 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】:

    根据我的经验,您的问题是由无法直接访问的不带 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官方文档Java Networking and Proxies使用System.setProperty方法为JVM启用代理网络。

    System.setProperty("http.proxyHost", "<your proxy host>");
    System.setProperty("http.proxyPort", "<your proxy port>");
    

    更新:

    下面代码的结果和上面一样。

    HttpURLConnection conn = (HttpURLConnection) urlOfImage.openConnection();
    conn.connect();
    InputStream input = conn.getInputStream();
    BufferedImage buffimage = ImageIO.read(input);
    

    【讨论】:

    • 我可以得到 URI。使用诸如“URLConnection conn = urlOfImage.openConnection(proxy);”之类的代理通过 openconnection 获取图像是一种好习惯吗
    • @PraveenGopal 与良好做法相同。由你决定。我使用openConnection发布相关代码,您可以参考。
    • 使用代理:代理 proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(HostName, 8080)); HttpURLConnection conn = (HttpURLConnection) urlOfImage.openConnection(proxy);
    • 作为信息:如果容器访问不是公共级别,请使用 SharedAccessBlobPolicy
    猜你喜欢
    • 2019-09-14
    • 2022-11-06
    • 1970-01-01
    • 1970-01-01
    • 2019-08-05
    • 2013-04-26
    • 2019-04-06
    • 2021-09-17
    • 1970-01-01
    相关资源
    最近更新 更多