Spring REST上传文件到亚马逊

时间:2018-01-10 17:48:21

标签: spring rest amazon-web-services

我在REST API中收到该文件,并希望将其上传到AWS(亚马逊网络服务)。但我不知道如何获得这张图片的路径,以及我应该得到的这张图片的路径。 (请查看此处提供的第3段代码)

这是我的代码:

S3Services接口:

public interface S3Services {
    void downloadFile(String keyName);
    void uploadFile(String keyName, String filePath);
}

S3ServicesImpl:

@Override
public void uploadFile(String keyName, String uploadFilePath) {

    try {
        File file = new File(uploadFilePath);
        s3client.putObject(new PutObjectRequest(bucketName, keyName, file));
        logger.info("===================== Upload File - Done! =====================");

    } catch (AmazonServiceException ase) {
        logger.info("Caught an AmazonServiceException from PUT requests, rejected reasons:");
        logger.info("Error Message:    " + ase.getMessage());
        logger.info("HTTP Status Code: " + ase.getStatusCode());
        logger.info("AWS Error Code:   " + ase.getErrorCode());
        logger.info("Error Type:       " + ase.getErrorType());
        logger.info("Request ID:       " + ase.getRequestId());
    } catch (AmazonClientException ace) {
        logger.info("Caught an AmazonClientException: ");
        logger.info("Error Message: " + ace.getMessage());
    }
}

最后,我有我的REST方法来获取照片:

@RequestMapping(value="/upload", method=RequestMethod.POST)
public @ResponseBody String handleFileUpload(
        @RequestParam("file") MultipartFile file){
    String name = "test11";
    if (!file.isEmpty()) {
        try {
            //convert multipartFile to File
            File fileToUpload = multipartToFile(file);
            s3Services.uploadFile("filename.png", ?????);
            return "You successfully uploaded " + name + " into " + name + "-uploaded !";
        } catch (Exception e) {
            return "You failed to upload " + name + " => " + e.getMessage();
        }
    } else {
        return "You failed to upload " + name + " because the file was empty.";
    }
}

我的问题是:

s3Services.uploadFile("filename.png", ?????);

我在REST方法中获取图像,我想直接上传此文件,但我必须提供我没有的图像路径。

我可以直接上传文件而不需要它的路径吗?

如果我无法直接上传文件,我该如何执行文件上传?我应该如何以及在哪里存储图像?

非常感谢!

0 个答案:

没有答案