如何将新文件上传到GCP bucket



我发现了如何读取/更新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 updatedn";
}
}

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

我怎样才能做到这一点?

@Autowired
private Storage gcs;
public void upload(String report) {
gcs.create(BlobInfo
.newBuilder("bucket_name", "fileName.json")
.build(),
report.getBytes());
}

相关内容

  • 没有找到相关文章

最新更新