阿里云封装上传相关——(难度:简单,等级:1)

DBC 85 0
温馨提示

本大模块难度级别为:无、简单、低、一般、中、较高、高

配置文件——yml

阿里云封装上传相关——(难度:简单,等级:1)插图

#----------阿里云OSS配置--------------
aliyun:
  oss:
    endpoint: oss-cn-zhangjiakou.aliyuncs.com
    access-key-id: LAftAj
    access-key-secret: fBk3lq2kvQFo
    bucketname: my-cloud-dbc

依赖文件——pom

<!--阿里云oss-->
            <dependency>
                <groupId>com.aliyun.oss</groupId>
                <artifactId>aliyun-sdk-oss</artifactId>
                <version>${aliyun.oss.version}</version>
            </dependency>
<aliyun.oss.version>3.10.2</aliyun.oss.version>

配置类——OSSConfig

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
/**
 * @author DBC
 * @version 1.0.0
 * @date 2022年10月01日 17:39:48
 * @website dbc655.top
 */
@ConfigurationProperties(prefix = "aliyun.oss")
@Configuration
@Data
public class OSSConfig {

    private String endpoint;
    private String accessKeyId;
    private String accessKeySecret;
    private String bucketname;
}

控制层——Controller

    @Autowired
    private FileService fileService;
    /**
     * 文件上传 最大默认1M
     *  文件格式、拓展名等判断
     * @param file
     * @return
     */
    @PostMapping("upload")
    public JsonData uploadUserImg(@RequestPart("file")MultipartFile file){

        String result = fileService.uploadUserImg(file);

        return result !=null ? JsonData.buildSuccess(result):JsonData.buildResult(BizCodeEnum.FILE_UPLOAD_USER_IMG_FAIL);

    }

实现层——FileServiceImpl

package net.dbc.service.impl;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.PutObjectResult;
import lombok.extern.slf4j.Slf4j;

import net.dbc.config.OSSConfig;
import net.dbc.service.FileService;
import net.dbc.utils.CommonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
/**
 * @author DBC
 * @version 1.0.0
 * @date 2022年10月01日 17:42:19
 * @website dbc655.top
 */
@Service
@Slf4j
public class FileServiceImpl implements FileService {

    @Autowired
    private OSSConfig ossConfig;

    @Override
    public String uploadUserImg(MultipartFile file) {
        String bucketName = ossConfig.getBucketname();
        String endpoint = ossConfig.getEndpoint();
        String accessKeyId = ossConfig.getAccessKeyId();
        String accessKeySecret = ossConfig.getAccessKeySecret();

        //oss客户端构建
        OSS ossClient = new OSSClientBuilder().build(endpoint,accessKeyId,accessKeySecret);

        //获取文件原始名称 xxx.jpg
        String originalFilename = file.getOriginalFilename();

        //jdk8语法日期格式
        LocalDateTime ldt = LocalDateTime.now();
        DateTimeFormatter pattern = DateTimeFormatter.ofPattern("yyyy/MM/dd");

        //user/2022/12/12/sdsdwe/
        String folder = pattern.format(ldt);
        String fileName = CommonUtil.generateUUID();
        String extendsion = originalFilename.substring(originalFilename.lastIndexOf("."));

        //在oss上的bucket创建文件夹
        String newFilename = "user/"+folder+"/"+fileName+extendsion;

        try {
            PutObjectResult putObjectResult = ossClient.putObject(bucketName, newFilename, file.getInputStream());
            //拼装返回路径
            if(putObjectResult!=null){
                String imgUrl = "https://"+bucketName+"."+endpoint+"/"+newFilename;
                return imgUrl;
            }

        } catch (IOException e) {
            log.error("文件上传失败:{}",e.getMessage());
        }finally {
            ossClient.shutdown();
        }

        return null;
    }
}

发表评论 取消回复
表情 图片 链接 代码

分享