一 新建云存储微服务
1 创建模块
Artifact:service_oss
2 配置pom.xml
检查一下guli_parent中的aliyun-sdk-oss的版本,切换到3.8.0
<dependencies>
<!--aliyunOSS-->
<dependency>
<groupId>com.aliyun.oss</groupId>
<artifactId>aliyun-sdk-oss</artifactId>
</dependency>
</dependencies>
3 配置application.yml
server:
port: 8120 # 服务端口
spring:
profiles:
active: dev # 环境设置
application:
name: service-oss # 服务名
aliyun:
oss:
endpoint: <你的endpoint>
keyid: <你的keyid>
keysecret: <你的keysecret>
# bucket可以在控制台创建,也可以使用java代码创建,注意先测试bucket是否已被占用
bucketname: gulimall-cakin
4 logback-spring.xml
修改日志路径为 guli_log/oss
5 创建启动类
创建ServiceOssApplication.java
@SpringBootApplication
@ComponentScan({"com.atguigu.guli"})
public class ServiceOssApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceOssApplication.class, args);
}
}
6 启动项目
测试项目初始化是否成功,报告如下错误
原因:jdbc依赖引入项目后,springboot的自动配置试图在配置文件中查找jdbc相关配置
解决方案:主类上添加注解exclude属性,禁用jdbc自动配置
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class) // 取消数据源自动配置
二 后端实现文件上传
1 从配置文件读取常量
创建常量读取工具类:OssProperties.java
@Data
@Component
@ConfigurationProperties(prefix = "aliyun.oss")
public class OssProperties {
private String endpoint;
private String keyid;
private String keysecret;
private String bucketname;
}
2 Service
public interface FileService {
/**
* 阿里云oss文件上传
* @param inputStream 输入流
* @param module 文件夹名称
* @param originalFilename 原始文件名
* @return 文件在oss服务器上的url地址
*/
String upload(InputStream inputStream, String module, String originalFilename);
}
3 实现类
@Service
public class FileServiceImpl implements FileService {
/**
* oss属性注入
*/
@Autowired
private OssProperties ossProperties;
@Override
public String upload(InputStream inputStream, String module, String originalFilename) {
//读取配置信息
String endpoint = ossProperties.getEndpoint();
String keyid = ossProperties.getKeyid();
String keysecret = ossProperties.getKeysecret();
String bucketname = ossProperties.getBucketname();
// 创建OSSClient实例
OSS ossClient = new OSSClientBuilder().build(endpoint, keyid, keysecret);
if(!ossClient.doesBucketExist(bucketname)){
ossClient.createBucket(bucketname);
ossClient.setBucketAcl(bucketname, CannedAccessControlList.PublicRead);
}
// 构建objectName:文件路径 avatar/2020/11/28/default.jpg
String folder = new DateTime().toString("yyyy/MM/dd");
String fileName = UUID.randomUUID().toString();
String fileExtension = originalFilename.substring(originalFilename.lastIndexOf("."));
String key = module +"/" + folder + "/" + fileName + fileExtension;
// 上传文件流
ossClient.putObject(bucketname, key, inputStream);
// 关闭OSSClient。
ossClient.shutdown();
// 返回url
//https://gulimall-cakin.oss-cn-chengdu.aliyuncs.com/avatar/default.jpg
return "https://" + bucketname + "." + endpoint + "/" + key;
}
}
4 控制器
@Api(description = "阿里云文件管理")
@CrossOrigin
@RestController
@RequestMapping("/admin/oss/file")
@Slf4j
public class FileController {
/**
* 文件上传服务
*/
@Autowired
private FileService fileService;
/**
* 功能描述:文件上传控制器
*
* @author cakin
* @date 2020/11/28
* @param file 文件
* @param module 文件夹
* @return R 返回给前端的数据
*/
@ApiOperation("文件上传")
@PostMapping("upload")
public R upload(@ApiParam(value = "文件", required = true) @RequestParam("file") MultipartFile file,
@ApiParam(value = "模块", required = true) @RequestParam("module") String module) {
try {
InputStream inputStream = file.getInputStream();
String originalFilename = file.getOriginalFilename();
String uploadUrl = fileService.upload(inputStream, module, originalFilename);
return R.ok().message("文件上传成功").data("url", uploadUrl);
} catch (Exception e) {
log.error(ExceptionUtils.getMessage(e));
throw new GuliException(ResultCodeEnum.FILE_UPLOAD_ERROR);
}
}
}