alioss
sky:jwt:# 设置jwt签名加密时使用的秘钥admin-secret-key: itcast# 设置jwt过期时间admin-ttl: 7200000# 设置前端传递过来的令牌名称admin-token-name: tokenalioss:endpoint: ${sky.alioss.endpoint}access-key-id: ${sky.alioss.access-key-id}access-key-secret: ${sky.alioss.access-key-secret}bucket-name: ${sky.alioss.bucket-name}
alioss
alioss:endpoint: oss-cn-beijing.aliyuncs.comaccess-key-id: LTAI5tD64jiY7ex4UJCVr5hpaccess-key-secret: eSpmy0Ke3rvMjNG8oTbMw3Zjy60HgKbucket-name: sky-itcast-ywq132
ossConfiguration
/*** 配置类,用于创建aliossutil对象*/
@Configuration
@Slf4j
public class ossConfiguration {@Bean//将返回值交给ioc容器管理,成为ioc容器的bean对象@ConditionalOnMissingBean//保证容器只有一个对象public AliOssUtil aliOssUtil (AliOssProperties aliOssProperties) {log.info("开始创建阿里云文件上传工具对象,{}",aliOssProperties);return new AliOssUtil(aliOssProperties.getEndpoint(),aliOssProperties.getAccessKeyId(),aliOssProperties.getAccessKeySecret(),aliOssProperties.getBucketName());}
}
CommonController
/*** 通用接口*/
@RestController
@RequestMapping("/admin/common")
@Api(tags = "通用接口")
@Slf4j
public class CommonController {@Autowiredprivate AliOssUtil aliOssUtil;/*** 文件上传** @param file* @return*/@PostMapping("/upload")@ApiOperation("文件上传")public Result<String> upload(MultipartFile file) {log.info("文件上传: { }", file);try {//原始文件名String originalFilename = file.getOriginalFilename();//截取原始文件名后缀 jpgString extension = originalFilename.substring(originalFilename.lastIndexOf("."));//构建新的文件夹名称String objectName = UUID.randomUUID().toString() + extension;//文件夹的请求路径String filePath = aliOssUtil.upload(file.getBytes(), objectName);return Result.success(filePath);} catch (IOException e) {log.error("文件上传失败");}return null;}
}