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

com.eworkcloud.web.util.OssClientUtils Maven / Gradle / Ivy

There is a newer version: 2.2.1
Show newest version
package com.eworkcloud.web.util;

import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.CannedAccessControlList;
import com.aliyun.oss.model.CreateBucketRequest;
import com.aliyun.oss.model.OSSObject;
import com.aliyun.oss.model.ObjectListing;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.auth.sts.AssumeRoleRequest;
import com.aliyuncs.auth.sts.AssumeRoleResponse.Credentials;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import com.eworkcloud.oss.OssConfiguration;
import com.eworkcloud.oss.OssProperties;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Date;

public abstract class OssClientUtils {

    /**
     * 获取OSS证书(STS)
     *
     * @param properties OSS配置
     * @return OSS证书
     */
    public static Credentials getCredentials(OssProperties properties) {
        try {
            DefaultProfile.addEndpoint("", "Sts", properties.getEndpoint());
            IClientProfile profile = DefaultProfile.getProfile("",
                    properties.getAccessKeyId(), properties.getAccessKeySecret());
            IAcsClient acsClient = new DefaultAcsClient(profile);

            AssumeRoleRequest request = new AssumeRoleRequest();
            request.setRoleArn(properties.getRoleArn());
            request.setRoleSessionName(properties.getRoleSessionName());
            request.setDurationSeconds(properties.getDurationSeconds());

            return acsClient.getAcsResponse(request).getCredentials();
        } catch (ClientException ex) {
            throw new RuntimeException(ex);
        }
    }

    /**
     * 获取OSS证书(STS)
     *
     * @return OSS证书
     */
    public static Credentials getCredentials() {
        OssProperties properties = new OssProperties();
        properties.setEndpoint(OssConfiguration.getEndpoint());
        properties.setAccessKeyId(OssConfiguration.getAccessKeyId());
        properties.setAccessKeySecret(OssConfiguration.getAccessKeySecret());
        properties.setRoleArn(OssConfiguration.getRoleArn());
        properties.setRoleSessionName(OssConfiguration.getRoleSessionName());
        properties.setDurationSeconds(OssConfiguration.getDurationSeconds());
        return getCredentials(properties);
    }

    /**
     * 获取OSS客户端(STS)
     *
     * @param endpoint    访问域名
     * @param credentials OSS证书
     * @return OSS客户端
     */
    @SuppressWarnings("deprecation")
    public static OSSClient getOSSClient(String endpoint, Credentials credentials) {
        return new OSSClient(endpoint, credentials.getAccessKeyId(),
                credentials.getAccessKeySecret(), credentials.getSecurityToken());
    }

    /**
     * 获取OSS客户端(STS)
     *
     * @param credentials OSS证书
     * @return OSS客户端
     */
    public static OSSClient getOSSClient(Credentials credentials) {
        return getOSSClient(OssConfiguration.getEndpoint(), credentials);
    }

    /**
     * 获取OSS客户端(OSS)
     *
     * @param properties OSS配置
     * @return OSS客户端
     */
    @SuppressWarnings("deprecation")
    public static OSSClient getOSSClient(OssProperties properties) {
        return new OSSClient(properties.getEndpoint(), properties.getAccessKeyId(), properties.getAccessKeySecret());
    }

    /**
     * 获取OSS客户端(OSS)
     *
     * @return OSS客户端
     */
    public static OSSClient getOSSClient() {
        OssProperties properties = new OssProperties();
        properties.setEndpoint(OssConfiguration.getEndpoint());
        properties.setAccessKeyId(OssConfiguration.getAccessKeyId());
        properties.setAccessKeySecret(OssConfiguration.getAccessKeySecret());
        return getOSSClient(properties);
    }

    /**
     * 检测并创建存储空间
     *
     * @param ossClient  OSS客户端
     * @param bucketName 存储空间
     */
    public static void createBucket(OSSClient ossClient, String bucketName) {
        if (!ossClient.doesBucketExist(bucketName)) {
            ossClient.createBucket(bucketName);
            CreateBucketRequest createBucketRequest = new CreateBucketRequest(bucketName);
            createBucketRequest.setCannedACL(CannedAccessControlList.PublicRead);
            ossClient.createBucket(createBucketRequest);
        }
    }

    /**
     * 检测并创建存储空间
     *
     * @param ossClient OSS客户端
     */
    public static void createBucket(OSSClient ossClient) {
        createBucket(ossClient, OssConfiguration.getBucketName());
    }

    /**
     * 下载指定键的对象
     *
     * @param ossClient  OSS客户端
     * @param bucketName 存储空间
     * @param key        键
     * @return 文件字节数组
     */
    public static InputStream getObject(OSSClient ossClient, String bucketName, String key) {
        OSSObject ossObject = ossClient.getObject(bucketName, key);
        return ossObject.getObjectContent();
    }

    /**
     * 下载指定键的对象
     *
     * @param ossClient OSS客户端
     * @param key       键
     * @return 文件字节数组
     */
    public static InputStream getObject(OSSClient ossClient, String key) {
        return getObject(ossClient, OssConfiguration.getBucketName(), key);
    }

    /**
     * 上传指定键的对象
     *
     * @param ossClient  OSS客户端
     * @param bucketName 存储空间
     * @param key        键
     * @param url        URL
     */
    public static void putObject(OSSClient ossClient, String bucketName, String key, URL url) {
        try {
            putObject(ossClient, bucketName, key, url.openStream());
        } catch (IOException ex) {
            throw new RuntimeException(ex);
        }
    }

    /**
     * 上传指定键的对象
     *
     * @param ossClient OSS客户端
     * @param key       键
     * @param url       URL
     */
    public static void putObject(OSSClient ossClient, String key, URL url) {
        try {
            putObject(ossClient, key, url.openStream());
        } catch (IOException ex) {
            throw new RuntimeException(ex);
        }
    }

    /**
     * 上传指定键的对象
     *
     * @param ossClient  OSS客户端
     * @param bucketName 存储空间
     * @param key        键
     * @param file       文件
     */
    public static void putObject(OSSClient ossClient, String bucketName, String key, File file) {
        try {
            putObject(ossClient, bucketName, key, new FileInputStream(file));
        } catch (IOException ex) {
            throw new RuntimeException(ex);
        }
    }

    /**
     * 上传指定键的对象
     *
     * @param ossClient OSS客户端
     * @param key       键
     * @param file      文件
     */
    public static void putObject(OSSClient ossClient, String key, File file) {
        try {
            putObject(ossClient, key, new FileInputStream(file));
        } catch (IOException ex) {
            throw new RuntimeException(ex);
        }
    }

    /**
     * 上传指定键的对象
     *
     * @param ossClient  OSS客户端
     * @param bucketName 存储空间
     * @param key        键
     * @param input      文件流
     */
    public static void putObject(OSSClient ossClient, String bucketName, String key, byte[] input) {
        putObject(ossClient, bucketName, key, new ByteArrayInputStream(input));
    }

    /**
     * 上传指定键的对象
     *
     * @param ossClient OSS客户端
     * @param key       键
     * @param input     文件流
     */
    public static void putObject(OSSClient ossClient, String key, byte[] input) {
        putObject(ossClient, key, new ByteArrayInputStream(input));
    }

    /**
     * 上传指定键的对象
     *
     * @param ossClient  OSS客户端
     * @param bucketName 存储空间
     * @param key        键
     * @param input      文件流
     */
    public static void putObject(OSSClient ossClient, String bucketName, String key, InputStream input) {
        ossClient.putObject(bucketName, key, input);
    }

    /**
     * 上传指定键的对象
     *
     * @param ossClient OSS客户端
     * @param key       键
     * @param input     文件流
     */
    public static void putObject(OSSClient ossClient, String key, InputStream input) {
        putObject(ossClient, OssConfiguration.getBucketName(), key, input);
    }

    /**
     * 复制指定键的对象
     *
     * @param ossClient        OSS客户端
     * @param sourceBucketName 源存储空间
     * @param sourceKey        源键
     * @param targetBucketName 目标存储空间
     * @param targetKey        目标键
     */
    public static void copyObject(OSSClient ossClient, String sourceBucketName, String sourceKey, String targetBucketName, String targetKey) {
        ossClient.copyObject(sourceBucketName, sourceKey, targetBucketName, targetKey);
    }

    /**
     * 复制指定键的对象
     *
     * @param ossClient        OSS客户端
     * @param sourceKey        源键
     * @param targetBucketName 目标存储空间
     * @param targetKey        目标键
     */
    public static void copyObject(OSSClient ossClient, String sourceKey, String targetBucketName, String targetKey) {
        copyObject(ossClient, OssConfiguration.getBucketName(), sourceKey, targetBucketName, targetKey);
    }

    /**
     * 复制指定键的对象
     *
     * @param ossClient OSS客户端
     * @param sourceKey 源键
     * @param targetKey 目标键
     */
    public static void copyObject(OSSClient ossClient, String sourceKey, String targetKey) {
        copyObject(ossClient, sourceKey, OssConfiguration.getBucketName(), targetKey);
    }

    /**
     * 删除指定键的对象
     *
     * @param ossClient  OSS客户端
     * @param bucketName 存储空间
     * @param key        键
     */
    public static void deleteObject(OSSClient ossClient, String bucketName, String key) {
        ossClient.deleteObject(bucketName, key);
    }

    /**
     * 删除指定键的对象
     *
     * @param ossClient OSS客户端
     * @param key       键
     */
    public static void deleteObject(OSSClient ossClient, String key) {
        deleteObject(ossClient, OssConfiguration.getBucketName(), key);
    }

    /**
     * 检测文件是否存在
     *
     * @param ossClient  OSS客户端
     * @param bucketName 存储空间
     * @param key        键
     * @return 是否存在
     */
    public static boolean doesObjectExist(OSSClient ossClient, String bucketName, String key) {
        return ossClient.doesObjectExist(bucketName, key);
    }

    /**
     * 检测文件是否存在
     *
     * @param ossClient OSS客户端
     * @param key       键
     * @return 是否存在
     */
    public static boolean doesObjectExist(OSSClient ossClient, String key) {
        return doesObjectExist(ossClient, OssConfiguration.getBucketName(), key);
    }

    /**
     * 列举存储空间下指定前缀的文件。最多列举100个文件。
     *
     * @param ossClient  OSS客户端
     * @param bucketName 存储空间
     * @param prefix     前缀
     * @return 文件列表
     */
    public static ObjectListing listObjects(OSSClient ossClient, String bucketName, String prefix) {
        return ossClient.listObjects(bucketName, prefix);
    }

    /**
     * 列举存储空间下指定前缀的文件。最多列举100个文件。
     *
     * @param ossClient OSS客户端
     * @param prefix    前缀
     * @return 文件列表
     */
    public static ObjectListing listObjects(OSSClient ossClient, String prefix) {
        return listObjects(ossClient, OssConfiguration.getBucketName(), prefix);
    }

    /**
     * 返回GET签名访问的URL
     *
     * @param ossClient  OSS客户端
     * @param bucketName 存储空间
     * @param key        键
     * @param expiration 到期时间
     * @return 签名访问的URL
     */
    public static URL generatePresignedUrl(OSSClient ossClient, String bucketName, String key, Date expiration) {
        return ossClient.generatePresignedUrl(bucketName, key, expiration);
    }

    /**
     * 返回GET签名访问的URL
     * expiration = 1000 * 3600 * 24
     *
     * @param ossClient  OSS客户端
     * @param bucketName 存储空间
     * @param key        键
     * @return 签名访问的URL
     */
    public static URL generatePresignedUrl(OSSClient ossClient, String bucketName, String key) {
        Date expiration = new Date(new Date().getTime() + 1000 * 3600 * 24);
        return generatePresignedUrl(ossClient, bucketName, key, expiration);
    }

    /**
     * 返回GET签名访问的URL
     *
     * @param ossClient  OSS客户端
     * @param key        键
     * @param expiration 到期时间
     * @return 签名访问的URL
     */
    public static URL generatePresignedUrl(OSSClient ossClient, String key, Date expiration) {
        return generatePresignedUrl(ossClient, OssConfiguration.getBucketName(), key, expiration);
    }

    /**
     * 返回GET签名访问的URL
     * expiration = 1000 * 3600 * 24
     *
     * @param ossClient OSS客户端
     * @param key       键
     * @return 签名访问的URL
     */
    public static URL generatePresignedUrl(OSSClient ossClient, String key) {
        return generatePresignedUrl(ossClient, OssConfiguration.getBucketName(), key);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy