簡體   English   中英

如何獲取Java中FileInputStream中的azure blob存儲內容?

[英]How to get azure blob storage content in FileInputStream in Java?

我正在研究 API(彈簧靴)。 其中我需要在 get 方法的響應體中返回 FileInputStream。

預期- 當前端調用 get-files API 時,應在瀏覽器上打開文件下載提示。

問題- 我們不能使用 blob.downloadToFile 方法,因為它會在本地機器(或托管 API 的地方)下載文件,我們需要一些東西,我們可以使用它在 API 調用中將文件直接發送到前端。 然而,還有另一種方法blob.download() ,它返回 OutputStream ,它不能在 API 調用中返回。

那么有什么方法可以將 OutputStream 轉換為 FileInputStream 而無需保存到我們設備上的實際文件。

示例代碼 -

public ByteArrayOutputStream downloadBlob() throws URISyntaxException, InvalidKeyException, StorageException, IOException {
        String storageConnectionString = "DefaultEndpointsProtocol=https;" + "AccountName=" + accountName + ";"
                + "AccountKey=" + accountKey;

        CloudStorageAccount storageAccount = CloudStorageAccount.parse(storageConnectionString);

        CloudBlobClient blobClient = storageAccount.createCloudBlobClient();

        final CloudBlobContainer container = blobClient.getContainerReference("container name");


        CloudBlockBlob blob = container.getBlockBlobReference("image.PNG");
        ByteArrayOutputStream outputStream =new ByteArrayOutputStream();
        blob.download(outputStream);
        System.out.println("file downloaded");

        return outputStream;
    }

注意-文件可以是任何類型。

這是解決方案 - 在瀏覽器中點擊 API 后,您的文件將在瀏覽器中下載 -

package com.example.demo.controllers;

import com.azure.storage.blob.BlobClient;
import com.azure.storage.blob.BlobContainerClient;
import com.azure.storage.blob.BlobServiceClient;
import com.azure.storage.blob.BlobServiceClientBuilder;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;


@RestController
public class FileDownload {
    @GetMapping(path="/download-file")
    public ResponseEntity<Resource> getFile(){
        String fileName = "Can not find symbol.docx";
        //azure credentials
        String connection_string = "Connection String of storage account on azure";

        BlobServiceClient blobServiceClient = new BlobServiceClientBuilder().connectionString(connection_string).buildClient();

        BlobContainerClient containerClient= blobServiceClient.getBlobContainerClient("Container name");
        System.out.println(containerClient.getBlobContainerName());

        BlobClient blob = containerClient.getBlobClient(fileName);

        //creating an object of output stream to recieve the file's content from azure blob.
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        blob.download(outputStream);

        //converting it to the inputStream to return
        final byte[] bytes = outputStream.toByteArray();
        ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes);
        ByteArrayResource resource = new ByteArrayResource(bytes);
        HttpHeaders headers = new HttpHeaders();
        headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + fileName + "\"");

        return ResponseEntity.ok().contentType(MediaType.APPLICATION_OCTET_STREAM)
                .headers(headers)
                .body(resource);
    }

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM