A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://stackoverflow.com/questions/79132616/how-to-upload-file-to-folder-using-blockblobclient below:

azure - 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.


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4