【发布时间】:2020-09-09 17:00:10
【问题描述】:
我是 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";
}
任何帮助将不胜感激。
L.E 对于遇到相同问题并正在寻找快速答案的任何人。您只需将字符串图像名称传递给删除方法,而不是整个 URL。
【问题讨论】:
标签: java spring amazon-web-services spring-boot amazon-s3