spring boot cloud storage file upload

Viewed 2234

upload file to google cloud storage from spring boot controller. please provide me a code.

so far i came up a code like this

@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();
}
1 Answers
Related