[英]How to upload new file into 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 updated\n";
}
}
但我找不到如何使用 spring gloud GCP 將新文件上傳到 GCP 存儲桶的方法。
我怎樣才能實現它?
@Autowired
private Storage gcs;
public void upload(String report) {
gcs.create(BlobInfo
.newBuilder("bucket_name", "fileName.json")
.build(),
report.getBytes());
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.