com.eworkcloud.web.util.OssClientUtils Maven / Gradle / Ivy
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.aliyun.oss.model.ObjectMetadata;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.auth.sts.AssumeRoleRequest;
import com.aliyuncs.auth.sts.AssumeRoleResponse.Credentials;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import com.eworkcloud.oss.OssConfiguration;
import com.eworkcloud.oss.OssProperties;
import lombok.SneakyThrows;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.URL;
import java.util.Date;
public abstract class OssClientUtils {
/**
* 获取OSS证书(STS)
*
* @param properties OSS配置
* @return OSS证书
*/
@SneakyThrows
public static Credentials getCredentials(OssProperties properties) {
DefaultProfile.addEndpoint("", "Sts", properties.getEndpoint());
IClientProfile profile = DefaultProfile.getProfile("",
properties.getAccessKey(), properties.getSecretKey());
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();
}
/**
* 获取OSS证书(STS)
*
* @return OSS证书
*/
public static Credentials getCredentials() {
OssProperties properties = new OssProperties();
properties.setEndpoint(OssConfiguration.getEndpoint());
properties.setAccessKey(OssConfiguration.getAccessKey());
properties.setSecretKey(OssConfiguration.getSecretKey());
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.getAccessKey(), properties.getSecretKey());
}
/**
* 获取OSS客户端(OSS)
*
* @return OSS客户端
*/
public static OSSClient getOSSClient() {
OssProperties properties = new OssProperties();
properties.setEndpoint(OssConfiguration.getEndpoint());
properties.setAccessKey(OssConfiguration.getAccessKey());
properties.setSecretKey(OssConfiguration.getSecretKey());
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 OSS对象参数
*/
public static ObjectMetadata getMetadata(OSSClient ossClient, String bucketName, String key) {
return ossClient.getObjectMetadata(bucketName, key);
}
/**
* 获取键的对象参数
*
* @param ossClient OSS客户端
* @param key 键
* @return OSS对象参数
*/
public static ObjectMetadata getMetadata(OSSClient ossClient, String key) {
return getMetadata(ossClient, OssConfiguration.getBucketName(), key);
}
/**
* 获取指定键的对象
*
* @param ossClient OSS客户端
* @param bucketName 存储空间
* @param key 键
* @return OSS对象
*/
public static OSSObject getObject(OSSClient ossClient, String bucketName, String key) {
return ossClient.getObject(bucketName, key);
}
/**
* 获取指定键的对象
*
* @param ossClient OSS客户端
* @param key 键
* @return OSS对象
*/
public static OSSObject getObject(OSSClient ossClient, String key) {
return getObject(ossClient, OssConfiguration.getBucketName(), key);
}
/**
* 下载指定键的对象
*
* @param ossClient OSS客户端
* @param bucketName 存储空间
* @param key 键
* @return 文件输入流
*/
public static InputStream getStream(OSSClient ossClient, String bucketName, String key) {
OSSObject ossObject = ossClient.getObject(bucketName, key);
return ossObject.getObjectContent();
}
/**
* 下载指定键的对象
*
* @param ossClient OSS客户端
* @param key 键
* @return 文件输入流
*/
public static InputStream getStream(OSSClient ossClient, String key) {
return getStream(ossClient, OssConfiguration.getBucketName(), key);
}
/**
* 上传指定键的对象
*
* @param ossClient OSS客户端
* @param bucketName 存储空间
* @param key 键
* @param url URL
*/
@SneakyThrows
public static void putObject(OSSClient ossClient, String bucketName, String key, URL url) {
putObject(ossClient, bucketName, key, url.openStream());
}
/**
* 上传指定键的对象
*
* @param ossClient OSS客户端
* @param key 键
* @param url URL
*/
@SneakyThrows
public static void putObject(OSSClient ossClient, String key, URL url) {
putObject(ossClient, key, url.openStream());
}
/**
* 上传指定键的对象
*
* @param ossClient OSS客户端
* @param bucketName 存储空间
* @param key 键
* @param file 文件
*/
@SneakyThrows
public static void putObject(OSSClient ossClient, String bucketName, String key, File file) {
putObject(ossClient, bucketName, key, new FileInputStream(file));
}
/**
* 上传指定键的对象
*
* @param ossClient OSS客户端
* @param key 键
* @param file 文件
*/
@SneakyThrows
public static void putObject(OSSClient ossClient, String key, File file) {
putObject(ossClient, key, new FileInputStream(file));
}
/**
* 上传指定键的对象
*
* @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 existObject(OSSClient ossClient, String bucketName, String key) {
return ossClient.doesObjectExist(bucketName, key);
}
/**
* 检测文件是否存在
*
* @param ossClient OSS客户端
* @param key 键
* @return 是否存在
*/
public static boolean existObject(OSSClient ossClient, String key) {
return existObject(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 presignedUrl(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 presignedUrl(OSSClient ossClient, String bucketName, String key) {
Date expiration = new Date(new Date().getTime() + 1000 * 3600 * 24);
return presignedUrl(ossClient, bucketName, key, expiration);
}
/**
* 返回GET签名访问的URL
*
* @param ossClient OSS客户端
* @param key 键
* @param expiration 到期时间
* @return 签名访问的URL
*/
public static URL presignedUrl(OSSClient ossClient, String key, Date expiration) {
return presignedUrl(ossClient, OssConfiguration.getBucketName(), key, expiration);
}
/**
* 返回GET签名访问的URL
* expiration = 1000 * 3600 * 24
*
* @param ossClient OSS客户端
* @param key 键
* @return 签名访问的URL
*/
public static URL presignedUrl(OSSClient ossClient, String key) {
return presignedUrl(ossClient, OssConfiguration.getBucketName(), key);
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy