🌹什么是阿里云OSS 阿里云对象存储(Alibaba Cloud Object Storage Service,简称OSS)是阿里云提供的海量、安全、低成本、高可靠的云存储服务。用户可以通过简单的API接口在任何时间、任何地点上传和下载数据,适用于图像、音视频、文档、网站等各种类型的数据存储和分发场景。
⭐阿里云OSS的优点 高可靠性 :OSS采用了多副本存储和容灾备份机制,确保数据的高可靠性和持久性。安全性 :支持多种安全策略,如访问控制、加密传输等,保障数据的安全性。弹性扩展 :支持按需存储和弹性扩展,用户可以根据实际需求灵活调整存储容量。低成本 :OSS的存储费用低廉,且支持按量付费模式,使用户能够根据实际使用量付费
🏳️🌈为什么要使用云服务OSS 解决本地存储无法直接访问,本地磁盘空间有限,本地磁盘损坏这3个问题
🎄使用步骤
文件上传操作
🎈在pom文件中添加下面的代码
1 2 3 4 5 6 7 8 9 10 <properties > <aliyun.sdk.oss > 3.10.2</aliyun.sdk.oss > </properties > <dependency > <groupId > com.aliyun.oss</groupId > <artifactId > aliyun-sdk-oss</artifactId > <version > ${aliyun.sdk.oss}</version > </dependency >
🎈本地存储的话 在yml中写明存储地方
1 2 3 ali: spring: localhost: D:\Program Files\Project\Test-Project\TEST01\alioosdemo\ali-service\src\main\resources\upload
🎈在Controller层进行上传操作
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 @RestController @RequestMapping("/api/common") public class CommonController { @Value("${ali.spring.localhost}") private String path; @Autowired private AliOssUtil aliOssUtil; @PostMapping("upload") public String Upload (MultipartFile file) throws IOException { String originalFilename = file.getOriginalFilename(); String substring = originalFilename.substring(originalFilename.lastIndexOf("." )); String filename = IdUtil.simpleUUID() + substring; File file1 = new File (filename); try { file.transferTo(file1); } catch (IOException e) { throw new RuntimeException (e); } return "上传成功" ; } }
文件上传操作(OSS版)
🎈Oss存储 在yml中写明存储地方 access-key-id 、 access-key-secret 、bucket-name
1 2 3 4 5 6 7 ali: alioos: endpoint: oss-cn-beijing.aliyuncs.com access-key-id: *********** access-key-secret: *********** bucket-name: ***********
使用@ConfigurationProperties注解读取 properties的数据
1 2 3 4 5 6 7 8 9 10 11 12 13 14 @Component @ConfigurationProperties(prefix = "ali.alioos") @Data public class AliOssProprtyies { private String endpoint; private String accessKeyId; private String accessKeySecret; private String bucketName; }
配置类,用于创建AliOssUtil对象
1 2 3 4 5 6 7 8 9 10 11 12 13 14 @Configuration public class OssConfiguration { @Bean @ConditionalOnMissingBean public AliOssUtil aliOssUtil (AliOssProprtyies aliOssProprtyies) { return new AliOssUtil (aliOssProprtyies.getEndpoint(), aliOssProprtyies.getAccessKeyId(), aliOssProprtyies.getAccessKeySecret(), aliOssProprtyies.getBucketName()); } }
定义阿里云工具类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 @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) { OSS ossClient = new OSSClientBuilder ().build(endpoint, accessKeyId, accessKeySecret); try { 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(); } } StringBuilder stringBuilder = new StringBuilder ("https://" ); stringBuilder .append(bucketName) .append("." ) .append(endpoint) .append("/" ) .append(objectName); log.info("文件上传到:{}" , stringBuilder.toString()); return stringBuilder.toString(); } }
自定义上传接口
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 @Autowired private AliOssUtil aliOssUtil;@PostMapping("upload") public String Upload (MultipartFile file) throws IOException { String originalFilename = file.getOriginalFilename(); String substring = originalFilename.substring(originalFilename.lastIndexOf("." )); String filename = IdUtil.simpleUUID() + substring; aliOssUtil.upload(file.getBytes(), filename); return "上传成功" ; }