java - Azure Storage blob getting the io.netty.channel.StacklessClosedChannelException while generating downloading the from Sas

admin2025-04-21  3

I am using the below code to generating the ByteStream from the SasUrl of Azure storage

 ConnectionProvider connectionProvider = 
 ConnectionProvider.builder("custom-connection-provider")
                  .maxConnections(200)
                  .pendingAcquireMaxCount(5000)
                  .build();

          BlobClient blobClient = new BlobClientBuilder()
                  .endpoint(searchMultipleWrapper.getSasUrl().trim()) // IT IS MY VALID SAS URL
                  .httpClient(new NettyAsyncHttpClientBuilder()
                          .connectionProvider(connectionProvider)
                          .readTimeout(Duration.ofMinutes(10))
                          .writeTimeout(Duration.ofMinutes(10))
                          .responseTimeout(Duration.ofMinutes(10))
                          .build())
                  .retryOptions(new RequestRetryOptions(
                          RetryPolicyType.EXPONENTIAL,
                          5,
                          2,
                          null,
                          null,
                          null
                  ))
                  .buildClient();

          log.info("BlobClient Object check {} ", blobClient);



          // Use try-with-resources to ensure InputStream is closed
          try (InputStream inputStream = blobClient.openInputStream();
               ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream()) {

              // Transfer data from InputStream to ByteArrayOutputStream
              inputStream.transferTo(byteArrayOutputStream);

              // Convert to Base64
              byte[] imageBytes = byteArrayOutputStream.toByteArray();
              String base64Image = Base64.getEncoder().encodeToString(imageBytes);

              log.info("Base 64 : {} ", base64Image);

             
              // Log the Base64 string (truncated for safety)
              log.info("Base64 Image (truncated): " + base64Image.substring(0, Math.min(base64Image.length(), 100)) + "...");
          } catch (Exception e) {
              log.error("Error while processing blob content", e);

          }

Exception which i am getting as below :-

stack_trace":"reactor.core.Exceptions$ReactiveException: ioty.channel.StacklessClosedChannelException\n\tat

Heading ##reactor.core.Exceptions.propagate(Exceptions.java:410)\n\tat

reactor.core.publisher.BlockingSingleSubscriber.blockingGet(BlockingSingleSubscriber.java:102)\n\tat reactor.core.publisher.Mono.block(Mono.java:1779)\n\tat com.azure.storage.blob.specialized.BlobClientBase.openInputStream(BlobClientBase.java:393)\n\tat com.azure.storage.blob.specialized.BlobClientBase.openInputStream(BlobClientBase.java:323)\n\tat com.azure.storage.blob.specialized.BlobClientBase.openInputStream(BlobClientBase.java:312)\n\tat com.azure.storage.blob.specialized.BlobClientBase.openInputStream(BlobClientBase.java:299)\n

I think it is the versioning issue which has the bug on the Window system. Please check the reference of this one Click here

Please help me to short out from this problem

I am using the below code to generating the ByteStream from the SasUrl of Azure storage

 ConnectionProvider connectionProvider = 
 ConnectionProvider.builder("custom-connection-provider")
                  .maxConnections(200)
                  .pendingAcquireMaxCount(5000)
                  .build();

          BlobClient blobClient = new BlobClientBuilder()
                  .endpoint(searchMultipleWrapper.getSasUrl().trim()) // IT IS MY VALID SAS URL
                  .httpClient(new NettyAsyncHttpClientBuilder()
                          .connectionProvider(connectionProvider)
                          .readTimeout(Duration.ofMinutes(10))
                          .writeTimeout(Duration.ofMinutes(10))
                          .responseTimeout(Duration.ofMinutes(10))
                          .build())
                  .retryOptions(new RequestRetryOptions(
                          RetryPolicyType.EXPONENTIAL,
                          5,
                          2,
                          null,
                          null,
                          null
                  ))
                  .buildClient();

          log.info("BlobClient Object check {} ", blobClient);



          // Use try-with-resources to ensure InputStream is closed
          try (InputStream inputStream = blobClient.openInputStream();
               ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream()) {

              // Transfer data from InputStream to ByteArrayOutputStream
              inputStream.transferTo(byteArrayOutputStream);

              // Convert to Base64
              byte[] imageBytes = byteArrayOutputStream.toByteArray();
              String base64Image = Base64.getEncoder().encodeToString(imageBytes);

              log.info("Base 64 : {} ", base64Image);

             
              // Log the Base64 string (truncated for safety)
              log.info("Base64 Image (truncated): " + base64Image.substring(0, Math.min(base64Image.length(), 100)) + "...");
          } catch (Exception e) {
              log.error("Error while processing blob content", e);

          }

Exception which i am getting as below :-

stack_trace":"reactor.core.Exceptions$ReactiveException: io.netty.channel.StacklessClosedChannelException\n\tat

Heading ##reactor.core.Exceptions.propagate(Exceptions.java:410)\n\tat

reactor.core.publisher.BlockingSingleSubscriber.blockingGet(BlockingSingleSubscriber.java:102)\n\tat reactor.core.publisher.Mono.block(Mono.java:1779)\n\tat com.azure.storage.blob.specialized.BlobClientBase.openInputStream(BlobClientBase.java:393)\n\tat com.azure.storage.blob.specialized.BlobClientBase.openInputStream(BlobClientBase.java:323)\n\tat com.azure.storage.blob.specialized.BlobClientBase.openInputStream(BlobClientBase.java:312)\n\tat com.azure.storage.blob.specialized.BlobClientBase.openInputStream(BlobClientBase.java:299)\n

I think it is the versioning issue which has the bug on the Window system. Please check the reference of this one Click here

Please help me to short out from this problem

Share Improve this question edited Jan 27 at 4:50 Ravindra Kushwaha asked Jan 23 at 14:45 Ravindra KushwahaRavindra Kushwaha 8,06214 gold badges59 silver badges110 bronze badges 9
  • Can you share your azure sas token format? – Venkatesan Commented Jan 24 at 2:55
  • Check the below answer. – Venkatesan Commented Jan 24 at 4:01
  • Please check the sasurl format myserver.blob.core.windows.net/mycontainer/USID/172952/… Same sasurl working with azcopy command but getting issue with java code... I just need the image/file bytestrem is it possible not saving to my disk area – Ravindra Kushwaha Commented Jan 24 at 4:59
  • How you are getting SAS url? – Venkatesan Commented Jan 24 at 5:06
  • Also, I seen in your sas url spr=https is misssing. – Venkatesan Commented Jan 24 at 5:15
 |  Show 4 more comments

2 Answers 2

Reset to default 1

generating the ByteStream from the SasUrl of Azure storage

You can use the below code with proper version and with sas url to generate the ByteStream using Azure Java SDK.

Code:

import com.azure.core.http.netty.NettyAsyncHttpClientBuilder;
import com.azure.storage.blob.BlobClient;
import com.azure.storage.blob.BlobClientBuilder;

import com.azure.storage.common.policy.RequestRetryOptions;
import com.azure.storage.common.policy.RetryPolicyType;
import reactor.netty.resources.ConnectionProvider;

import java.io.ByteArrayOutputStream;
import java.time.Duration;
import java.util.Base64;  

public class App {
     public static void main(String[] args) {
        // Example SAS URL (replace with your actual SAS URL)
        String sasUrl = "https://<Account name>.blob.core.windows.net/<Conatiner name>/<blob name>?sp=r&st=2025-01-24T03:07:42Z&se=2025-01-24T11:07:42Z&spr=https&sv=2022-11-02&sr=b&sig=redacted";

        ConnectionProvider connectionProvider = ConnectionProvider.builder("custom-connection-provider")
                .maxConnections(200) // Adjust as per your concurrency needs
                .pendingAcquireMaxCount(5000)
                .build();

        BlobClient blobClient = new BlobClientBuilder()
                .endpoint(sasUrl.trim())
                .retryOptions(new RequestRetryOptions(
                        RetryPolicyType.EXPONENTIAL,
                        5,
                        2,
                        null,
                        null,
                        null
                ))
                .httpClient(new NettyAsyncHttpClientBuilder()
                .connectionProvider(connectionProvider)
                .readTimeout(Duration.ofMinutes(10))
                .writeTimeout(Duration.ofMinutes(10))
                .responseTimeout(Duration.ofMinutes(10))
                .build())
                .buildClient();

        System.out.println("BlobClient created successfully");

        // Read blob content as a Base64-encoded string
        try (ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream()) {

            // Download blob content into the output stream
                        blobClient.downloadStream(byteArrayOutputStream);

            // Convert the downloaded bytes to Base64
            byte[] imageBytes = byteArrayOutputStream.toByteArray();
            String base64Image = Base64.getEncoder().encodeToString(imageBytes);

            // Log the Base64 string (truncated for safety)
            System.out.println("Base64 Image (truncated): " +
                    base64Image.substring(0, Math.min(base64Image.length(), 100)) + "...");

        } catch (Exception e) {
            System.err.println("Error while processing blob content");
            e.printStackTrace();
        }
    }
}

My versions are azure-storage-blob is 12.29.0 and azure-core-http-netty is 1.15.7.

Output:

BlobClient created successfully
Base64 Image (truncated): /9j/4AAQSkZJRgABAQEAYABgAAD/2wBDAAIBAQIBAQICAgICAxxxxxxxxxxxxxxHBwcGBwcICQsJCAgKCAcHCg0KCgsM...

Reference: Download a blob with Java - Azure Storage | Microsoft Learn

As i have mentioned in the question that it might be an open issue from the dependency side

I have used the below dependency which works perfectly previously i was using the azure-storage-blob artifactId which was casing the above-mentioned issue.

        <dependency>
            <groupId>com.azure</groupId>
            <artifactId>azure-core-http-okhttp</artifactId>
            <version>1.12.0</version>
        </dependency>
转载请注明原文地址:http://anycun.com/QandA/1745193491a90409.html