实现上传图片到阿里云OSS

依赖

java 复制代码
<!--阿里云OSS-->
<dependency>
    <groupId>com.aliyun.oss</groupId>
    <artifactId>aliyun-sdk-oss</artifactId>
    <version>3.15.1</version>
</dependency>

AliOssUtils工具类

java 复制代码
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属性类

java 复制代码
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配置文件

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

UploadController

java 复制代码
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;
    }
}
相关推荐
小张认为的测试5 分钟前
Liunx上Jenkins 持续集成 Java + Maven + TestNG + Allure + Rest-Assured 接口自动化项目
java·ci/cd·jenkins·maven·接口·testng
Channing Lewis33 分钟前
flask常见问答题
后端·python·flask
蘑菇丁34 分钟前
ansible批量生产kerberos票据,并批量分发到所有其他主机脚本
java·ide·eclipse
Channing Lewis35 分钟前
如何保护 Flask API 的安全性?
后端·python·flask
呼啦啦啦啦啦啦啦啦2 小时前
【Redis】持久化机制
java·redis·mybatis
我想学LINUX2 小时前
【2024年华为OD机试】 (A卷,100分)- 微服务的集成测试(JavaScript&Java & Python&C/C++)
java·c语言·javascript·python·华为od·微服务·集成测试
空の鱼7 小时前
java开发,IDEA转战VSCODE配置(mac)
java·vscode
!!!5258 小时前
日志技术-LogBack入门程序&Log配置文件&日志级别
spring boot
P7进阶路8 小时前
Tomcat异常日志中文乱码怎么解决
java·tomcat·firefox