1.Maven导包

      <!--阿里云核心API-->
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>aliyun-java-sdk-core</artifactId>
            <version>4.5.3</version>
        </dependency>
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>dysmsapi20170525</artifactId>
            <version>2.0.1</version>
        </dependency>
        <!--阿里云oss存储API-->
        <dependency>
            <groupId>com.aliyun.oss</groupId>
            <artifactId>aliyun-sdk-oss</artifactId>
            <version>3.10.2</version>
        </dependency>

2.封装阿里云oss对象储存

public class OssTemplate{

    private String endpoint = "endpoint";
    private String accessKeyId = "accessKeyId";
    private String accessKeySecret = "accessKeySecret ";
    private String bucketName = "bucketName";
    private String url = "url"; //外网访问域名

    public String upLoad(String fileName, FileInputStream inputStream) throws FileNotFoundException {

        //目录文件名称格式  例: 2022/05/01/UUID.xxx
        String filename = new SimpleDateFormat("yyyy/MM/dd").format(new Date())
            + "/" + UUID.randomUUID().toString() + fileName.substring(fileName.lastIndexOf("."));

        String endpoint = this.endpoint;
        String accessKeyId = this.accessKeyId;
        String accessKeySecret = this.accessKeySecret;
        String bucketName = this.bucketName;

        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

        // 填写Bucket名称和Object完整路径。Object完整路径中不能包含Bucket名称。
        ossClient.putObject(bucketName, filename, inputStream);

        // 关闭OSSClient。
        ossClient.shutdown();

        String url = this.url + filename;
        return url;
    }


    public boolean delete(String filePath){
        String endpoint = this.endpoint;
        String accessKeyId = this.accessKeyId;
        String accessKeySecret = this.accessKeySecret;
        String bucketName = this.bucketName;
        // 填写Object完整路径,例如exampledir/exampleobject.txt。Object完整路径中不能包含Bucket名称。
        String objectName = filePath;

        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

        try {
            ossClient.deleteObject(bucketName, objectName);
            return true;
        }catch (Exception e){
            return false;
        }finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
    }
}

3.加载@Bean

@Bean
public OssTemplate ossTemplate() {
    return new OssTemplate();
}

4.使用方法

@Autowired
private OssTemplate ossTemplatel;

@PostMapping("/upload")
public ResponseEntity uploadImgAndGetUrl(MultipartFile file) throws IOException {
    String filename = file.getOriginalFilename();
    FileInputStream inputStream = (FileInputStream) file.getInputStream();
    String url = ossTemplate.upLoad(filename, inputStream);

    return ResponseEntity.ok(url);
}
最后修改:2023 年 05 月 02 日
如果觉得我的文章对你有用,请随意赞赏