实现上传图片到阿里云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;
    }
}
相关推荐
IT_陈寒16 小时前
React 18实战:7个被低估的Hooks技巧让你的开发效率提升50%
前端·人工智能·后端
星星电灯猴17 小时前
Thor 抓包工具详解 iOS 抓包方法、HTTPS 抓包难点与常见网络调试工具对比
后端
姓王者17 小时前
可能解决Tauri多窗口应用阻塞问题
后端
RoyLin17 小时前
TypeScript设计模式:抽象工厂模式
前端·后端·typescript
没逻辑17 小时前
Post-Quantum HTTPS:未来的安全通信架构
后端·安全
间彧17 小时前
SimpleDateFormat既然不推荐使用,为什么java 8+中不删除此类
java
云中雾丽17 小时前
Redis 使用记录
后端
间彧17 小时前
DateTimeFormatter相比SimpleDateFormat在性能上有何差异?
java
间彧18 小时前
为什么说SimpleDateFormat是经典的线程不安全类
java