SpringBoot整合阿里云OSS对象存储服务的实现

 更新时间:2020年08月10日 10:57:30   作者:Mistra丶  
这篇文章主要介绍了SpringBoot整合阿里云OSS对象存储服务的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

今天来整合一下SpringBoot和阿里云OSS对象存储服务。

一、配置OSS服务

先在阿里云开通对象存储服务,拿到AccessKeyId、AccessKeySecret。


创建你的bucket(存储空间),相当于一个一个的文件夹目录。按业务需求分类存储你的文件,图片,音频,app包等等。创建bucket是要选择该bucket的权限,私有,公共读,公共读写,按需求选择。创建bucket时对应的endpoint要记住,上传文件需要用到。


可以配置bucket的生命周期,比如说某些文件有过期时间的,可以配置一下。

二、代码实现

引入依赖包

<dependency>
  <groupId>com.aliyun.oss</groupId>
  <artifactId>aliyun-sdk-oss</artifactId>
  <version>2.8.3</version>
</dependency>

配置文件application.yml

aliyun-oss:
 #bucket名称
 bucketApp: xxx-app
 domainApp: https://xxx-app.oss-cn-shenzhen.aliyuncs.com/
 region: oss-cn-shenzhen
 endpoint : https://oss-cn-shenzhen.aliyuncs.com
 accessKeyId: 你的accessKeyId
 accessKeySecret: 你的accessKeySecret

对应上面配置文件的properties类

package com.example.file.config;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Component
@ConfigurationProperties(prefix = "aliyun-oss")
@Data
public class AliyunOSSProperties {

  /**
   * 服务器地点
   */
  private String region;
  /**
   * 服务器地址
   */
  private String endpoint;
  /**
   * OSS身份id
   */
  private String accessKeyId;
  /**
   * 身份密钥
   */
  private String accessKeySecret;

  /**
   * App文件bucketName
   */
  private String bucketApp;
  /**
   * App包文件地址前缀
   */
  private String domainApp;
}

上传文件工具类

package com.example.file.utils;

import com.aliyun.oss.OSSClient;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.ObjectMetadata;
import com.aliyun.oss.model.PutObjectResult;
import com.example.common.exception.BusinessErrorCode;
import com.example.common.exception.BusinessException;
import com.example.common.utils.FileIdUtils;
import com.example.file.config.AliyunOSSProperties;
import com.example.file.config.FileTypeEnum;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.Validate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

@Component
public class AliyunOSSUtil {

  @Autowired
  private AliyunOSSProperties aliyunOSSProperties;

  private static Logger logger = LoggerFactory.getLogger(AliyunOSSUtil.class);

  /**
   * 文件不存在
   */
  private final String NO_SUCH_KEY = "NoSuchKey";
  /**
   * 存储空间不存在
   */
  private final String NO_SUCH_BUCKET = "NoSuchBucket";

  /**
   * 上传文件到阿里云 OSS 服务器
   *
   * @param files
   * @param fileTypeEnum
   * @param bucketName
   * @param storagePath
   * @return
   */
  public List<String> uploadFile(MultipartFile[] files, FileTypeEnum fileTypeEnum, String bucketName, String storagePath, String prefix) {
    //创建OSSClient实例
    OSSClient ossClient = new OSSClient(aliyunOSSProperties.getEndpoint(), aliyunOSSProperties.getAccessKeyId(), aliyunOSSProperties.getAccessKeySecret());
    List<String> fileIds = new ArrayList<>();
    try {
      for (MultipartFile file : files) {
      		//创建一个唯一的文件名,类似于id,就是保存在OSS服务器上文件的文件名(自定义文件名)
        String fileName = FileIdUtils.creater(fileTypeEnum.getCode());
        InputStream inputStream = file.getInputStream();
        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 = StringUtils.isNotBlank(storagePath) ? storagePath + "/" + fileName : fileName;
        //上传文件
        PutObjectResult result = ossClient.putObject(bucketName, fileName, inputStream, objectMetadata);
        logger.info("Aliyun OSS AliyunOSSUtil.uploadFileToAliyunOSS,result:{}", result);
        fileIds.add(prefix + fileName);
      }
    } catch (IOException e) {
      logger.error("Aliyun OSS AliyunOSSUtil.uploadFileToAliyunOSS fail,reason:{}", e);
    } finally {
      ossClient.shutdown();
    }
    return fileIds;
  }

  /**
   * 删除文件
   *
   * @param fileName
   * @param bucketName
   */
  public void deleteFile(String fileName, String bucketName) {
    OSSClient ossClient = new OSSClient(aliyunOSSProperties.getEndpoint(), aliyunOSSProperties.getAccessKeyId(), aliyunOSSProperties.getAccessKeySecret());
    ossClient.deleteObject(bucketName, fileName);
    shutdown(ossClient);
  }

  /**
   * 根据图片全路径删除就图片
   *
   * @param imgUrl   图片全路径
   * @param bucketName 存储路径
   */
  public void delImg(String imgUrl, String bucketName) {
    if (StringUtils.isBlank(imgUrl)) {
      return;
    }
    //问号
    int index = imgUrl.indexOf('?');
    if (index != -1) {
      imgUrl = imgUrl.substring(0, index);
    }
    int slashIndex = imgUrl.lastIndexOf('/');
    String fileId = imgUrl.substring(slashIndex + 1);
    OSSClient ossClient = new OSSClient(aliyunOSSProperties.getEndpoint(), aliyunOSSProperties.getAccessKeyId(), aliyunOSSProperties.getAccessKeySecret());
    ossClient.deleteObject(bucketName, fileId);
    shutdown(ossClient);
  }

  /**
   * 判断文件是否存在
   *
   * @param fileName  文件名称
   * @param bucketName 文件储存空间名称
   * @return true:存在,false:不存在
   */
  public boolean doesObjectExist(String fileName, String bucketName) {
    Validate.notEmpty(fileName, "fileName can be not empty");
    Validate.notEmpty(bucketName, "bucketName can be not empty");
    OSSClient ossClient = new OSSClient(aliyunOSSProperties.getEndpoint(), aliyunOSSProperties.getAccessKeyId(), aliyunOSSProperties.getAccessKeySecret());
    try {
      boolean found = ossClient.doesObjectExist(bucketName, fileName);
      return found;
    } finally {
      shutdown(ossClient);
    }

  }

  /**
   * 复制文件
   *
   * @param fileName       源文件名称
   * @param bucketName      源储存空间名称
   * @param destinationBucketName 目标储存空间名称
   * @param destinationObjectName 目标文件名称
   */
  public void ossCopyObject(String fileName, String bucketName, String destinationBucketName, String destinationObjectName) {
    Validate.notEmpty(fileName, "fileName can be not empty");
    Validate.notEmpty(bucketName, "bucketName can be not empty");
    Validate.notEmpty(destinationBucketName, "destinationBucketName can be not empty");
    Validate.notEmpty(destinationObjectName, "destinationObjectName can be not empty");
    OSSClient ossClient = new OSSClient(aliyunOSSProperties.getEndpoint(), aliyunOSSProperties.getAccessKeyId(), aliyunOSSProperties.getAccessKeySecret());
    // 拷贝文件。
    try {
      ossClient.copyObject(bucketName, fileName, destinationBucketName, destinationObjectName);
    } catch (OSSException oe) {
      logger.error("errorCode:{},Message:{},requestID:{}", oe.getErrorCode(), oe.getMessage(), oe.getRequestId());
      if (oe.getErrorCode().equals(NO_SUCH_KEY)) {
        throw new BusinessException(BusinessErrorCode.NO_SUCH_KEY);
      } else if (oe.getErrorCode().equals(NO_SUCH_BUCKET)) {
        throw new BusinessException(BusinessErrorCode.NO_SUCH_BUCKET);
      } else {
        throw new BusinessException(BusinessErrorCode.FAIL);
      }
    } finally {
      shutdown(ossClient);
    }
  }

  /**
   * 关闭oos
   *
   * @param ossClient ossClient
   */
  private void shutdown(OSSClient ossClient) {
    ossClient.shutdown();
  }
}

文件类型枚举

package com.example.file.config;

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;
  }

}

调用工具类上传文件

@Override
  public List<String> uploadImg(MultipartFile[] files) {
    if (files == null) {
      throw new BusinessException(BusinessErrorCode.OPT_UPLOAD_FILE);
    }
    try {
      return aliyunOSSUtil.uploadFile(files, FileTypeEnum.IMG, aliyunOSSProperties.getBucketProduct(), null, aliyunOSSProperties.getDomainProduct());
    } catch (Exception e) {
      logger.error("uploadImg error e:{}", e);
      throw new BusinessException(BusinessErrorCode.UPLOAD_FAIL);
    }
  }

返回的List是文件上传之后文件的文件名集合。
到此就整合完成了。可以登录OSS控制台查看对应的文件。更多相关SpringBoot整合阿里云OSS内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Java陷阱之assert关键字详解

    Java陷阱之assert关键字详解

    这篇文章详细介绍了Java陷阱之assert关键字,有需要的朋友可以参考一下
    2013-09-09
  • 三种简单排序算法(使用java实现)

    三种简单排序算法(使用java实现)

    下面小编就为大家带来一篇三种简单排序算法(使用java实现)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2016-07-07
  • Java实战之小蜜蜂扩音器网上商城系统的实现

    Java实战之小蜜蜂扩音器网上商城系统的实现

    这篇文章主要介绍了如何利用Java实现简单的小蜜蜂扩音器网上商城系统,文中采用到的技术有JSP、Servlet 、JDBC、Ajax等,感兴趣的可以动手试一试
    2022-03-03
  • 精通Java List 按字段提取对象

    精通Java List 按字段提取对象

    这篇文章主要介绍了精通Java List 按字段提取对象的相关资料,需要的朋友可以参考下
    2023-11-11
  • Java集合中contains方法的效率对比分析

    Java集合中contains方法的效率对比分析

    这篇文章主要介绍了Java集合中contains方法的效率对比分析,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-05-05
  • idea搭建可运行Servlet的Web项目

    idea搭建可运行Servlet的Web项目

    在网上看到一篇很详细的 intelliJ IDEA 创建web项目并简单部署servlet的图文教程,今天自己也配置一下,留个笔记,感兴趣的可以了解一下
    2021-06-06
  • 浅谈Spring Boot 2.0迁移指南主要注意点

    浅谈Spring Boot 2.0迁移指南主要注意点

    Spring官方的Spring Boot 2变动指南,主要是帮助您将应用程序迁移到Spring Boot 2.0,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-10-10
  • 关于@JSONField和@JsonFormat的使用区别说明

    关于@JSONField和@JsonFormat的使用区别说明

    这篇文章主要介绍了关于@JSONField 和 @JsonFormat的区别说明,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-11-11
  • 浅谈Java的两种多线程实现方式

    浅谈Java的两种多线程实现方式

    本篇文章主要介绍了浅谈Java的两种多线程实现方式,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-08-08
  • 关于Java中byte[] 和 String互相转换问题

    关于Java中byte[] 和 String互相转换问题

    这篇文章主要介绍了Java中byte[] 和 String互相转换问题,通过用例给大家介绍了通过String类将String转换成byte[]或者byte[]转换成String,具体实例代码跟随小编一起看看吧
    2022-01-01

最新评论