从Spring Boot Controller将文件上传到Google云存储。请给我一个代码。
到目前为止,我提出了这样的代码
@RequestMapping(value = "/uploadFile", method = RequestMethod.POST)
@SuppressWarnings("deprecation")
public String uploadFile( @RequestParam("files") Part filePart ) throws IOException {
private static Storage storage = null;
final String bucketName = "mcqimages";
DateTimeFormatter dtf = DateTimeFormat.forPattern("-YYYY-MM-dd-HHmmssSSS");
DateTime dt = DateTime.now(DateTimeZone.UTC);
String dtString = dt.toString(dtf);
String fileName = "C:\\Users\\sachinthah\\Downloads\\821092.png";
fileName = filePart.getSubmittedFileName() + dtString;
// the inputstream is closed by default, so we don't need to close it here
BlobInfo blobInfo =
storage.create(
BlobInfo
.newBuilder(bucketName, fileName)
// Modify access list to allow all users with link to read file
.setAcl(new ArrayList<>(Arrays.asList(Acl.of(User.ofAllUsers(), Role.READER))))
.build(),
filePart.getInputStream());
return blobInfo.getMediaLink();
}
答案 0 :(得分:-1)
有两个不错的来源,您可以在其中获取有关将文件上传到Google云存储的指南,第一个是官方GCP指南《使用Java的Cloud Storage》 [1],第二个是Google Cloud Storage简介[2]。 ],它们会逐步引导您,并为您提供归档您的任务所需的代码。
[1] https://cloud.google.com/java/getting-started/using-cloud-storage [2] https://www.baeldung.com/java-google-cloud-storage