java - 在 Spring Boot 中将文件上传到 S3 并将它们存储在类路径中
问题描述
我有方法将文件从用户上传到 S3 存储桶。但是当我测试它时,它既保存在 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);
}
难道我做错了什么?如何避免将文件保存到我的类路径?
解决方案
此行将内容保存在您的类路径中:“fileOutputStream.write(file);” 下一行是将其保存在相应 AWS 账户的 S3 存储桶中。
推荐阅读
- c# - c# - 过滤后保存DataGridView
- css - 跨多行的伪元素创建连续的下划线动画
- node.js - 为什么谷歌分析查询响应总用户数随维度变化?
- javascript - 提交表单后如何重定向页面?
- runtime - Trace32 函数运行时未按预期工作
- c++ - 使用 ESP8266 和 Blynk 应用程序进行实时步进电机控制
- ubuntu - 使用 ssh -t 可以通过 Jump Host 进行连接,但 ssh -W 不起作用
- python - 如何在 sudo 命令之后从 Python 转换输出
到 字符串? - regex - 正则表达式:可选组不起作用
- html - 如何组织在网格中?