亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何從 Google Cloud Storage 下載文件并將其返回到 Spring 控制器中

如何從 Google Cloud Storage 下載文件并將其返回到 Spring 控制器中

躍然一笑 2023-07-28 17:02:16
我有一個 Spring Boot 應用程序。用戶可以登錄我的應用程序并上傳文件。用戶的所有文件都存儲在Google Cloud Storage中。現在,我希望用戶能夠下載他們的文件。所以,我必須從云存儲下載文件。我不知道我的控制器應該是什么樣子。使用我當前的代碼,我得到一個空文件。上傳已經完成,連接也正常。public static Blob downloadFile(Storage storage, String fileName){        Blob blob = storage.get(BUCKET_NAME, fileName);        return blob;    }@RequestMapping(value = "/downloadFileTest")    @ResponseBody    public void downloadFile(HttpSession session,            HttpServletResponse response) {        Storage storage = de.msm.msmcenter.service.cloudstorage.Authentication.getStorage();        Blob blob = de.msm.msmcenter.service.cloudstorage.Authentication.downloadFile(storage,"test.txt");        ReadChannel readChannel = blob.reader();        InputStream inputStream = Channels.newInputStream(readChannel);        try {            response.setContentType("application/force-download");            response.setHeader("Content-Disposition", "attachment; filename=test.txt");            IOUtils.copy(inputStream, response.getOutputStream());            response.flushBuffer();            inputStream.close();        } catch (Exception e){            e.printStackTrace();        }    }我實際上希望能夠下載任何文件,而不僅僅是txt。當用戶打開鏈接時,會下載名為 test.txt 的文件,但它是空的。
查看完整描述

3 回答

?
紅顏莎娜

TA貢獻1842條經驗 獲得超13個贊

看起來您只想授予用戶下載文件的權限。

解決方案是使用Signed URL,它可以讓您向用戶提供一個 URL 以在有限的時間內訪問/下載對象。如果您將用戶直接重定向到該 URL,下載將立即開始。


查看完整回答
反對 回復 2023-07-28
?
哈士奇WWW

TA貢獻1799條經驗 獲得超6個贊

我將代碼更改為:


public static String downloadFile(Storage storage, String fileName){

        Blob blob = storage.get(BUCKET_NAME, fileName);

        String PATH_TO_JSON_KEY = "/your/path";

        URL signedUrl = null;

        try {

            signedUrl = storage.signUrl(BlobInfo.newBuilder(BUCKET_NAME, fileName).build(),

                    1, TimeUnit.DAYS, SignUrlOption.signWith(ServiceAccountCredentials.fromStream(

                            new FileInputStream(PATH_TO_JSON_KEY))));

        } catch (IOException e) {

            e.printStackTrace();

        }

        return signedUrl.toString();

    }


查看完整回答
反對 回復 2023-07-28
?
紅糖糍粑

TA貢獻1815條經驗 獲得超6個贊

#將此行添加到 spring-boot application.properties 文件 spring.cloud.gcp.credentials.location=classpath:key.json


    // read/download objects

public static ResponseEntity<byte[]> getObjectFromGCP(String yourfileName) throws IOException {

    String objectNameWithLocation ="your file location with file name in GCP bucket";

     //create your storage object with your credentials

    

    Credentials credentials = GoogleCredentials.fromStream(new 

       ClassPathResource("key.json").getInputStream());

    Storage storage = StorageOptions.newBuilder().setCredentials(credentials).build().getService();

    BlobId blobId = BlobId.of(bucketName, objectNameWithLocation);

    Blob blob = storage.get(blobId);

    return ResponseEntity.ok().contentType(MediaType.valueOf(FileTypeMap.getDefaultFileTypeMap().getContentType(yourfileName)))

            .body(blob.getContent(BlobSourceOption.generationMatch()));

}


查看完整回答
反對 回復 2023-07-28
  • 3 回答
  • 0 關注
  • 219 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號