Java 如何将新文件上载到GCP bucket?

Java 如何将新文件上载到GCP bucket?,java,spring-boot,google-cloud-platform,spring-cloud,spring-cloud-gcp,Java,Spring Boot,Google Cloud Platform,Spring Cloud,Spring Cloud Gcp,我发现了如何读取/更新GCP bucket中的现有文件: @RestController public class WebController { @Value("gs://${gcs-resource-test-bucket}/my-file.txt") private Resource gcsFile; @RequestMapping(value = "/", method = RequestMethod.GET) public String readGc

我发现了如何读取/更新GCP bucket中的现有文件:

@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 bucket中的方法


如何实现它?

您可以查看java代码示例的官方GCP。
@Autowired
private Storage gcs;

public void upload(String report) {

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

}