一 .准备
进入阿里
进入控制台
创建bucket
新建目录
点击AccessKey管理
创建AccessKey并复制下载key值
二.使用
导入阿里巴巴和spring依赖
<dependency>
<groupId>com.aliyun.oss</groupId>
<artifactId>aliyun-sdk-oss</artifactId>
<version>3.8.0</version>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alicloud-oss</artifactId>
<version>2.2.0.RELEASE</version>
</dependency>
配置yml文件 两个key用上面下载的, endpoint到阿里巴巴里面找
上传文件
public String upload(MultipartFile file) {
//拼接一个新的文件名称
String originalFilename = file.getOriginalFilename();
String uuid = UUID.randomUUID().toString().toUpperCase().replace("-", "");
String newFilename = uuid + "-" + originalFilename;
//上传
String bucketName = "youBucketName";
String objectName = "imgs/" + newFilename;
try {
ossClient.putObject(new PutObjectRequest(bucketName, objectName, file.getInputStream()));
} catch (IOException e) {
throw new CustomException(ExceptionCode.UPLOAD_ERROR);
}
return "https://wnhz09-sk.oss-cn-hangzhou.aliyuncs.com/" + objectName;
}
删除
ossClient.deleteObject(bucketName, objectName);