How to read a file from Google Cloud Storage in Java

后端 未结 2 805
天命终不由人
天命终不由人 2020-12-03 08:32

I would like to read a file from Google Cloud storage using Java. The below link was not helpful as I dont use HttpServletRequest and HttpServletResponse.

Reading i

2条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-03 09:06

    Read this GCloud doc for more info.

    Your code should be:

    Storage storage = StorageOptions.newBuilder()
                .setProjectId(projectId)
                .setCredentials(GoogleCredentials.fromStream(new FileInputStream(serviceAccountJSON)))
                .build()
                .getService();
    Blob blob = storage.get(BUCKET_URL, OBJECT_URL);
    String fileContent = new String(blob.getContent());
    

提交回复
热议问题