java—如何直接从spring boot应用程序将文件上载到azure blob存储,而不将文件放入内存(缓冲内存)

dsekswqp  于 2021-06-30  发布在  Java
关注(0)|答案(1)|浏览(428)

我有一个spring-boot应用程序,它将zip文件(大小>250mb)上传到azure blob存储,如果文件没有直接上传到azure,那么它将使用内存空间,这可能导致在多个请求的情况下内存大小不足。所以,请给我一些参考或代码片段来解决我的问题。谢谢。

oprakyz7

oprakyz71#

如果您想直接上传文件到azure blob,请参考以下步骤
软件开发包

<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

代码

@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";
    }

相关问题