首先创建一个阿里云oss,记住其中的的AccessKey和id还有bucketname,可以在右上角的AccessKey管理中查看
1.配置pom依赖
<dependency> <groupId>com.aliyun.oss</groupId> <artifactId>aliyun-sdk-oss</artifactId> <version>3.15.1</version> </dependency> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.4</version> </dependency> <dependency> <groupId>commons-beanutils</groupId> <artifactId>commons-beanutils</artifactId> <version>1.9.3</version> </dependency>
2.在application写入oss配置信息
alioss: endpoint: 地址 access-key-id: id access-key-secret: key bucket-name:仓库名
3.创建一个properties用于获取写在application中的oss配置信息
@Component @ConfigurationProperties(prefix = "alioss") @Data public class AliOssProperties { private String endpoint; private String accessKeyId; private String accessKeySecret; private String bucketName; }
4.创建一个AliOssUtil
@Data @AllArgsConstructor @Slf4j public class AliOssUtil { private String endpoint; private String accessKeyId; private String accessKeySecret; private String bucketName; public String upload(byte[] bytes,String objectName){ //创建osslient实例 OSS ossClient = new OSSClientBuilder().build(endpoint,accessKeyId,accessKeySecret); try { // 创建PutObject请求。 ossClient.putObject(bucketName, objectName, new ByteArrayInputStream(bytes)); } catch (OSSException oe) { System.out.println("Caught an OSSException, which means your request made it to OSS, " + "but was rejected with an error response for some reason."); System.out.println("Error Message:" + oe.getErrorMessage()); System.out.println("Error Code:" + oe.getErrorCode()); System.out.println("Request ID:" + oe.getRequestId()); System.out.println("Host ID:" + oe.getHostId()); } catch (ClientException ce) { System.out.println("Caught an ClientException, which means the client encountered " + "a serious internal problem while trying to communicate with OSS, " + "such as not being able to access the network."); System.out.println("Error Message:" + ce.getMessage()); } finally { if (ossClient != null) { ossClient.shutdown(); } } //文件访问路径规则 https://BucketName.Endpoint/ObjectName StringBuilder stringBuilder = new StringBuilder("https://"); stringBuilder .append(bucketName) .append(".") .append(endpoint) .append("/") .append(objectName); log.info("文件上传到:{}", stringBuilder.toString()); return stringBuilder.toString(); } }
5.创建一个OssConfiguration配置类,用于配置AliOssUtil,注意这里需要在工具类对象后面加上{},否则控制台不会输出工具类对象属性!
@Configuration @Slf4j public class OssConfiguration { @Bean @ConditionalOnMissingBean public AliOssUtil aliOssUtil(AliOssProperties aliOssProperties){ log.info("开始创建阿里云文件上传工具类对象:{}",aliOssProperties); return new AliOssUtil(aliOssProperties.getEndpoint(), aliOssProperties.getAccessKeyId(), aliOssProperties.getAccessKeySecret(), aliOssProperties.getBucketName()); } }
6.最后controller
@Autowired private AliOssUtil aliOssUtil; @PostMapping("/upImg") public Result<String> upImg(MultipartFile file){ log.info("文件上传,{}",file); try { //原始文件名 String originalFilename = file.getOriginalFilename(); //截取原文件名后缀 demo.exe String extension = originalFilename.substring(originalFilename.lastIndexOf(".")); //构建新文件名 String objectName = UUID.randomUUID().toString() + extension; //文件的请求路径 String filePath = aliOssUtil.upload(file.getBytes(), objectName); return Result.success(filePath); }catch (Exception e){ log.error("文件上传失败,{}",e); } return Result.error("文件上传失败"); }