0

I am using azure-storage-blob Java SDK version 12.8.0 with SpringBoot 2.x, and I would like to do multipart-uploads from local storage to Azure blob container, it worked well when specifying the filename only in the code.

Here's the sample code

String folder = "20241024T180025";
String filename = "corey.jpg";
String folderandfilename = folder + "/" + filename;
 
BlobContainerClient containerClient = new BlobContainerClientBuilder()
                    .connectionString(connectionString)
                    .containerName(containerName)
                    .buildClient();

BlockBlobClient blobClient = containerClient.getBlobClient(filename).getBlockBlobClient();
List<String> blockIds = new ArrayList<>();
try (BufferedInputStream inputStream = new BufferedInputStream(new FileInputStream(file))) {
    byte[] buffer = new byte[(int) chunkSize];
    int bytesRead;
    int blockNumber = 0;
            
    while ((bytesRead = inputStream.read(buffer)) != -1) {
        String blockId = Base64.getEncoder().encodeToString(String.format("block-%07d", blockNumber).getBytes(StandardCharsets.UTF_8));
        byte[] uploadBuffer;
        if (bytesRead < buffer.length) {
            uploadBuffer = Arrays.copyOf(buffer, bytesRead);
        } else {
            uploadBuffer = buffer;
        }
                        
        blockIds.add(blockId);
        blobClient.stageBlock(blockId, new ByteArrayInputStream(uploadBuffer), bytesRead);
        blockNumber++;
    }
    blobClient.commitBlockList(blockIds);
}

I also need to create a folder for storing the uploaded file so I assigned the String folderandfilename to it.

BlockBlobClient blobClient = containerClient.getBlobClient(folderandfilename).getBlockBlobClient();

But it failed with error message:

Status code 400, "InvalidBlobOrBlockThe specified blob or block content is invalid.\n

It seems like BlockBlobClient accepts the filename like "corey.jpg" but doesn't support something like "20241024T180025/corey.jpg", is there anything I can do to fix the issue and achieve my goal?

I would appreciate any ideas, thanks.

3
  • Are you trying add large file Using java? Commented Oct 28, 2024 at 8:54
  • @Venkatesan yes, it is, with springboot 2.x Commented Oct 28, 2024 at 9:02
  • Check the below answer. Commented Oct 28, 2024 at 9:05

1 Answer 1

1

How to upload file to folder using BlockBlobClient?

In Azure Blob storage the folders are virtual directories. so,"20241024T180025/corey.jpg" should be treated as a single blob name, not an actual directory structure.

You can use the below code to upload file to folder using BlockBlobClient using Azure java SDK.

Code:

String connectionString = "xxxxx";
        String containerName = "venkat";
        String folder = "20241024T180025";
        String filename = "corey.jpg";
        String folderandfilename = folder + "/" + filename;
        
        long chunkSize = 4 * 1024 * 1024; // 4MB chunk size, adjust if needed
        File file = new File("C:\image stamp.jpg");

        BlobContainerClient containerClient = new BlobContainerClientBuilder()
                .connectionString(connectionString)
                .containerName(containerName)
                .buildClient();
        BlockBlobClient blobClient = containerClient.getBlobClient(folderandfilename).getBlockBlobClient();
       
        List<String> blockIds = new ArrayList<>();

        try (BufferedInputStream inputStream = new BufferedInputStream(new FileInputStream(file))) {
            byte[] buffer = new byte[(int) chunkSize];
            int bytesRead;
            int blockNumber = 0;

            while ((bytesRead = inputStream.read(buffer)) != -1) {
                String blockId = Base64.getEncoder().encodeToString(String.format("block-%07d", blockNumber).getBytes(StandardCharsets.UTF_8));
            
                byte[] uploadBuffer;
                if (bytesRead < buffer.length) {
                    uploadBuffer = Arrays.copyOf(buffer, bytesRead);
                } else {
                    uploadBuffer = buffer;
                }

                blockIds.add(blockId);
                blobClient.stageBlock(blockId, new ByteArrayInputStream(uploadBuffer), bytesRead);
                blockNumber++;
            }
            blobClient.commitBlockList(blockIds);
            System.out.println("File uploaded successfully as " + folderandfilename);

        } catch (IOException e) {
            e.printStackTrace();
        }

Output:

File uploaded successfully as 20241024T180025/corey.jpg

enter image description here Portal: enter image description here

Sign up to request clarification or add additional context in comments.

8 Comments

Thanks for the quick reply and the clear explanation, but this is what I have tried before, the exact same code BlockBlobClient blobClient = containerClient.getBlobClient(folderandfilename).getBlockBlobClient();, I got this error message "InvalidBlobOrBlock".
Have you tried with my exact code?
I can't find any difference between yours and mine.
I assume there was something wrong in my test environment, it backed to work now.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.