繁体   English   中英

春季启动云存储文件上传

[英]spring boot cloud storage file upload

从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();
}

有两个不错的来源,您可以从中获取有关将文件上传到Google云存储的指南,第一个是官方GCP指南《使用Java进行云存储》 [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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM