我是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。
您没有检查从返回的响应amazonS3.deleteObject()
是否真正成功。它可能正在返回故障状态。
我猜想根本的问题是,您正在将完整的URL传递给delete方法,而不只是S3中文件的路径。例如,使用以下URL:https://mybucket.s3.eu-west-2.amazonaws.com/what.png
S3对象路径就是what.png
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句