简体   繁体   English

AWS 上的 S3 文件上传问题 - Spring Boot

[英]S3 file upload issue on AWS - Spring Boot

I have written a piece of code, to upload a file on Amazon S3.我编写了一段代码,用于在 Amazon S3 上上传文件。 It is working fine in my local system.它在我的本地系统中运行良好。 I am able to upload a file and as a response I am getting the file url.我可以上传文件,作为响应,我得到了文件 url。 But when I try to upload those files to AWS server, they are not getting uploaded, I am getting a 200 response as well, and its showing a rectangle box on postman.但是当我尝试将这些文件上传到 AWS 服务器时,它们没有被上传,我也收到了 200 响应,并且它在邮递员上显示了一个矩形框。

Can anyone help me to solve this issue?谁能帮我解决这个问题? Any help will be appreciated.任何帮助将不胜感激。 Thanks!谢谢!

@Service
public class AmazonClient {

    private AmazonS3 amazonS3;

    @Value("${amazonProperties.accessKey}")
    public String accessKey;

    @Value("${amazonProperties.secretKey}")
    public String secretKey;

    @Value("${amazonProperties.bucketName}")
    public String bucketName;

    @Value("${amazonProperties.endpointUrl}")
    public String endpointUrl;

    @Value("${amazonProperties.region}")
    public String region;

    @PostConstruct
    private void initializeAmazon()
    {
        AWSCredentials credentials = new BasicAWSCredentials(accessKey, secretKey);

        this.amazonS3 =  AmazonS3ClientBuilder
                .standard()
                .withCredentials(new AWSStaticCredentialsProvider(credentials))
                .withRegion(region)
                .build();
    }


    private File convertMultiPartToFile(MultipartFile file) throws IOException
    {
        File convFile = new File(file.getOriginalFilename());
        FileOutputStream fos = new FileOutputStream(convFile);
        fos.write(file.getBytes());
        fos.close();
        return convFile;
    }

    private String generateFileName(MultipartFile multiPart)
    {
        return new Date().getTime() + "-" + multiPart.getOriginalFilename().replace(" ", "_");
    }

    private void uploadFileTos3bucket(String fileName, File file)
    {
        amazonS3.putObject(new PutObjectRequest(bucketName, fileName, file)
                .withCannedAcl(CannedAccessControlList.PublicRead));
    }

    public String uploadFile(MultipartFile multipartFile) {

        String fileUrl = "";

        try
        {
            File file = convertMultiPartToFile(multipartFile);
            String fileName = generateFileName(multipartFile);
            fileUrl = endpointUrl + "/" + bucketName + "/" + fileName;
            uploadFileTos3bucket(fileName, file);
            //file.delete();
        }

        catch (Exception e)
        {
            e.printStackTrace();
        }

        return fileUrl;
    }

    public String deleteFileFromS3Bucket(String fileUrl)
    {
        String fileName = fileUrl.substring(fileUrl.lastIndexOf("/") + 1);
        amazonS3.deleteObject(new DeleteObjectRequest(bucketName + "/", fileName));

        return "Successfully deleted";
    }
}

Please ensure that the AWS IAM credentials provided are working and the bucket location, as well as the name, are provided correctly.请确保提供的 AWS IAM 凭证有效,并且正确提供了存储桶位置和名称。 If it is then:- i.如果是这样:- i。 Make sure that the bucket is accessible programmatically.确保可以以编程方式访问存储桶。 ii. ii. Make sure that the IAM role has privileges enough to interact with S3 bucket eg(Full S3 access policy).确保 IAM 角色具有与 S3 存储桶交互的足够权限,例如(完整的 S3 访问策略)。

These might be probable resolutions.这些可能是可能的解决方案。

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

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