【问题标题】:Error converting multipartFile to file while uploading File to s3 Bucket java.io.FileNotFoundException将文件上传到 s3 Bucket java.io.FileNotFoundException 时将 multipartFile 转换为文件时出错
【发布时间】:2021-10-04 15:04:32
【问题描述】:

我正在尝试使用 Spring Boot 和 spring-cloud-starter-aws 将文件上传到 S3 Bucket。

它在我的本地运行良好,但在上传到 EBS 时,文件没有上传。

代码:

public String uploadFileToS3Bucket(MultipartFile file, String path, boolean enablePublicReadAccess) {
        File fileObj = convertMultiPartFileToFile(file);
        String fileName = System.currentTimeMillis() + "_" + file.getOriginalFilename();
        fileName = path +fileName;
        try {
            s3Client.putObject(new PutObjectRequest(bucketName, fileName, fileObj));
            fileObj.delete();
        }catch (Exception e) {
            System.out.println("File Upload Failed due to the following: "+ e);
        }
        
        System.out.println("File uploaded : " + fileName);
        return fileName;
    }

convertMultiPartFileToFile 方法:

     private File convertMultiPartFileToFile(MultipartFile file) {
        Random rnd = new Random();
        String randomVal = String.valueOf(100000 + rnd.nextInt(900000));
        String fileName = randomVal + "-" + file.getOriginalFilename();

        System.out.println("upload file name test:::: " + fileName);

        String filePath = "";

        logger.info(env.getProperty("testing"));
        File convertedFile = null;

        try {
            
            convertedFile = new File(filePath + fileName);
            
            System.out.println("The converted file: "+ convertedFile);
            convertedFile.createNewFile();
            FileOutputStream fos = new FileOutputStream(convertedFile);
            fos.write(file.getBytes());
            fos.close();

        } catch (Exception e) {
            System.out.println("Error converting multipartFile to file:" + e);
        }

        return convertedFile;
    }

在日志文件中我收到以下错误:

Error converting multipartFile to file:java.io.IOException: Permission denied
File Upload Failed due to the following: com.amazonaws.SdkClientException: Unable to 
calculate MD5 hash: 746350-about_us_work.jpeg (No such file or directory)

EBS IAM 角色具有 S3FullAccess。

它在本地可以完美运行,但在服务器上却不是这样。任何想法可能是什么问题?

【问题讨论】:

标签: amazon-web-services spring-boot amazon-s3 spring-cloud-aws


【解决方案1】:

使用来自Unable to calculate MD5 : AWS S3 bucket的参考解决了它

更新后的代码如下所示:

public String uploadFileToS3Bucket(MultipartFile file, String path, boolean enablePublicReadAccess) {
        File fileObj = convertMultiPartFileToFile(file);
        String fileName = System.currentTimeMillis() + "_" + file.getOriginalFilename();
        fileName = path +fileName;
        try {
            InputStream is=file.getInputStream();        
            s3Client.putObject(new PutObjectRequest(bucketName, fileName, is, new ObjectMetadata()));
            fileObj.delete();
        }catch (Exception e) {
            System.out.println("File Upload Failed due to the following: "+ e);
        }
        
        System.out.println("File uploaded : " + fileName);
        return fileName;
    }

【讨论】:

    猜你喜欢
    • 2013-03-14
    • 2023-03-17
    • 1970-01-01
    • 2015-04-12
    • 2023-01-09
    • 2022-12-09
    • 2020-05-07
    • 2017-07-13
    • 2015-05-05
    相关资源
    最近更新 更多