我发现excellent example如何读取/更新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存储桶的方法。
如何实现?
答案 0 :(得分:0)
@Autowired
private Storage gcs;
public void upload(String report) {
gcs.create(BlobInfo
.newBuilder("bucket_name", "fileName.json")
.build(),
report.getBytes());
}