一. 配置内容

1.配置pom.xml

        <!--aliyun-->
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>aliyun-java-sdk-core</artifactId>
            <version>4.3.3</version>
        </dependency>
        <dependency>
            <groupId>com.aliyun.oss</groupId>
            <artifactId>aliyun-sdk-oss</artifactId>
            <version>3.10.2</version>
        </dependency>

2.配置application.properties

#阿里云 OSS
#不同的服务器,地址不同
aliyun.oss.file.endpoint={Endpoint(地域节点)}
aliyun.oss.file.keyid={自己的AccessKey ID}
aliyun.oss.file.keysecret={自己的keysecret}
#bucket可以在控制台创建,也可以使用java代码创建
aliyun.oss.file.bucketname={Bucket 名称}

3.新建oss配置文件类注入springboot

package com.carolin_violet.travel_system.utils;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * @ClassName OssPropertiesUtils
 * @Description 阿里云oss相关配置
 * @Author zj
 * @Date 2022/5/23 10:55
 * @Version 1.0
 */
@Component
public class OssPropertiesUtils implements InitializingBean {
    @Value("${aliyun.oss.file.endpoint}")
    private String endpoint;

    @Value("${aliyun.oss.file.keyid}")
    private String keyId;

    @Value("${aliyun.oss.file.keysecret}")
    private String keySecret;

    @Value("${aliyun.oss.file.bucketname}")
    private String bucketName;

    public static String END_POINT;
    public static String KEY_ID;
    public static String KEY_SECRET;
    public static String BUCKET_NAME;

    @Override
    public void afterPropertiesSet() throws Exception {
        END_POINT = endpoint;
        KEY_ID = keyId;
        KEY_SECRET = keySecret;
        BUCKET_NAME = bucketName;
    }
}

二.上传一张图片

// 上传图片至阿里云并返回路径
    @Override
    public String uploadFileAvatar(MultipartFile file) {

        String endpoint = OssPropertiesUtils.END_POINT;
        String accessKeyId = OssPropertiesUtils.KEY_ID;
        String accessKeySecret = OssPropertiesUtils.KEY_SECRET;
        String bucketName = OssPropertiesUtils.BUCKET_NAME;

        try {
            // 创建OSSClient实例。
            OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
            // 获取上传文件输入流
            InputStream inputStream = file.getInputStream();
            // 获取文件名字
            String fileName = file.getOriginalFilename();
            String uuid = UUID.randomUUID().toString().replaceAll("-", "");
            fileName = uuid + fileName;

	    // 自定义的路径
            fileName = "travel_GaoChun/" + fileName;

            // 创建PutObject请求。
            ossClient.putObject(bucketName, fileName, inputStream);
            ossClient.shutdown();

            // 把上传之后文件路径返回
            String url = "https://" + bucketName + "." + endpoint + "/" + fileName;
            return url;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

三.删除图片

1. 删除一张图片

// picture为图片url路径
 @Override
    public void removePhoto(String picture) {
        String endpoint = OssPropertiesUtils.END_POINT;
        String accessKeyId = OssPropertiesUtils.KEY_ID;
        String accessKeySecret = OssPropertiesUtils.KEY_SECRET;
        String bucketName = OssPropertiesUtils.BUCKET_NAME;

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

        // 将完整的url替换为oss存储路径并删除
        ossClient.deleteObject(bucketName, picture.replace("https://{自己的路径}.aliyuncs.com/", ""));

        ossClient.shutdown();
    }

2. 删除多张图片

    @Override
    public void removePhotos(List keys) {

        String endpoint = OssPropertiesUtils.END_POINT;
        String accessKeyId = OssPropertiesUtils.KEY_ID;
        String accessKeySecret = OssPropertiesUtils.KEY_SECRET;
        String bucketName = OssPropertiesUtils.BUCKET_NAME;

        // 遍历图片列表,切割字符串,将所有图片路径存入keys
        // 也是将完整的url替换为oss存储路径
        List<String> keys2 = new ArrayList<>();
        for (String key : kjeys) {
            String[] arr = key.split("/");
            String url = arr[arr.length-2] + "/" + arr[arr.length-1];
            keys2.add(url);
        }

        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        // 删除请求
        DeleteObjectsRequest deleteRequest = new DeleteObjectsRequest(bucketName);
        // 赋值需要删除的文件
        deleteRequest.setKeys(keys2);
        //deleteRequest.setEncodingType(URL_ENCODING);
        // 调用删除
        DeleteObjectsResult deleteObjectsResult = ossClient.deleteObjects(deleteRequest);
        // 获取删除的文件
        List<String> deletedObjects = deleteObjectsResult.getDeletedObjects();
        // 关闭OSSClient。
        ossClient.shutdown();

Q.E.D.


一个二次元web开发咸鱼