如何从Spring Boot Rest将图像上传到App Engine存储桶

时间:2018-10-19 10:30:09

标签: java spring-boot google-app-engine

我一直在尝试将图像从Spring Boot控制器上传到app引擎存储区,同样在下面的代码中。但经邮递员测试后无法正常工作。

@RestController
@RequestMapping("/api")
public class UploadingController {
@RequestMapping(method = RequestMethod.POST, value = "/imageUpload")
@SuppressWarnings("deprecation")
public String uploadImages(Part filePart) throws IOException {

String bucketName = "mcqimages";
    DateTimeFormatter dtf = DateTimeFormat.forPattern("-YYYY-MM-dd-HHmmssSSS");
    DateTime dt = DateTime.now(DateTimeZone.UTC);
    String dtString = dt.toString(dtf);
    final String 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 the public download link
    return blobInfo.getMediaLink();
}
}

0 个答案:

没有答案