package com.oying.utils;
|
|
import com.obs.services.ObsClient;
|
import com.obs.services.model.AccessControlList;
|
import com.obs.services.model.ObsObject;
|
import com.obs.services.model.PutObjectRequest;
|
import com.obs.services.model.PutObjectResult;
|
import com.oying.exception.BadRequestException;
|
|
import java.io.File;
|
import java.io.FileOutputStream;
|
import java.io.InputStream;
|
|
public class ObsUtils {
|
|
|
|
/**
|
* 创建ObsClient实例
|
*
|
* @param properties obs配置
|
* @return ObsClient实例
|
*/
|
public static ObsClient getObsClient(ObsProperties properties) {
|
// 创建ObsClient实例
|
return new ObsClient(properties.getAccessKeyId(), properties.getAccessKeySecret(), properties.getEndpoint());
|
}
|
|
/**
|
* 上传对象权限
|
*
|
* @param properties obs配置
|
* @param file 文件
|
* @param objectKey 对象名
|
* @param acl 对象权限
|
* @return PutObjectResult
|
*/
|
public static PutObjectResult putObject(ObsProperties properties, File file, String objectKey, AccessControlList acl) {
|
// 创建ObsClient实例
|
ObsClient obsClient = getObsClient(properties);
|
try {
|
PutObjectRequest request = new PutObjectRequest();
|
request.setBucketName(properties.getBucket());
|
request.setObjectKey(objectKey);
|
request.setFile(file);
|
// 对象权限
|
request.setAcl(acl);
|
// 为待上传的本地文件路径,需要指定到具体的文件名
|
return obsClient.putObject(request);
|
} catch (Exception e) {
|
throw new BadRequestException("上传对象失败" + e.getMessage());
|
} finally {
|
FileUtil.del(file);
|
CloseUtil.close(obsClient);
|
}
|
}
|
|
/**
|
* 上传对象
|
*
|
* @param properties obs配置
|
* @param file 文件
|
* @param objectKey 对象名
|
* @return PutObjectResult
|
*/
|
public static PutObjectResult putObject(ObsProperties properties, File file, String objectKey) {
|
// 创建ObsClient实例
|
ObsClient obsClient = getObsClient(properties);
|
try {
|
// 为待上传的本地文件路径,需要指定到具体的文件名
|
return obsClient.putObject(properties.getBucket(), objectKey, file);
|
} catch (Exception e) {
|
throw new BadRequestException("上传对象失败" + e.getMessage());
|
} finally {
|
FileUtil.del(file);
|
CloseUtil.close(obsClient);
|
}
|
}
|
|
/**
|
* 下载对象
|
*
|
* @param properties obs配置
|
* @param objectKey 对象名
|
* @param path 保存路径
|
*/
|
public static String getObject(ObsProperties properties, String objectKey, String path) {
|
// 创建ObsClient实例
|
ObsClient obsClient = getObsClient(properties);
|
ObsObject obsObject = obsClient.getObject(properties.getBucket(), objectKey);
|
InputStream in = obsObject.getObjectContent();
|
if (in != null) {
|
FileOutputStream out = null;
|
try {
|
// getCanonicalFile 可解析正确各种路径
|
File dest = new File(path).getCanonicalFile();
|
// 检测是否存在目录
|
if (!dest.getParentFile().exists()) {
|
dest.getParentFile().mkdirs();
|
}
|
out = new FileOutputStream(path);
|
byte[] b = new byte[1024];
|
int length;
|
while ((length = in.read(b)) > 0) {
|
out.write(b, 0, length);
|
}
|
} catch (Exception e) {
|
throw new BadRequestException("流输出异常");
|
} finally {
|
CloseUtil.close(in);
|
CloseUtil.close(out);
|
CloseUtil.close(obsClient);
|
}
|
}
|
return path;
|
}
|
|
/**
|
* 删除对象
|
*
|
* @param properties obs配置
|
* @param objectKey 对象名
|
*/
|
public static void deleteObject(ObsProperties properties, String objectKey) {
|
// 创建ObsClient实例
|
ObsClient obsClient = getObsClient(properties);
|
// 删除指定的对象
|
obsClient.deleteObject(properties.getBucket(), objectKey);
|
CloseUtil.close(obsClient);
|
}
|
|
public static String getPublicObjectUrl(ObsProperties properties, String objectKey) {
|
return getPublicObjectUrl(properties.getBucket(), properties.getEndpoint(), objectKey);
|
}
|
|
public static String getPublicObjectUrl(String bucketName, String endpoint, String objectKey) {
|
// 移除objectKey开头可能存在的文件分割符('/')
|
String cleanObjectKey = objectKey.startsWith("/") ? objectKey.substring(1) : objectKey;
|
// 标准URL格式
|
return String.format("https://%s.%s/%s", bucketName, endpoint.replaceAll("^https?://", ""), cleanObjectKey);
|
}
|
}
|