实现上传图片到阿里云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;
    }
}
相关推荐
全栈老石14 分钟前
Python 异步生存手册:给被 JS async/await 宠坏的全栈工程师
后端·python
大模型玩家七七16 分钟前
梯度累积真的省显存吗?它换走的是什么成本
java·javascript·数据库·人工智能·深度学习
space621232721 分钟前
在SpringBoot项目中集成MongoDB
spring boot·后端·mongodb
CodeToGym1 小时前
【Java 办公自动化】Apache POI 入门:手把手教你实现 Excel 导入与导出
java·apache·excel
凡人叶枫1 小时前
C++中智能指针详解(Linux实战版)| 彻底解决内存泄漏,新手也能吃透
java·linux·c语言·开发语言·c++·嵌入式开发
Tony Bai1 小时前
再见,丑陋的 container/heap!Go 泛型堆 heap/v2 提案解析
开发语言·后端·golang
JMchen1231 小时前
Android后台服务与网络保活:WorkManager的实战应用
android·java·网络·kotlin·php·android-studio
阔皮大师1 小时前
INote轻量文本编辑器
java·javascript·python·c#
寻找奶酪的mouse1 小时前
30岁技术人对职业和生活的思考
前端·后端·年终总结
小法师爱分享2 小时前
StickyNotes,简单便签超实用
java·python