为了账号安全,请及时绑定邮箱和手机立即绑定

如何从 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条经验 获得超12个赞

看起来您只想授予用户下载文件的权限。

解决方案是使用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 关注
  • 106 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信