Im trying to upload an image through my backend to azure blob storage.
However the image size is 0 when viewing in Microsoft Azure Storage explorer
Uploading the image to my backend works as it should.
When saving the image locally. The image data has a proper size and gets displayed.
However, when uploading to Azure Blob Storage, the image has a size of 0 in Microsoft Azure Storage Explorer.
Im also using Azurite for simulating the environment.
@POST
@Path("upload-image")
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.APPLICATION_JSON)
public Uni<Response> uploadImage(@MultipartForm FormData formData) {
String uniqueFileName = "wearyimage-" + UUID.randomUUID() + "-" + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss")) + "-" + formData.getFileName();
Log.info("Uploading image: " + formData.getFileName() + " at " + LocalDateTime.now());
Log.info("Unique file name: " + uniqueFileName);
try {
Log.info("Actual file size: " + formData.file.available());
File localFile = new File("testFiles", uniqueFileName);
try (FileOutputStream out = new FileOutputStream(localFile)) {
BinaryData binaryData = BinaryData.fromStream(formData.getFile(), formData.getFileSize());
byte[] bytes = binaryData.toBytes();
out.write(bytes);
}
Log.info("File saved locally: " + localFile.getAbsolutePath());
} catch (IOException e) {
Log.error("Failed to save file locally", e);
}
Mono<BlockBlobItem> blockBlobItem = blobServiceAsyncClient.createBlobContainerIfNotExists("images")
.map(blobContainerAsyncClient -> blobContainerAsyncClient.getBlobAsyncClient(uniqueFileName))
.flatMap(blobAsyncClient -> {
InputStream inputStream = formData.getFile();
long fileSize = formData.getFileSize();
Log.info(BinaryData.fromStream(inputStream,fileSize).getLength());
return blobAsyncClient.upload(BinaryData.fromStream(inputStream, fileSize));
});
return Uni.createFrom().completionStage(blockBlobItem.toFuture()).map(it -> Response.status(CREATED).type(MediaType.APPLICATION_JSON).entity(Map.of("filename", uniqueFileName)).build());
}