java在Spring boot中将文件上传到S3,并将其存储在类路径中
我有办法将文件从用户上传到S3 bucket。但当我测试它时,它既保存在S3 buckend上,也保存在我的类路径中。 这是我上传文件的功能:
public void uploadFileToNews(byte[] file, String fileName) {
File fileToSave = new File(fileName);
try(FileOutputStream fileOutputStream = new FileOutputStream(fileToSave) ) {
fileOutputStream.write(file);
amazonS3.putObject(new PutObjectRequest("gwnews", fileName, fileToSave));
} catch (IOException e) {
log.error("Error during uploading file to S3", e);
}
}
这就是我服务的功能:
public News addNewsImage(MultipartFile multipartFile, String newsId) throws IOException {
News news = getById(newsId);
news.setImageUrl(news.getId() + ".png");
fileService.uploadFileToNews(multipartFile.getBytes(), news.getId() + ".png");
return newsRepository.save(news);
}
我做错什么了吗?如何避免将文件保存到类路径
# 1 楼答案
这一行将内容保存在类路径中:“fileOutputStream.write(file);” 下一行是将其保存在S3存储桶中,用于相应的AWS帐户