【问题标题】:How to upload file to azure blob storage directly from spring boot application, without taking file in to in-memory (Buffer memory)如何直接从 Spring Boot 应用程序将文件上传到 Azure Blob 存储,而不将文件放入内存(缓冲内存)
【发布时间】:2020-12-08 12:16:15
【问题描述】:

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

【问题讨论】:

    标签: java azure spring-boot azure-blob-storage


    【解决方案1】:

    如果要直接将文件上传到 Azure blob,请参考以下步骤

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

    【讨论】:

    • 谢谢,但这段代码会等到 mutilpartfile 收到完整文件(将文件放入本地内存),我只想将文件直接流式传输到 azure。
    • @brijeshPatil 该方法会分块读取和上传。它不会在内存中加载所有缓冲区然后上传到 Azure。更多详情请参考i.stack.imgur.com/q9bLt.pngi.stack.imgur.com/2KlUS.png
    • 好的,感谢您的帮助,非常感谢
    • @brijeshPatil 既然对你有帮助,请accept it as an answer好吗?它可能会帮助有类似问题的人。
    • 你确定它不会将文件加载到缓冲内存中吗,因为当我测试它时,它显示它使用 5MB 用于 5MB 文件,1MB 用于 1MB 文件。我在请求方法开始和方法结束时使用this code 计算内存
    猜你喜欢
    • 2020-10-14
    • 2021-04-24
    • 2013-03-18
    • 2017-01-24
    • 2017-08-19
    • 2019-08-13
    • 2021-03-07
    • 2020-06-01
    相关资源
    最近更新 更多