Spring Boot + AWS S3:无法删除存储桶中的文件

helloApp:

我是AWS的新手,我尝试学习的第一个模块是用于文件存储的S3。

上传工作正常,问题出在删除。所以当我上传文件时,我将文件名的字符串版本存储在AWS存储桶中mybucket,并将整个URL存储在mysql数据库中,如下所示->https://mybucket.s3.eu-west-2.amazonaws.com/what.png

删除的问题是,即使我在这种情况下将整个URL传递https://mybucket.s3.eu-west-2.amazonaws.com/what.png给delete方法,该方法也会成功执行到每个步骤,告诉我文件已被成功删除,但是当我检查存储桶时,文件仍然存在我尝试在这里搜索类似的问题,但是找不到可以帮助我理解问题所在的代码。

@Service
public class AmazonS3ClientServiceImpl {
    private String awsS3AudioBucket; //bucket name
    private AmazonS3 amazonS3; // s3 object which uploads file
    private static final Logger logger = LoggerFactory.getLogger(AmazonS3ClientServiceImpl.class);

    @Autowired
    public AmazonS3ClientServiceImpl(Region awsRegion, AWSCredentialsProvider awsCredentialsProvider, String awsS3AudioBucket) {
        this.amazonS3 = AmazonS3ClientBuilder.standard()
                .withCredentials(awsCredentialsProvider)
                .withRegion(awsRegion.getName()).build();
        this.awsS3AudioBucket = awsS3AudioBucket;
    }


    public String uploadFileToS3Bucket(MultipartFile multipartFile, boolean enablePublicReadAccess) {
        String uploadedfile = ""; // the file path which is on s3
        String fileName = multipartFile.getOriginalFilename();

        try {
            //creating the file in the server (temporarily)
            File file = new File(fileName);
            FileOutputStream fos = new FileOutputStream(file);
            fos.write(multipartFile.getBytes());
            fos.close();

            PutObjectRequest putObjectRequest = new PutObjectRequest(this.awsS3AudioBucket, fileName, file);

            if (enablePublicReadAccess) {
                putObjectRequest.withCannedAcl(CannedAccessControlList.PublicRead);
            }
            this.amazonS3.putObject(putObjectRequest);
            uploadedfile = String.valueOf(this.amazonS3.getUrl(awsS3AudioBucket, fileName));
            System.out.println(this.amazonS3.getUrl(awsS3AudioBucket, fileName));
            System.out.println(uploadedfile);


            //removing the file created in the server
            file.delete();
        } catch (IOException | AmazonServiceException ex) {
            logger.error("error [" + ex.getMessage() + "] occurred while uploading [" + fileName + "] ");
        }
        return uploadedfile;
    }

    public void deleteFileFromS3Bucket(String fileName) {
        LOGGER.info("Deleting file with name= " + fileName);
        final DeleteObjectRequest deleteObjectRequest = new DeleteObjectRequest(this.awsS3AudioBucket, fileName);
        amazonS3.deleteObject(deleteObjectRequest);
        LOGGER.info("File deleted successfully");
    }

当我调用deletemethod时,我会使用它

@GetMapping("/dashboard/showposts/delete/{id}")
    public String deletePost(@PathVariable("id") Long id, Model model) {
        System.out.println("GOT HERE");
        //Retrieving Post image name
        Post post = postService.findBydId(id);
        String imageName = post.getImage();
        System.out.println(imageName);
        //Deleting image from S3 bucket
        amazonClient.deleteFileFromS3Bucket(imageName);
        //Deleting post from db
        postService.detelePost(id);
        String success = "Successfully deleted post with Id" + id;
        model.addAttribute("success", success);
        return "redirect:/admin/dashboard/showposts";
    }

任何帮助将不胜感激。

LE对于遇到相同问题并寻求快速解答的任何人,您只需将字符串图像名称传递给delete方法,而不必传递整个URL。

马克B:

您没有检查从返回的响应amazonS3.deleteObject()是否真正成功。它可能正在返回故障状态。

我猜想根本的问题是,您正在将完整的URL传递给delete方法,而不只是S3中文件的路径。例如,使用以下URL:https://mybucket.s3.eu-west-2.amazonaws.com/what.pngS3对象路径就是what.png

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Spring Boot无法连接到AWS S3存储桶-IAM凭证正确

Spring Boot Amazon AWS S3存储桶文件下载-访问被拒绝

通过Spring Boot应用程序将日志文件存储在AWS S3中

Spring Cloud DataFlow在AWS S3存储桶源中的新文件之后使用启动任务

在Spring Boot中以流形式下载AWS S3文件

使用Spring Integration AWS轮询S3存储桶以进行文件和处理

Spring Boot从AWS参数存储读取参数

AWS S3存储桶删除

AWS Beanstalk中的Spring Boot微服务

带有Spring Boot错误的AWS S3“配置文件不能为空”

使用Spring Boot从AWS S3检索文件的最佳方法是?

AWS S3无法通过PHP SDK删除存储桶中的对象

如何使用AWS CLI删除S3存储桶中的多个文件

删除 AWS S3 存储桶时权限不足,无法列出存储桶“XXXX”的对象

检查 AWS S3 存储桶中的文件数

带有Spring Cloud AWS S3的Spring Batch为欧盟存储桶返回301

如何使用spring-integration-aws获取S3存储桶中当前日期的对象计数?

Spring Boot无法在ElasticBeanstalk AWS上找到JSP文件

如何使用Spring Cloud AWS从S3删除文件?

需要在 Spring Boot 属性文件中配置 AWS 区域

Spring Boot,Spring Cloud AWS和AWS SQS无法从队列中读取

Spring Boot DynamoDB AWS异常

Python-无法将文件上传到AWS S3存储桶中的已定义文件夹

(Spring Boot)Java AWS程序的RSA私钥存储位置

如何在 Spring Boot 中使用 aws s3 进行事务

AWS Elastic Beanstalk上的Spring Boot并记录到文件

AWS SES在Spring Boot Java中实现而不使用凭证

spring boot 未连接到 AWS 中的 RabbitMQ

来自多个s3存储桶的Spring Integration AWS s3-inbound-streaming-channel-adapter流