azureazure-blob-storageazure-java-sdk

How to upload file to folder using BlockBlobClient


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.


Solution

  • 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