SpringBoot - 阿里云OSS - 上传和删除

1,首先在 pom.xml 中加入maven依赖

 <!-- 阿里云oss -->
 <dependency>
     <groupId>com.aliyun.oss</groupId>
         <artifactId>aliyun-sdk-oss</artifactId>
         <version>3.10.2</version>
 </dependency>

 

2,在 application.yml 中配置 oss 的相关属性

aliyun:
  oss:
    endpoint : "https://oss-cn-beijing.aliyuncs.com"
    accessKeyId : "你的。。。"
    accessKeySecret : "你的。。。"
    bucketName : "你的。。。"

 

3,将 oss 的属性注入到实体类 OssInstance.java , 方便之后的同一调用

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

@Component
@Data
@ConfigurationProperties(prefix = "aliyun.oss")
public class OssInstance {
    private String endpoint;
    private String accessKeyId;
    private String accessKeySecret;
    private String bucketName;
}

 

4,编写 oss 的功能接口   OssService.java

import java.io.File;

public interface OssService {

    //如果OSS文件存在,则上传的数据会覆盖该文件的内容;如果OSS文件不存在,则会新建该文件。
    //如果修改文件,则传入之前的包含文件后缀的路径即可
    /**
     * 阿里云oss文件上传公共方法  (选择单个上传,上传完之后即可返回上传的地址)
     * @param file 上传的文件对象
     * @param filePath 上传到oss的文件地址。包括文件的后缀
     * @return 上传后存储在oss上的路径。 便于之后的访问和删除
     */
    String upload(File file, String filePath);

    /**
     * 阿里云oss文件删除
     * @param filepath
     * @return
     */
    public String delete(String filepath);
}

 

5, 实现 oss 功能的接口

import cn.hutool.core.io.FileUtil;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.example.demo.service.OssService;
import com.example.demo.utils.OssInstance;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.InputStream;

@Service
public class OssServiceImpl implements OssService {

    @Autowired
    private OssInstance ossInstance;

    @Override
    public String upload(File file, String filePath) {
        String endpoint = ossInstance.getEndpoint();
        String accessKeyId = ossInstance.getAccessKeyId();
        String accessKeySecret = ossInstance.getAccessKeySecret();
        String bucketName = ossInstance.getBucketName();

        // 文件流
        InputStream inputStream = FileUtil.getInputStream(file);


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

        try {
            // 创建PutObject请求。
            ossClient.putObject(bucketName, filePath, inputStream);
        } catch (Exception e) {
            System.out.println(e.getMessage());
            return "上传失败";
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
        return filePath;
    }

    @Override
    public String delete(String filepath) {
        String endpoint = ossInstance.getEndpoint();
        String accessKeyId = ossInstance.getAccessKeyId();
        String accessKeySecret = ossInstance.getAccessKeySecret();
        String bucketName = ossInstance.getBucketName();

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

        try {
            // 删除文件。
            ossClient.deleteObject(bucketName, filepath);
        } catch (Exception e) {
            System.out.println(e.getMessage());
            return "删除失败";
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
        return "删除成功";
    }

}

 

 


 

 

结束 ,自己写个Controller 直接调用 , 或者在其他需要使用到oss的服务直接注入 OssService 调用就可以 。       

 

posted @ 2022-03-06 12:18  武卡卡  阅读(613)  评论(0编辑  收藏  举报