实现上传图片到阿里云OSS

依赖



    com.aliyun.oss
    aliyun-sdk-oss
    3.15.1

 AliOssUtils工具类

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.UUID;

/**
 * @Description: 实现上传图片到阿里云OSS
 * @Author: 翰戈.summer
 * @Date: 2023/11/16
 * @Param:
 * @Return:
 */
@Component
@RequiredArgsConstructor
public class AliOssUtils {

    private final AliOssProperties aliOSSProperties;

    /**
     * @Description: 上传图片,并获取图片访问路径
     * @Author: 翰戈.summer
     * @Date: 2023/11/16
     * @Param: MultipartFile
     * @Return: String
     */
    public String upload(MultipartFile file) throws IOException {
        //获取配置信息
        String endpoint = aliOSSProperties.getEndpoint();
        String accessKeyId = aliOSSProperties.getAccessKeyId();
        String accessKeySecret = aliOSSProperties.getAccessKeySecret();
        String bucketName = aliOSSProperties.getBucketName();

        //获取上传图片的输入流
        InputStream inputStream = file.getInputStream();

        //避免图片覆盖
        String originalFilename = file.getOriginalFilename();
        String fileName = null;
        if (originalFilename != null) {
            fileName = UUID.randomUUID() + originalFilename.substring(originalFilename.lastIndexOf("."));
        }

        //上传图片到OSS
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        ossClient.putObject(bucketName, fileName, inputStream);

        //图片访问路径
        String url = endpoint.split("//")[0] + "//" + bucketName + "." + endpoint.split("//")[1] + "/" + fileName;
        //关闭ossClient
        ossClient.shutdown();
        return url; //返回图片访问路径
    }
}

AliOssProperties属性类

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

/**
 * @Description: 阿里云OSS配置属性类
 * @Author: 翰戈.summer
 * @Date: 2023/11/16
 * @Param:
 * @Return:
 */
@Data
@Component
@ConfigurationProperties(prefix = "upload.alioss")
public class AliOssProperties {
    //区域地址
    private String endpoint;

    //密钥id
    private String accessKeyId;

    //密钥密码
    private String accessKeySecret;

    //bucket名称
    private String bucketName;
}

 yml配置文件

upload:
  alioss:
    endpoint: #区域地址
    access-key-id: #密钥id
    access-key-secret: #密钥密码
    bucket-name: #bucket名称

UploadController

import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * @Description: 图片上传相关接口
 * @Author: 翰戈.summer
 * @Date: 2023/11/28
 * @Param:
 * @Return:
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/upload")
public class UploadController {

    private final AliOssUtils aliOssUtils;

    @PostMapping
    public String uploadImage(MultipartFile multipartFile) {
        String url;
        try {
            url = aliOssUtils.upload(multipartFile);
        } catch (Exception ex) {
            throw new RuntimeException("图片上传失败!");
        }
        return url;
    }
}

你可能感兴趣的:(其他内容,阿里云,spring,boot,java,后端)