上一篇:springboot2.2.X手册:对象复制哪种最快?7种复制方式性能对比

今天有个非常巧合的机会,无意间发现阿里云的OSS一年只需要9块钱,5年只需要45块钱,有40G容量,这是什么概念?在送人吗?赶紧撸一把,有可能是我以前没发现,但是我记得以前好贵的~~~

OSS介绍

海量、安全、低成本、高可靠的云存储服务,提供99.9999999999%的数据可靠性。使用RESTful API 可以在互联网任何位置存储和访问,容量和处理能力弹性扩展,多种存储类型供选择全面优化存储成本。

开通服务

可以私信我获取优惠券,发送:优惠券

构建工具包

引入ossClient包

     <!-- 图片上传 SDK 阿里云oss --><dependency><groupId>com.aliyun.oss</groupId><artifactId>aliyun-sdk-oss</artifactId><version>3.9.1</version></dependency><!-- 文件上传工具 --><dependency><groupId>commons-fileupload</groupId><artifactId>commons-fileupload</artifactId><version>1.4</version></dependency><!-- springboot核心web --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency>
/*** 阿里云oss属性配置* @author:溪云阁* @date:2020年5月16日*/
@Component
@ConfigurationProperties(prefix = "module.boots.oss")
@Data
public class AliyunOSSProperties {// oss上bucket的名称private String bucketName;// 阿里对应的访问idprivate String accessKeyId;// 阿里对应的密钥private String accessKeySecret;// oss对应的区域节点private String endpoint;}
/*** OSS文件上传工具* @author:溪云阁* @date:2020年5月16日*/
@Component
@Slf4j
public class AliyunOssUtils {@Autowiredprivate AliyunOSSProperties properties;/*** 上传文件到阿里云 OSS 服务器* @author 溪云阁* @param files 文件* @param fileTypeEnum 文件类型* @return List<String>*/public List<String> uploadFile(MultipartFile[] files, String storagePath, FileTypeEnum fileTypeEnum) {// 创建OSSClient实例final OSS ossClient = new OSSClientBuilder().build(properties.getEndpoint(), properties.getAccessKeyId(), properties.getAccessKeySecret());final List<String> fileIds = new ArrayList<>();try {for (final MultipartFile file : files) {// 创建一个唯一的文件名,类似于id,就是保存在OSS服务器上文件的文件名(自定义文件名)String fileName = IdUtil.randomUUID();final InputStream inputStream = file.getInputStream();// 设置对象final ObjectMetadata objectMetadata = new ObjectMetadata();// 设置数据流里有多少个字节可以读取objectMetadata.setContentLength(inputStream.available());objectMetadata.setCacheControl("no-cache");objectMetadata.setHeader("Pragma", "no-cache");objectMetadata.setContentType(file.getContentType());objectMetadata.setContentDisposition("inline;filename=" + fileName);fileName = storagePath + "/" + fileName;// 上传文件final PutObjectResult result = ossClient.putObject(properties.getBucketName(), fileName, inputStream, objectMetadata);log.info("Aliyun OSS AliyunOSSUtil.uploadFileToAliyunOSS,result:{}", result);}}catch (final IOException e) {log.error("Aliyun OSS AliyunOSSUtil.uploadFileToAliyunOSS fail,reason:{}", e);}finally {ossClient.shutdown();}return fileIds;}/*** 删除文件* @author 溪云阁* @param fileName void*/public void deleteFile(String fileName) {final OSS ossClient = new OSSClientBuilder().build(properties.getEndpoint(), properties.getAccessKeyId(), properties.getAccessKeySecret());try {ossClient.deleteObject(properties.getBucketName(), fileName);}catch (final Exception e) {log.error("{}", e.fillInStackTrace());}finally {ossClient.shutdown();}}/*** 判断文件是否存在* @author 溪云阁* @param fileName 文件名称* @return boolean*/public boolean doesObjectExist(String fileName) {final OSS ossClient = new OSSClientBuilder().build(properties.getEndpoint(), properties.getAccessKeyId(), properties.getAccessKeySecret());try {if (Strings.isEmpty(fileName)) {log.error("文件名不能为空");return false;} else {final boolean found = ossClient.doesObjectExist(properties.getBucketName(), fileName);return found;}}finally {if (ossClient != null) {ossClient.shutdown();}}}}
/*** 文件类型枚举* @author:溪云阁* @date:2020年5月16日*/public enum FileTypeEnum {IMG(1, "图片"),AUDIO(2, "音频"),VIDEO(3, "视频"),APP(4, "App包"),OTHER(5, "其他");private Integer code;private String message;FileTypeEnum(Integer code, String message) {this.code = code;this.message = message;}public Integer getCode() {return code;}public String getMessage() {return message;}}

整合springboot

     <!-- 公共组件:阿里oss工具包 --><dependency><groupId>com.boots</groupId><artifactId>module-boots-oss</artifactId><version>1.0.0.RELEASE</version></dependency><!-- 公共组件:swagger服务+入参出参+统一异常拦截 --><dependency><groupId>com.boots</groupId><artifactId>module-boots-api</artifactId><version>1.0.0.RELEASE</version></dependency>
#oss上bucket的名称
module.boots.oss.bucketName: "bucket-boots"
#阿里对应的访问id
module.boots.oss.accessKeyId: "阿里云的accessId"
#阿里对应的密钥
module.boots.oss.accessKeySecret: "阿里云的accessKey"
#oss对应的区域节点
module.boots.oss.endpoint: "http://oss-cn-shenzhen.aliyuncs.com"
#上传文件总的最大值
spring.servlet.multipart.max-request-size: 10MB
#单个文件的最大值
spring.servlet.multipart.max-file-size: 10MB
/*** 文件服务接口* @author:溪云阁* @date:2020年5月17日*/
@Api(tags = { "OSS服务:文件接口" })
@RestController
@RequestMapping("web/Oss")
public class OssController {@Autowiredprivate AliyunOssUtils ossUtils;/*** 上传文件* @author 溪云阁* @param files* @return List<String>*/@ApiOperation(value = "上传文件")@PostMapping(value = "/uploadFiles")@SneakyThrows(CommonRuntimeException.class)public List<String> uploadFiles(@RequestParam("files") MultipartFile[] files) {return ossUtils.uploadFile(files, "imgs", FileTypeEnum.IMG);}}

上传测试

搞定,这个5年45块钱,买的真开心

--END--

作者:@溪云阁

如需要源码,转发,关注后私信我。

部分图片或代码来源网络,如侵权请联系删除,谢谢!

最新文章

  1. 【本人秃顶程序员】使用Spring Cloud Stream和RabbitMQ实现事件驱动的微服务
  2. MySQL 误操作恢复表
  3. 程序员修炼之道:从小工到专家阅读笔记01
  4. 虚拟化运行[OpenStack] VMWare产品介绍
  5. OpenWRT开源项目论坛遭未授权访问,可被用于供应链攻击
  6. md5修改工具_【q001】如何校验文件的MD5
  7. 二维数组及以上维度的数组,本质是一维数组?(C语言)
  8. Android:日常学习笔记(7)———探究UI开发(4)
  9. visio2010安装
  10. (转载)100+个程序员开发必备参考手册(在线及下载)
  11. android清除图案锁 位置,安卓手机清除锁屏密码、锁屏图案的教程
  12. Lucas–Kanade(LK)光流算法详解
  13. 如何共享计算机网络,电脑怎么共享网络给手机上网
  14. [转载] 胡锡进:5000亿买一包爆米花 我不想让我的国家这样
  15. 用HTML+CSS简单做了张简历表格
  16. 用c语言验证5阶魔方矩阵,穷举法打印n阶魔方矩阵
  17. 基础设施即代码:一场变革即将到来
  18. 宝塔wordpress安装及使用(宝塔wordpress建站教程)
  19. HTML5 postMessage 和 onmessage API 详细应用
  20. python Process, Pipo进程池管道的理解

热门文章

  1. android 程序 读logo,制作Android启动Logo
  2. Centos设置自带中文输入法
  3. spark submit参数及调试
  4. 使用xmake配合arm-none-eabi-gcc构建stm32工程
  5. A股主板上市需要哪些条件
  6. php网站水印功能代码,php给图片添加水印实例代码
  7. App 瘦身最佳实践
  8. 一个超级好看的代刷网模板-增加诸多功能 增加用户体验
  9. 快速开平方根倒数算法(Fast inverse square root)的一点探究
  10. 前端学习笔记-9.1怎样注册亚马逊aws免费1年云服务器?