阿里OSS
新建一个properties 永远存储阿里OSS的配置文件信息
因为bucket是可读不可写的 当需要获取岛文件路径时 直接使用host字段 + 文件路径即可读取文件信息
objectname 用于存储文件
aliyun.oss.endpoint = oss-cn-beijing.aliyuncs.com
aliyun.oss.bucket = lyra-news
aliyun.oss.objectname = images/
aliyun.oss.host = test
和之前一样读取配置我二级
@PropertySource("classpath:aliOSS.properties")
@Component
@ConfigurationProperties(prefix = "aliyun.oss")
public class AliyunOSSResources {
private String endpoint;
private String bucket;
private String objectname;
private String host;
public String getEndpoint() {
return endpoint;
}
public void setEndpoint(String endpoint) {
this.endpoint = endpoint;
}
public String getBucket() {
return bucket;
}
public void setBucket(String bucket) {
this.bucket = bucket;
}
public String getObjectname() {
return objectname;
}
public void setObjectname(String objectname) {
this.objectname = objectname;
}
public String getHost() {
return host;
}
public void setHost(String host) {
this.host = host;
}
}
根据API文档 写配置工具类 返回时将路径和host拼接 返回名称
@Component
public class AliOSSUtils {
@Autowired
private AliyunOSSResources aliyunOSSResources;
@Autowired
private AliyunResources aliyunResources;
public String uploadFile(InputStream fileInputStream, String uploadPathAndFileName) {
// yourEndpoint填写Bucket所在地域对应的Endpoint。以华东1(杭州)为例,Endpoint填写为https://oss-cn-hangzhou.aliyuncs.com。
String endpoint = aliyunOSSResources.getEndpoint();
// 阿里云账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM用户进行API访问或日常运维,请登录RAM控制台创建RAM用户。
String accessKeyId = aliyunResources.getAccessKeyId();
String accessKeySecret = aliyunResources.getAccessKeySecret();
// 创建OSSClient实例。
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
// 依次填写Bucket名称(例如examplebucket)和Object完整路径(例如exampledir/exampleobject.txt)。Object完整路径中不能包含Bucket名称。
ossClient.putObject(aliyunOSSResources.getBucket(), uploadPathAndFileName, fileInputStream);
// 关闭OSSClient。
ossClient.shutdown();
return aliyunOSSResources.getHost() + "/" + uploadPathAndFileName;
}
}
避免文件重复 使用uuid来进行命名 并将后缀拼接
public String uploadFileOfOSS(MultipartFile multipartFile, String userId) throws IOException {
String fileName = multipartFile.getOriginalFilename();
if (StringUtils.isNotBlank(fileName) && StringUtils.isNotBlank(userId)) {
String suffix = fileName.substring(fileName.lastIndexOf("."));
String filePathAndFileName = userId + "/" + UUID.randomUUID().toString() + suffix;
return aliOSSUtils.uploadFile(multipartFile.getInputStream(), filePathAndFileName);
} else {
GraceException.display(ResponseStatusEnum.FILE_UPLOAD_NULL_ERROR);
return null;
}
}
controller调用即可
虽然道路是曲折的,但前途是光明的。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律