阿里云OSS对象存储

  • Post author:
  • Post category:其他


一、导入阿里云依赖

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

        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>aliyun-java-sdk-sts</artifactId>
            <version>3.0.0</version>
        </dependency>

        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>aliyun-java-sdk-core</artifactId>
            <version>4.4.6</version>
        </dependency>

二、OSS配置

import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Component;

/**
 * oss 配置
 *
 * @author mirror
 * @date 2020-11-25
 */
@Component
public class OssConfig {


    /**
     * ak
     */
    public static String ACCESS_KEY_ID;

    @Value("${config.oss.access_key_id}")
    public void setAccessKeyId(String accessKeyId){
        ACCESS_KEY_ID = accessKeyId;
    }
    /**
     * sk
     */
    public static String SECRET_ACCESS_KEY;

    @Value("${config.oss.secret_access_key}")
    public void setSecretAccessKey(String secretAccessKey){
        SECRET_ACCESS_KEY = secretAccessKey;
    }

    /**
     * 角色ARN
     */
    public static String ROLE_ARN;

    @Value("${config.oss.role_arn}")
    public void setRoleArn(String roleArn){
        ROLE_ARN = roleArn;
    }

    /**
     * 角色名称
     */
    public static String ROLE_SESSION_NAME;

    @Value("${config.oss.role_session_name}")
    public void setRoleSessionName(String roleSessionName){
        ROLE_SESSION_NAME = roleSessionName;
    }


    /**
     * 桶
     */
    public static final String BUCKET_NAME_PRIVATE = "BUCKET_NAME_PRIVATE";


    /**
     * endpoint
     */
    public static final String END_POINT = "END_POINT";

    /**
     * 区域ID
     */
    public static final String REGION_ID = "REGION_ID";

    /**
     * ASC ID
     */
    public static final String ASC_REGION_ID = "ASC_REGION_ID";

}

代理配置

/**
 * 代理配置
 *
 * @author mirror
 * @date 2020-12-17
 */
public class ProxyConfig {

    public static final String HOSTNAME = "HOSTNAME";
    public static final Integer PORT = 8080;
    public static final String PROXY_USERNAME = "PROXY_USERNAME";
    public static final String PROXY_PASSWORD = "PROXY_PASSWORD";

}

三、OSSClient配置

import com.aliyun.oss.ClientBuilderConfiguration;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.http.HttpClientConfig;
import com.aliyuncs.profile.DefaultProfile;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.HttpClientBuilder;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import static com.neusoft.evergrande.constant.aliyun.OssConfig.*;
import static com.neusoft.evergrande.constant.aliyun.ProxyConfig.*;


/**
 *
 * @author mirror
 */
@Slf4j
@Component
public class OssClientConfig {

    @Value("${rest.proxy}")
    Boolean isProxy;


    @Bean
    public OSS getOssClient(){

        OSS ossClient = null;

        if(isProxy){
            log.info("========  >>>  代理启用成功");
            ClientBuilderConfiguration config = new ClientBuilderConfiguration();
            config.setProxyHost(HOSTNAME);
            config.setProxyPort(PORT);
            config.setProxyUsername(PROXY_USERNAME);
            config.setProxyPassword(PROXY_PASSWORD);

            ossClient = new OSSClientBuilder().build(END_POINT, ACCESS_KEY_ID, SECRET_ACCESS_KEY, config);

        }else{
            ossClient = new OSSClientBuilder().build(END_POINT, ACCESS_KEY_ID, SECRET_ACCESS_KEY);

        }

        return ossClient;
    }

    @Bean
    public IAcsClient getAcsClient(){

        DefaultProfile profile = DefaultProfile.getProfile(REGION_ID, ACCESS_KEY_ID, SECRET_ACCESS_KEY);

        if(isProxy){
            log.info("========  >>>  代理启用成功");
            // 配置HTTP客户端
            HttpClientConfig config = HttpClientConfig.getDefault();
            BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
            credentialsProvider.setCredentials(new AuthScope(HOSTNAME, PORT),
                    new UsernamePasswordCredentials(PROXY_USERNAME, PROXY_PASSWORD));
            config.setExtParam("apache.httpclient.builder", HttpClientBuilder.create()
                    .setProxy(new HttpHost(HOSTNAME, PORT))
                    .setDefaultCredentialsProvider(credentialsProvider));


            profile.setHttpClientConfig(config);
            profile.setHttpClientConfig(config);

        }

        return  new DefaultAcsClient(profile);
    }




}

四、业务编写

import com.aliyun.oss.*;
import com.aliyun.oss.common.comm.ResponseMessage;
import com.aliyun.oss.model.*;
import com.neusoft.evergrande.common.response.Response;
import com.neusoft.evergrande.config.aliyun.OssClientConfig;
import com.neusoft.evergrande.pojo.vo.aliyun.OssKeyVO;
import com.neusoft.evergrande.service.aliyun.AliyunService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;

import static com.neusoft.evergrande.constant.aliyun.OssConfig.BUCKET_NAME_PRIVATE;
import static com.neusoft.evergrande.constant.aliyun.OssConfig.END_POINT;
import static com.neusoft.evergrande.constant.aliyun.ProxyConfig.*;
import static com.neusoft.evergrande.constant.aliyun.STSOssConfig.*;

/**
 * @author mirror
 */
@Api(tags = {"阿里云OSS服务"})
@Slf4j
@RestController
@RequestMapping("/aliyun/oss")
public class ALiController {

    @Resource
    private OssClientConfig clientConfig;

    @Resource
    private AliyunService aliyunService;


    @ApiOperation("获取临时AK/SK")
    @PostMapping("/getTemporaryKey")
    public Response<?> getTemporaryKey() {
        String userId = "110";

        if (StringUtils.isBlank(userId)) {
            log.info("=======>>>  用户不存在 ");
            return new Response<>("400", "用户不存在");
        }
        OssKeyVO ossKeyVO = aliyunService.getTemporaryKey(userId);
        return Response.success(ossKeyVO);
    }


    /**
     * 使用临时身份上传
     *
     * @param file       文件
     * @param path       填写Object完整路径,完整路径中不能包含Bucket名称,例如exampledir/exampleobject.txt。
     * @param bucketName 填写Bucket名称,例如examplebucket。
     * @return 操作结果
     */
    @ApiOperation("使用临时身份上传")
    @PostMapping("/uploadByAssRole")
    public Response<?> uploadByAssRole(@RequestParam("file") MultipartFile file,
                                       @RequestParam String path,
                                       @RequestParam String bucketName) {


        ClientBuilderConfiguration config = new ClientBuilderConfiguration();
        config.setProxyHost(HOSTNAME);
        config.setProxyPort(PORT);
        config.setProxyUsername(PROXY_USERNAME);
        config.setProxyPassword(PROXY_PASSWORD);


        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(END_POINT, STS_ACCESS_KEY_ID, STS_SECRET_ACCESS_KEY, SECURITY_TOKEN, config);

        try {
            // 创建PutObjectRequest对象。
            PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, path, file.getInputStream());
            // 如果需要上传时设置存储类型和访问权限,请参考以下示例代码。
            // ObjectMetadata metadata = new ObjectMetadata();
            // metadata.setHeader(OSSHeaders.OSS_STORAGE_CLASS, StorageClass.Standard.toString());
            // metadata.setObjectAcl(CannedAccessControlList.Private);
            // putObjectRequest.setMetadata(metadata);

            // 上传文件。
            ossClient.putObject(putObjectRequest);
        } catch (OSSException oe) {
            System.out.println("Caught an OSSException, which means your request made it to OSS, "
                    + "but was rejected with an error response for some reason.");
            System.out.println("Error Message:" + oe.getErrorMessage());
            System.out.println("Error Code:" + oe.getErrorCode());
            System.out.println("Request ID:" + oe.getRequestId());
            System.out.println("Host ID:" + oe.getHostId());
            return new Response<>(oe.getErrorCode(), oe.getErrorMessage());
        } catch (Exception ce) {
            System.out.println("Caught an ClientException, which means the client encountered "
                    + "a serious internal problem while trying to communicate with OSS, "
                    + "such as not being able to access the network.");
            System.out.println("Error Message:" + ce.getMessage());
            return new Response<>("500", "未知异常");
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
        return new Response<>("200", "成功");
    }

    @ApiOperation("获取url")
    @PostMapping("/getDownloadUrl")
    public Response<?> getDownloadUrl(@RequestParam String bucketName, @RequestParam String path) {

        return aliyunService.getDownloadUrl(bucketName, path);

    }

    @ApiOperation("删除OSS中的文件")
    @PostMapping("/deleteOssFile")
    public Response<?> deleteOssFile(@RequestParam String path) {

        return aliyunService.deleteOssFile(BUCKET_NAME_PRIVATE, path);

    }


    /**
     * 上传
     *
     * @param file 文件
     * @param path 填写Object完整路径,完整路径中不能包含Bucket名称,例如exampledir/exampleobject.txt。
     * @return
     */
    @PostMapping("/upload")
    public Response<?> upload(@RequestParam MultipartFile file, @RequestParam String path) {

        log.info("========>>>  {} ", file);
        // 填写Object完整路径,完整路径中不能包含Bucket名称,例如exampledir/exampleobject.txt。
        String objectName = "demo/demo02.jpg";
        // 填写本地文件的完整路径,例如D:\\localpath\\examplefile.txt。
        // 如果未指定本地路径,则默认从示例程序所属项目对应本地路径中上传文件。
        String filePath = "F:\\testPicture\\763072697.jpg";

        // 创建OSSClient实例。
        OSS ossClient = clientConfig.getOssClient();
        log.info("========>>>  创建OSSClient实例成功  ");


        try {
            log.info("========>>>  {} ", file.getInputStream());
            // 创建PutObjectRequest对象。
            PutObjectRequest putObjectRequest = new PutObjectRequest(BUCKET_NAME_PRIVATE, path, file.getInputStream());
            // 如果需要上传时设置存储类型和访问权限,请参考以下示例代码。
            // ObjectMetadata metadata = new ObjectMetadata();
            // metadata.setHeader(OSSHeaders.OSS_STORAGE_CLASS, StorageClass.Standard.toString());
            // metadata.setObjectAcl(CannedAccessControlList.Private);
            // putObjectRequest.setMetadata(metadata);

            // 上传文件。
            PutObjectResult putObjectResult = ossClient.putObject(putObjectRequest);
            ResponseMessage response = putObjectResult.getResponse();

            return new Response<>("200", "成功");
        } catch (OSSException oe) {
            System.out.println("Caught an OSSException, which means your request made it to OSS, "
                    + "but was rejected with an error response for some reason.");
            System.out.println("Error Message:" + oe.getErrorMessage());
            System.out.println("Error Code:" + oe.getErrorCode());
            System.out.println("Request ID:" + oe.getRequestId());
            System.out.println("Host ID:" + oe.getHostId());
        } catch (Exception ce) {
            System.out.println("Caught an ClientException, which means the client encountered "
                    + "a serious internal problem while trying to communicate with OSS, "
                    + "such as not being able to access the network.");
            System.out.println("Error Message:" + ce.getMessage());
            ce.printStackTrace();
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
        return new Response<>("500", "失败");
    }

    @PostMapping("/uploadByRole")
    public Response<?> uploadByRole() {


        // 填写Bucket名称,例如examplebucket。
        String bucketName = "mirror-bucket-grand";
        // 填写Object完整路径,完整路径中不能包含Bucket名称,例如exampledir/exampleobject.txt。
        String objectName = "demo/demo04.jpg";
        // 填写本地文件的完整路径,例如D:\\localpath\\examplefile.txt。
        // 如果未指定本地路径,则默认从示例程序所属项目对应本地路径中上传文件。
        String filePath = "F:\\testPicture\\763072697.jpg";

        ClientBuilderConfiguration config = new ClientBuilderConfiguration();
        config.setProxyHost(HOSTNAME);
        config.setProxyPort(PORT);
        config.setProxyUsername(PROXY_USERNAME);
        config.setProxyPassword(PROXY_PASSWORD);


        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(END_POINT, STS_ACCESS_KEY_ID, STS_SECRET_ACCESS_KEY, SECURITY_TOKEN, config);

        try {
            // 创建PutObjectRequest对象。
            PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, objectName, new File(filePath));
            // 如果需要上传时设置存储类型和访问权限,请参考以下示例代码。
            // ObjectMetadata metadata = new ObjectMetadata();
            // metadata.setHeader(OSSHeaders.OSS_STORAGE_CLASS, StorageClass.Standard.toString());
            // metadata.setObjectAcl(CannedAccessControlList.Private);
            // putObjectRequest.setMetadata(metadata);

            // 上传文件。
            ossClient.putObject(putObjectRequest);
        } catch (OSSException oe) {
            System.out.println("Caught an OSSException, which means your request made it to OSS, "
                    + "but was rejected with an error response for some reason.");
            System.out.println("Error Message:" + oe.getErrorMessage());
            System.out.println("Error Code:" + oe.getErrorCode());
            System.out.println("Request ID:" + oe.getRequestId());
            System.out.println("Host ID:" + oe.getHostId());
        } catch (Exception ce) {
            System.out.println("Caught an ClientException, which means the client encountered "
                    + "a serious internal problem while trying to communicate with OSS, "
                    + "such as not being able to access the network.");
            System.out.println("Error Message:" + ce.getMessage());
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
        return new Response<>("200", "成功");
    }

    @PostMapping("/batchDelete")
    public Response<?> batchDelete(@RequestBody List<String> list) {


        return aliyunService.batchDelete(list);
    }


}
import com.common.response.Response;
import com.pojo.vo.aliyun.OssKeyVO;

import java.util.List;

/**
 * @desc: 阿里云 业务层
 * @author mirror
 * @version 1.0
 * @since: 2022年5月13日 09:00
 */

public interface AliyunService {

    /**
     * 获取临时AK/SK
     * @param userId 用户id
     * @return 操作结果
     */
    OssKeyVO getTemporaryKey(String userId);

    /**
     * 获取下载链接
     * @param bucketName 桶名称
     * @param path 路径 填写Object完整路径,例如exampleobject.txt。Object完整路径中不能包含Bucket名称。
     * @return 下载路径
     */
    Response<?> getDownloadUrl(String bucketName , String path);

    /**
     * 删除OSS文件
     * @param bucketName 桶名称
     * @param path 填写文件完整路径。文件完整路径中不能包含Bucket名称。
     * @return 操作结果
     */
    Response<?> deleteOssFile(String bucketName, String path);

    /**
     * 批量删除文件
     * @return 操作结果
     * @param list
     */
    Response<?> batchDelete(List<String> list);

}
import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONObject;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.DeleteObjectsRequest;
import com.aliyun.oss.model.DeleteObjectsResult;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.sts.model.v20150401.AssumeRoleRequest;
import com.aliyuncs.sts.model.v20150401.AssumeRoleResponse;
import com.google.gson.Gson;
import com.neusoft.evergrande.common.response.Response;
import com.neusoft.evergrande.config.aliyun.OssClientConfig;
import com.neusoft.evergrande.pojo.vo.aliyun.OssKeyVO;
import com.neusoft.evergrande.service.aliyun.AliyunService;
import com.neusoft.evergrande.utils.aliyun.MirrorRedisUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static com.neusoft.evergrande.constant.aliyun.OssConfig.*;

/**
 * @author mirror
 * @version 1.0
 * @desc: 阿里云 业务层
 * @since: 2022年2月13日 09:00
 */
@Slf4j
@Service
public class AliyunServiceImpl implements AliyunService {

    @Resource
    private OssClientConfig clientConfig;

    public static final String ALIYUN_ASSUME_ROLE_KEY = "aliyun_assume_role_key:";


    private OssKeyVO getAssumeRole(boolean state) {
        IAcsClient client = clientConfig.getAcsClient();

        //构造请求,设置参数。关于参数含义和设置方法,请参见《API参考》。
        AssumeRoleRequest request = new AssumeRoleRequest();
        request.setRegionId(ASC_REGION_ID);
        request.setRoleArn(ROLE_ARN);
        request.setRoleSessionName(ROLE_SESSION_NAME);
        // 过期时间范围为 15min -- 1hr
        long time = 900L;
        if (state) {
            time = 3600L;
        }
        request.setDurationSeconds(time);
        OssKeyVO ossKeyVO = null;
        //发起请求,并得到响应。
        try {
            ossKeyVO = new OssKeyVO();
            AssumeRoleResponse response = client.getAcsResponse(request);
            log.info("========>>>  调用结果为 = {} ", new Gson().toJson(response));
            AssumeRoleResponse.Credentials credentials = response.getCredentials();

            String accessKeyId = credentials.getAccessKeyId();
            String accessKeySecret = credentials.getAccessKeySecret();
            String securityToken = credentials.getSecurityToken();
            String expiration = credentials.getExpiration();

            DateTime offset = DateUtil.offset(DateUtil.parse(expiration), DateField.HOUR, 8);
            String expireTime = DateUtil.format(offset, "yyyy-MM-dd HH:mm:ss");

            ossKeyVO.setAccessKeyId(accessKeyId);
            ossKeyVO.setAccessKeySecret(accessKeySecret);
            ossKeyVO.setSecurityToken(securityToken);
            ossKeyVO.setExpiration(expireTime);
            ossKeyVO.setEndPoint(END_POINT);
            log.info("========>>>  获取临时AK/SK结束 = {} ", ossKeyVO);

        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            log.info("========>>>  ErrCode : " + e.getErrCode());
            log.info("========>>>  ErrMsg : " + e.getErrMsg());
            log.info("========>>>  RequestId : " + e.getRequestId());
        } finally {

            if (client != null) {
                // 关闭client
                client.shutdown();
            }
        }

        return ossKeyVO;
    }


    @Override
    public OssKeyVO getTemporaryKey(String userId) {
        OssKeyVO ossKeyVO = null;

        String stringObsKeyVO = (String) MirrorRedisUtils.get(ALIYUN_ASSUME_ROLE_KEY + userId);
        ossKeyVO = JSONObject.parseObject(stringObsKeyVO, OssKeyVO.class);
        if (StringUtils.isNotBlank(stringObsKeyVO) && ossKeyVO != null) {
            log.info("========>>>  Redis中存在临时AK = {} ", ossKeyVO);
            return ossKeyVO;
        }
        ossKeyVO = getAssumeRole(true);
        if (ossKeyVO != null) {
            log.info("=======>>>  Redis中不存在临时AK ");
            String ossKeyVOStr = JSONObject.toJSONString(ossKeyVO);
            MirrorRedisUtils.set(ALIYUN_ASSUME_ROLE_KEY + userId, ossKeyVOStr, 3600);
            log.info("========>>>  获取的临时AK为 = {} ", ossKeyVO);
        }

        return ossKeyVO;
    }

    @Override
    public Response<?> getDownloadUrl(String bucketName , String path) {


        // 创建OSSClient实例。
//        OSS ossClient = new OSSClientBuilder().build(END_POINT, ACCESS_KEY_ID, SECRET_ACCESS_KEY);
        OSS ossClient = clientConfig.getOssClient();

        try {
            // 设置签名URL过期时间,单位为毫秒。
//            Date expiration = new Date(new Date().getTime() + 3600 * 1000);
            Date expiration = new Date(System.currentTimeMillis() + 3600 * 1000);
            // 生成以GET方法访问的签名URL,访客可以直接通过浏览器访问相关内容。
            URL url = ossClient.generatePresignedUrl(bucketName, path, expiration);
            log.info("获取的URL为 = {}", url);

            return Response.success(url);

        } catch (OSSException oe) {
            log.info("========   >>>  Caught an OSSException, which means your request made it to OSS, "
                    + "but was rejected with an error response for some reason.");
            log.info("========   >>>  Error Message:" + oe.getErrorMessage());
            log.info("========   >>>  Error Code:" + oe.getErrorCode());
            log.info("========   >>>  Request ID:" + oe.getRequestId());
            log.info("========   >>>  Host ID:" + oe.getHostId());

            return Response.error(oe.getErrorCode(), oe.getErrorMessage());
        } catch (com.aliyun.oss.ClientException ce) {
            log.info("========   >>>  Caught an ClientException, which means the client encountered "
                    + "a serious internal problem while trying to communicate with OSS, "
                    + "such as not being able to access the network.");
            log.info("========   >>>  Error Message:" + ce.getMessage());
            return Response.error(ce.getErrorCode(), ce.getErrorMessage());
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }


    }

    @Override
    public Response<?> deleteOssFile(String bucketName, String path) {


        // 创建OSSClient实例。
//        OSS ossClient = new OSSClientBuilder().build(END_POINT, ACCESS_KEY_ID, SECRET_ACCESS_KEY);
        OSS ossClient = clientConfig.getOssClient();
        try {
            // 删除文件或目录。如果要删除目录,目录必须为空。
            ossClient.deleteObject(bucketName, path);

            return Response.success(null);
        } catch (OSSException oe) {
            log.info("Caught an OSSException, which means your request made it to OSS, "
                    + "but was rejected with an error response for some reason.");
            log.info("Error Message:" + oe.getErrorMessage());
            log.info("Error Code:" + oe.getErrorCode());
            log.info("Request ID:" + oe.getRequestId());
            log.info("Host ID:" + oe.getHostId());
            return Response.error(oe.getErrorCode(), oe.getErrorMessage());
        } catch (com.aliyun.oss.ClientException ce) {
            log.info("Caught an ClientException, which means the client encountered "
                    + "a serious internal problem while trying to communicate with OSS, "
                    + "such as not being able to access the network.");
            log.info("Error Message:" + ce.getMessage());
            return Response.error(ce.getErrorCode(), ce.getErrorMessage());
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }

    }

    @Override
    public Response<?> batchDelete(List<String> keys) {


        OSS ossClient = clientConfig.getOssClient();

        try {
            // 删除文件。
            // 填写需要删除的多个文件完整路径。文件完整路径中不能包含Bucket名称。
//            List<String> keys = new ArrayList<String>();
//            keys.add("exampleobjecta.txt");
//            keys.add("testfolder/sampleobject.txt");
//            keys.add("exampleobjectb.txt");
            log.info("========>>>  删除的文件路径为 = {}", keys);
            DeleteObjectsResult deleteObjectsResult = ossClient.deleteObjects(
                    new DeleteObjectsRequest(BUCKET_NAME_PRIVATE).withKeys(keys).withEncodingType("url")
            );
            List<String> deletedObjects = deleteObjectsResult.getDeletedObjects();
            try {
                for(String obj : deletedObjects) {
                    String deleteObj =  URLDecoder.decode(obj, "UTF-8");
                    System.out.println(deleteObj);
                }
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        } catch (OSSException oe) {
            System.out.println("Caught an OSSException, which means your request made it to OSS, "
                    + "but was rejected with an error response for some reason.");
            System.out.println("Error Message:" + oe.getErrorMessage());
            System.out.println("Error Code:" + oe.getErrorCode());
            System.out.println("Request ID:" + oe.getRequestId());
            System.out.println("Host ID:" + oe.getHostId());
        } catch (com.aliyun.oss.ClientException ce) {
            System.out.println("Caught an ClientException, which means the client encountered "
                    + "a serious internal problem while trying to communicate with OSS, "
                    + "such as not being able to access the network.");
            System.out.println("Error Message:" + ce.getMessage());
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }

        return null;
    }
}
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 *
 * @author gao.yzh
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@ApiModel(value = "阿里云OSS临时AK/SK出参VO")
public class OssKeyVO {

    /**
     * AK/SK和securitytoken的过期时间
     *
     */
    @ApiModelProperty(name = "过期时间" ,value = "过期时间")
    private String expiration;

    /**
     * 获取的AK
     *
     */
    @ApiModelProperty(name = "获取的AK")
    private String accessKeyId;

    /**
     * 获取的SK
     *
     */
    @ApiModelProperty(name = "获取的SK")
    private String accessKeySecret;

    /**
     * securityToken是将所获的AK、SK等信息进行加密后的字符串
     *
     */
    @ApiModelProperty(name = "securityToken是将所获的AK、SK等信息进行加密后的字符串")
    private String securityToken;

    @ApiModelProperty(name = "securityToken是将所获的AK、SK等信息进行加密后的字符串")
    private String endPoint;

}

使用临时AK上传时

/**
 * oss 配置
 *
 * @author mirror
 * @date 2020-11-25
 */
public class STSOssConfig {


    /**
     * 临时AK
     */
    public static final String STS_ACCESS_KEY_ID = "STS_ACCESS_KEY_ID ";

    /**
     * 临时sk
     */
    public static final String STS_SECRET_ACCESS_KEY = "STS_SECRET_ACCESS_KEY";

    /**
     * 临时身份token
     */
    public static final String SECURITY_TOKEN = "SECURITY_TOKEN";


}



版权声明:本文为m0_52180274原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。