gpt4 book ai didi

java - 如何将新文件上传到GCP存储桶?

转载 作者:行者123 更新时间:2023-12-01 20:26:22 28 4
gpt4 key购买 nike

我找到了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 Cloud GCP 将新文件上传到 GCP 存储桶的方法。

我怎样才能实现它?

最佳答案

@Autowired
private Storage gcs;

public void upload(String report) {

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

}

关于java - 如何将新文件上传到GCP存储桶?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58915330/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com