SpringBoot 阿里Oss 文件上传配置与开发

185 阅读2分钟

上目录结构

1.png

  1. 引入阿里依赖和 日期依赖

2.png

  1. 配置文件

3.png

文件内容如下

# 端口号
server.port=8002

#服务名
spring.application.name=service-oss

# 环境设置 dev test prod
spring.profiles.active=dev


#阿里云 OSS
aliyun.oss.file.endpoint=
aliyun.oss.file.keyid=
aliyun.oss.file.keysecret=
aliyun.oss.file.buketname=

2. 创建 获取配置文件的工具类

4.png

以下是文件内容

package com.school.oss.utils;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * @User: Json
 * @Date: 2021/12/5
 **/

//当项目已启动, spring接口, spring加载之后 执行接口(InitializingBean)中的一个方法 (afterPropertiesSet)
// 这样配置后 项目启动后 会自动把这些配置文件属性 赋值为常量 
// 因为这些属性定义的都是 private 的 如果不这样设计 后续是使用不了的
@Component
public class ConstantProperties implements InitializingBean {

    //读取配置文件
    @Value("${aliyun.oss.file.endpoint}")
    private String endpoint;

    @Value("${aliyun.oss.file.keyid}")
    private String keyid;

    @Value("${aliyun.oss.file.keysecret}")
    private String keysecret;

    @Value("${aliyun.oss.file.buketname}")
    private String buketname;


    //定义公开的静态的常量
    public static String END_POINT;

    public static String KEY_ID;

    public static String KEY_SECRET;

    public static String BUKET_NAME;

    //在 这个重写方法中 设置常量 别的类中进行调用
    @Override
    public void afterPropertiesSet() throws Exception {
        END_POINT=endpoint;
        KEY_ID=keyid;
        KEY_SECRET=keysecret;
        BUKET_NAME=buketname;

    }
}

3. 在实现类中调用oss 进行文件上传

5.png

以下是文件内容

package com.school.oss.service.impl;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.school.oss.service.OssService;
import com.school.oss.utils.ConstantProperties;
import org.joda.time.DateTime;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;

/**
 * @User: Json
 * @Date: 2021/12/5
 **/
@Service
public class OssImpl implements OssService {


    //上传文件到oss
    @Override
    public String uploadFile(MultipartFile file) {
        // yourEndpoint填写Bucket所在地域对应的Endpoint。以华东1(杭州)为例,Endpoint填写为https://oss-cn-hangzhou.aliyuncs.com。
        String endpoint = ConstantProperties.END_POINT;
// 阿里云账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM用户进行API访问或日常运维,请登录RAM控制台创建RAM用户。
        String accessKeyId =  ConstantProperties.KEY_ID;
        String accessKeySecret =  ConstantProperties.KEY_SECRET;
        String bucketName = ConstantProperties.BUKET_NAME;

         try {
             //获取当前日期  joda.time 工具类 //防止 命名冲突  分类
             String DatePath = new DateTime().toString("yyyy/MM/dd");
             String fileName=file.getOriginalFilename();
             fileName = DatePath+fileName;

             // 创建OSSClient实例。
             OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

           // 填写本地文件的完整路径。如果未指定本地路径,则默认从示例程序所属项目对应本地路径中上传文件流。
             InputStream inputStream = file.getInputStream();
              // 依次填写Bucket名称(例如examplebucket)和Object完整路径(例如exampledir/exampleobject.txt)。Object完整路径中不能包含Bucket名称。
             ossClient.putObject(bucketName, fileName, inputStream);

           // 关闭OSSClient。
             ossClient.shutdown();
             //返回路径
             String url="https://"+bucketName+"."+endpoint+"/"+fileName;
             return url;
         }catch (Exception e){
             e.printStackTrace();
             return null;
         }


    }
}

4. 重启 oss 模块 进行测试

6.png