How to upload file to azure blob storage directly from spring boot application, without taking file in to in-memory (Buffer memory)

Viewed 3213

I have spring boot application which will upload zip file (size > 250MB) to azure blob storage, if file is not directly uploaded to azure then it will use in-memory space which might lead to shortage of memory size in case of multiple requests. So, please give me some reference or code snippet to solve my problem. Thanks.

1 Answers

If you want to directly upload a file to Azure blob, please refer to the following steps

  1. SDK
<dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.3.1</version>
        </dependency>
<dependency>
            <groupId>com.azure</groupId>
            <artifactId>azure-storage-blob</artifactId>
            <version>12.6.0</version>
        </dependency
  1. Code
@PostMapping(path = "/upload")
    public String uploadFile(@RequestParam("file") MultipartFile file) throws IOException {
        String constr="";


        BlobContainerClient container = new BlobContainerClientBuilder()
                .connectionString(constr)
                .containerName("upload")
                .buildClient();


        BlobClient blob=container.getBlobClient(file.getOriginalFilename());

        blob.upload(file.getInputStream(),file.getSize(),true);


        return "ok";
    }

enter image description here

Related