All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.gitee.summer9102.develop.alibaba.oss.AliOssUtil Maven / Gradle / Ivy

The newest version!
package com.gitee.summer9102.develop.alibaba.oss;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.ObjectMetadata;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.auth.sts.AssumeRoleRequest;
import com.aliyuncs.auth.sts.AssumeRoleResponse;
import com.aliyuncs.cdn.model.v20180510.PushObjectCacheRequest;
import com.aliyuncs.cdn.model.v20180510.PushObjectCacheResponse;
import com.aliyuncs.http.MethodType;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.function.Function;

public class AliOssUtil {

    /**
     * 删除一个Object
     *
     * @param aliOss     操作对象
     * @param objectName bucket下的文件名
     */
    public static void deleteObject(AliOss aliOss, String objectName) {
        OSSClient ossClient = aliOss.getOssClient();
        AliOssProperties aliOssProperties = aliOss.getAliOssProperties();
        ossClient.deleteObject(aliOssProperties.getBucketName(), objectName);
    }

    /**
     * 上传-文件流
     *
     * @param aliOss      操作对象
     * @param key         唯一值
     * @param contentType content-type
     * @param inputStream 输入流
     * @return 访问地址
     */
    public static String upload(AliOss aliOss, String key, String contentType, InputStream inputStream) {
        OSSClient ossClient = aliOss.getOssClient();
        AliOssProperties aliOssProperties = aliOss.getAliOssProperties();
        ObjectMetadata objectMeta = new ObjectMetadata();
        objectMeta.setContentType(contentType);
        ossClient.putObject(aliOssProperties.getBucketName(), key, inputStream, objectMeta);

        String hostName = aliOssProperties.getHostName();
        if (!hostName.endsWith("/")) hostName = hostName + "/";

        return hostName + key;
    }

    /**
     * 上传-文件路径
     *
     * @param aliOss      操作对象
     * @param key         唯一值
     * @param contentType content-type
     * @param filePath    文件路径
     * @return 访问地址
     * @throws FileNotFoundException FileNotFoundException
     */
    public static String upload(AliOss aliOss, String key, String contentType, String filePath) throws FileNotFoundException {
        return upload(aliOss, key, contentType, new FileInputStream(filePath));
    }

    /**
     * oss上传权限token
     *
     * @param aliOss          操作对象
     * @param roleSessionName 支持输入 2 ~ 32 个字符,请输入至少 2 个字符,如果只有 1 个字符,会出现错误
     * @return AliOssToken
     * @throws com.aliyuncs.exceptions.ClientException ClientException
     */
    public static AliOssToken getUploadToken(AliOss aliOss, String roleSessionName) throws com.aliyuncs.exceptions.ClientException {
        AliOssProperties aliOssProperties = aliOss.getAliOssProperties();
        JSONObject statement0 = new JSONObject(3);
        statement0.put("Effect", "Allow");
        statement0.put("Action", Arrays.asList("oss:PutObject", "oss:GetObject"));
        String resource = "acs:oss:*:*:" + aliOssProperties.getBucketName() + "/" + aliOssProperties.getFilePath() + (aliOssProperties.getFilePath().endsWith("/") ? "*" : "/*");
        statement0.put("Resource", Collections.singletonList(resource));
        JSONArray statement = new JSONArray(1);
        statement.add(statement0);
        JSONObject policy = new JSONObject(2);
        policy.put("Version", "1");
        policy.put("Statement", statement);
        return getUploadToken(aliOss, roleSessionName, policy.toJSONString());
    }

    /**
     * oss上传权限token
     *
     * @param aliOss          操作对象
     * @param roleSessionName 支持输入 2 ~ 32 个字符,请输入至少 2 个字符,如果只有 1 个字符,会出现错误
     * @param policy          权限配置
     * @return AliOssToken
     * @throws com.aliyuncs.exceptions.ClientException ClientException
     */
    public static AliOssToken getUploadToken(AliOss aliOss, String roleSessionName, String policy) throws com.aliyuncs.exceptions.ClientException {
        IAcsClient acsClient = aliOss.getAcsClient();
        AliOssProperties aliOssProperties = aliOss.getAliOssProperties();
        // 创建一个 AssumeRoleRequest 并设置请求参数
        AssumeRoleRequest request = new AssumeRoleRequest();
        request.setMethod(MethodType.POST);
        request.setRoleArn(aliOssProperties.getStsRoleArn());
        request.setRoleSessionName(roleSessionName);

        request.setPolicy(policy);
        request.setDurationSeconds(aliOssProperties.getStsTokenDurationSeconds());

        // 发起请求,并得到response
        AssumeRoleResponse response = acsClient.getAcsResponse(request);
        AssumeRoleResponse.Credentials credentials = response.getCredentials();

        AliOssToken token = new AliOssToken();
        token.setEndpoint(aliOssProperties.getEndpoint());
        token.setBucketName(aliOssProperties.getBucketName());
        token.setHostName(aliOssProperties.getHostName());
        token.setFilePath(aliOssProperties.getFilePath());
        token.setAccessKeyId(credentials.getAccessKeyId());
        token.setAccessKeySecret(credentials.getAccessKeySecret());
        token.setSecurityToken(credentials.getSecurityToken());
        token.setExpiration(credentials.getExpiration());
        return token;
    }

    /**
     * cdn预热
     *
     * @param aliOss 操作对象
     * @param urls   链接集合
     * @return String
     * @throws com.aliyuncs.exceptions.ClientException ClientException
     */
    public static String preheat(AliOss aliOss, List urls) throws com.aliyuncs.exceptions.ClientException {
        PushObjectCacheRequest request = new PushObjectCacheRequest();
        request.setObjectPath(listToString(urls, "\\n", String::toString));
        PushObjectCacheResponse response = aliOss.getAcsClient().getAcsResponse(request);
        return JSON.toJSONString(response);
    }

    /**
     * 拼接string
     *
     * @param list  集合
     * @param split 分隔符
     * @param key   元素取值方法
     * @return String
     */
    public static  String listToString(List list, String split, Function key) {
        boolean first = true;
        StringBuilder b = new StringBuilder();
        for (T t : list) {
            if (first) {
                first = false;
            } else {
                b.append(split);
            }
            b.append(key.apply(t));
        }
        return b.toString();
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy