How to download a file from Google Cloud Storage with Java?

后端 未结 3 690
不知归路
不知归路 2020-12-10 19:58

I\'m developping an application that provides users with an interface where they can download files from our Google Cloud Storage. I wrote unit tests and I could connect to

3条回答
  •  臣服心动
    2020-12-10 20:35

    @AnandMohan is correct. You just have to make copy on "tmp" folder because all the locations are read only, just "tmp" is writable. Check below:

        Storage storage = StorageOptions.newBuilder().setProjectId(PROJECT_ID).build().getService();
        Blob blob = storage.get(BUCKET_NAME, fileName);
        ReadChannel readChannel = blob.reader();
        File file = new File("/tmp/" + FILE_NAME);
        FileOutputStream fileOuputStream = new FileOutputStream(file);
        fileOuputStream.getChannel().transferFrom(readChannel, 0, Long.MAX_VALUE);
        fileOuputStream.close();
        // Now file is ready to send whereever you want
    

提交回复
热议问题