0

我找到了如何读取/更新 GCP 存储桶中现有文件的绝佳示例:

@RestController
public class WebController {

    @Value("gs://${gcs-resource-test-bucket}/my-file.txt")
    private Resource gcsFile;

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String readGcsFile() throws IOException {
        return StreamUtils.copyToString(
                this.gcsFile.getInputStream(),
                Charset.defaultCharset()) + "\n";
    }

    @RequestMapping(value = "/", method = RequestMethod.POST)
    String writeGcs(@RequestBody String data) throws IOException {
        try (OutputStream os = ((WritableResource) this.gcsFile).getOutputStream()) {
            os.write(data.getBytes());
        }
        return "file was updated\n";
    }
}

但我找不到如何使用 spring gloud GCP 将新文件上传到 GCP 存储桶的方法。

我怎样才能实现它?

4

1 回答 1

2
@Autowired
private Storage gcs;

public void upload(String report) {

    gcs.create(BlobInfo
                    .newBuilder("bucket_name", "fileName.json")
                    .build(),
            report.getBytes());

}
于 2019-11-18T14:12:56.353 回答