com.taotao.boot.oss.ucloud.UCloudOssClient Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of taotao-boot-starter-oss-ucloud Show documentation
Show all versions of taotao-boot-starter-oss-ucloud Show documentation
taotao-boot-starter-oss-ucloud
The newest version!
/*
* Copyright (c) 2020-2030, Shuigedeng ([email protected] & https://blog.taotaocloud.top/).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.taotao.boot.oss.ucloud;
import cn.ucloud.ufile.UfileClient;
import cn.ucloud.ufile.api.object.ObjectApiBuilder;
import cn.ucloud.ufile.api.object.multi.MultiUploadInfo;
import cn.ucloud.ufile.api.object.multi.MultiUploadPartState;
import cn.ucloud.ufile.bean.DownloadFileBean;
import cn.ucloud.ufile.bean.ObjectInfoBean;
import cn.ucloud.ufile.bean.ObjectListBean;
import cn.ucloud.ufile.bean.ObjectProfile;
import cn.ucloud.ufile.exception.UfileClientException;
import cn.ucloud.ufile.util.StorageType;
import com.taotao.boot.common.utils.log.LogUtils;
import com.taotao.boot.oss.common.constant.OssConstant;
import com.taotao.boot.oss.common.exception.OssException;
import com.taotao.boot.oss.common.model.DirectoryOssInfo;
import com.taotao.boot.oss.common.model.FileOssInfo;
import com.taotao.boot.oss.common.model.OssInfo;
import com.taotao.boot.oss.common.model.SliceConfig;
import com.taotao.boot.oss.common.model.download.DownloadCheckPoint;
import com.taotao.boot.oss.common.model.download.DownloadObjectStat;
import com.taotao.boot.oss.common.model.upload.UpLoadCheckPoint;
import com.taotao.boot.oss.common.model.upload.UpLoadFileStat;
import com.taotao.boot.oss.common.model.upload.UpLoadPartEntityTag;
import com.taotao.boot.oss.common.model.upload.UpLoadPartResult;
import com.taotao.boot.oss.common.model.upload.UploadPart;
import com.taotao.boot.oss.common.service.StandardOssClient;
import com.taotao.boot.oss.common.util.OssPathUtil;
import org.dromara.hutool.core.convert.ConvertUtil;
import org.dromara.hutool.core.date.DatePattern;
import org.dromara.hutool.core.date.DateTime;
import org.dromara.hutool.core.date.DateUtil;
import org.dromara.hutool.core.io.IoUtil;
import org.dromara.hutool.core.io.file.FileNameUtil;
import org.dromara.hutool.core.io.file.FileUtil;
import org.dromara.hutool.core.reflect.FieldUtil;
import org.dromara.hutool.core.text.CharPool;
import org.dromara.hutool.core.text.StrUtil;
import org.dromara.hutool.core.util.ObjUtil;
import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* https://docs.ucloud.cn/ufile/README
*
* @author shuigedeng
* @version 2022.04
* @since 2022-04-27 17:43:23
*/
public class UCloudOssClient implements StandardOssClient {
public static final String OBJECT_OBJECT_NAME = "objectApiBuilder";
private UfileClient ufileClient;
private ObjectApiBuilder objectApiBuilder;
private UCloudOssConfig uCloudOssConfig;
public UCloudOssClient(
UfileClient ufileClient, ObjectApiBuilder objectApiBuilder, UCloudOssConfig uCloudOssConfig) {
this.ufileClient = ufileClient;
this.objectApiBuilder = objectApiBuilder;
this.uCloudOssConfig = uCloudOssConfig;
}
@Override
public OssInfo upLoad(InputStream is, String targetName, Boolean isOverride) {
String bucketName = getBucket();
String key = getKey(targetName, false);
if (isOverride || !isExist(targetName)) {
try {
objectApiBuilder
.putObject(is, 0, "")
.nameAs(key)
.toBucket(bucketName)
.execute();
} catch (Exception e) {
throw new OssException(e);
}
}
return getInfo(targetName);
}
@Override
public OssInfo upLoadCheckPoint(File file, String targetName) {
uploadFile(file, targetName, uCloudOssConfig.getSliceConfig(), OssConstant.OssType.UCLOUD);
return getInfo(targetName);
}
@Override
public void completeUpload(UpLoadCheckPoint upLoadCheckPoint, List partEntityTags) {
List partStates = partEntityTags.stream()
.sorted(Comparator.comparingInt(UpLoadPartEntityTag::getPartNumber))
.map(partEntityTag -> {
MultiUploadPartState multiUploadPartState = new MultiUploadPartState();
// TODO
return multiUploadPartState;
})
.toList();
MultiUploadInfo multiUploadInfo = new MultiUploadInfo();
objectApiBuilder.finishMultiUpload(multiUploadInfo, partStates);
FileUtil.del(upLoadCheckPoint.getCheckpointFile());
}
@Override
public void prepareUpload(
UpLoadCheckPoint uploadCheckPoint,
File upLoadFile,
String targetName,
String checkpointFile,
SliceConfig slice) {
String bucket = getBucket();
String key = getKey(targetName, false);
uploadCheckPoint.setMagic(UpLoadCheckPoint.UPLOAD_MAGIC);
uploadCheckPoint.setUploadFile(upLoadFile.getPath());
uploadCheckPoint.setKey(key);
uploadCheckPoint.setBucket(bucket);
uploadCheckPoint.setCheckpointFile(checkpointFile);
uploadCheckPoint.setUploadFileStat(UpLoadFileStat.getFileStat(uploadCheckPoint.getUploadFile()));
long partSize = slice.getPartSize();
long fileLength = upLoadFile.length();
int parts = (int) (fileLength / partSize);
if (fileLength % partSize > 0) {
parts++;
}
uploadCheckPoint.setUploadParts(
splitUploadFile(uploadCheckPoint.getUploadFileStat().getSize(), partSize));
uploadCheckPoint.setPartEntityTags(new ArrayList<>());
uploadCheckPoint.setOriginPartSize(parts);
try {
MultiUploadInfo multiUploadInfo = objectApiBuilder
.initMultiUpload(key, "", bucket)
.withStorageType(StorageType.STANDARD)
.execute();
uploadCheckPoint.setUploadId(multiUploadInfo.getUploadId());
} catch (Exception e) {
throw new OssException(e);
}
}
@Override
public UpLoadPartResult uploadPart(UpLoadCheckPoint upLoadCheckPoint, int partNum, InputStream inputStream) {
UploadPart uploadPart = upLoadCheckPoint.getUploadParts().get(partNum);
long partSize = uploadPart.getSize();
UpLoadPartResult partResult = new UpLoadPartResult(partNum + 1, uploadPart.getOffset(), partSize);
try {
inputStream.skip(uploadPart.getOffset());
MultiUploadInfo multiUploadInfo = new MultiUploadInfo();
MultiUploadPartState multiUploadPartState = objectApiBuilder
.multiUploadPart(multiUploadInfo, null, partNum)
.from(null, ConvertUtil.toInt(uploadPart.getOffset()), ConvertUtil.toInt(partSize), partNum)
.execute();
partResult.setNumber(multiUploadPartState.getPartIndex());
UpLoadPartEntityTag upLoadPartEntityTag = new UpLoadPartEntityTag();
upLoadPartEntityTag.setETag(multiUploadPartState.geteTag());
upLoadPartEntityTag.setPartNumber(multiUploadPartState.getPartIndex());
partResult.setEntityTag(upLoadPartEntityTag);
} catch (Exception e) {
partResult.setFailed(true);
partResult.setException(e);
} finally {
IoUtil.closeQuietly(inputStream);
}
return partResult;
}
@Override
public void downLoad(OutputStream os, String targetName) {
ObjectProfile objectProfile = new ObjectProfile();
objectProfile.setBucket(getBucket());
objectProfile.setKeyName(getKey(targetName, false));
try {
DownloadFileBean downloadFileBean =
objectApiBuilder.downloadFile(objectProfile).execute();
IoUtil.copy(FileUtil.getInputStream(downloadFileBean.getFile()), os);
} catch (UfileClientException e) {
throw new OssException(e);
}
}
@Override
public void downLoadCheckPoint(File localFile, String targetName) {
downLoadFile(localFile, targetName, uCloudOssConfig.getSliceConfig(), OssConstant.OssType.UCLOUD);
}
@Override
public DownloadObjectStat getDownloadObjectStat(String targetName) {
try {
ObjectProfile objectProfile = objectApiBuilder
.objectProfile(getKey(targetName, false), getBucket())
.execute();
DateTime date = DateUtil.parse(objectProfile.getLastModified());
long contentLength = objectProfile.getContentLength();
String eTag = objectProfile.geteTag();
DownloadObjectStat downloadObjectStat = new DownloadObjectStat();
downloadObjectStat.setSize(contentLength);
downloadObjectStat.setLastModified(date);
downloadObjectStat.setDigest(eTag);
return downloadObjectStat;
} catch (Exception e) {
throw new OssException(e);
}
}
@Override
public void prepareDownload(
DownloadCheckPoint downloadCheckPoint, File localFile, String targetName, String checkpointFile) {
downloadCheckPoint.setMagic(DownloadCheckPoint.DOWNLOAD_MAGIC);
downloadCheckPoint.setDownloadFile(localFile.getPath());
downloadCheckPoint.setBucketName(getBucket());
downloadCheckPoint.setKey(getKey(targetName, false));
downloadCheckPoint.setCheckPointFile(checkpointFile);
downloadCheckPoint.setObjectStat(getDownloadObjectStat(targetName));
long downloadSize;
if (downloadCheckPoint.getObjectStat().getSize() > 0) {
Long partSize = uCloudOssConfig.getSliceConfig().getPartSize();
long[] slice = getDownloadSlice(
new long[0], downloadCheckPoint.getObjectStat().getSize());
downloadCheckPoint.setDownloadParts(splitDownloadFile(slice[0], slice[1], partSize));
downloadSize = slice[1];
} else {
// download whole file
downloadSize = 0;
downloadCheckPoint.setDownloadParts(splitDownloadOneFile());
}
downloadCheckPoint.setOriginPartSize(
downloadCheckPoint.getDownloadParts().size());
createDownloadTemp(downloadCheckPoint.getTempDownloadFile(), downloadSize);
}
@Override
public InputStream downloadPart(String key, long start, long end) {
ObjectProfile objectProfile = new ObjectProfile();
objectProfile.setBucket(getBucket());
objectProfile.setKeyName(key);
try {
DownloadFileBean downloadFileBean = objectApiBuilder
.downloadFile(objectProfile)
.withinRange(start, end)
.execute();
return FileUtil.getInputStream(downloadFileBean.getFile());
} catch (UfileClientException e) {
throw new OssException(e);
}
}
@Override
public void delete(String targetName) {
objectApiBuilder.deleteObject(getBucket(), getKey(targetName, false));
}
@Override
public void copy(String sourceName, String targetName, Boolean isOverride) {
String bucketName = getBucket();
String targetKey = getKey(targetName, false);
if (isOverride || !isExist(targetName)) {
try {
objectApiBuilder
.copyObject(bucketName, getKey(sourceName, false))
.copyTo(bucketName, targetKey)
.execute();
} catch (Exception e) {
throw new OssException(e);
}
}
}
@Override
public OssInfo getInfo(String targetName, Boolean isRecursion) {
String key = getKey(targetName, false);
OssInfo ossInfo = getBaseInfo(key);
ossInfo.setName(StrUtil.equals(targetName, StrUtil.SLASH) ? targetName : FileNameUtil.getName(targetName));
ossInfo.setPath(OssPathUtil.replaceKey(targetName, ossInfo.getName(), true));
if (isRecursion && isDirectory(key)) {
String prefix = OssPathUtil.convertPath(key, false);
ObjectListBean objectListBean;
try {
objectListBean = objectApiBuilder
.objectList(getBucket())
.withPrefix(prefix.endsWith("/") ? prefix : prefix + CharPool.SLASH)
.execute();
} catch (Exception e) {
throw new OssException(e);
}
List objectList = objectListBean.getObjectList();
List fileOssInfos = new ArrayList<>();
List directoryInfos = new ArrayList<>();
for (ObjectInfoBean objectInfoBean : objectList) {
String fileName = objectInfoBean.getFileName();
if (FileNameUtil.getName(fileName).equals(FileNameUtil.getName(key))) {
ossInfo.setLastUpdateTime(
DateUtil.date(objectInfoBean.getModifyTime()).toString(DatePattern.NORM_DATETIME_PATTERN));
ossInfo.setCreateTime(
DateUtil.date(objectInfoBean.getCreateTime()).toString(DatePattern.NORM_DATETIME_PATTERN));
ossInfo.setLength(objectInfoBean.getSize());
} else if (isDirectory(fileName)) {
directoryInfos.add(getInfo(OssPathUtil.replaceKey(fileName, getBasePath(), false), true));
} else {
fileOssInfos.add(getInfo(OssPathUtil.replaceKey(fileName, getBasePath(), false), false));
}
}
if (ObjUtil.isNotEmpty(fileOssInfos) && fileOssInfos.get(0) instanceof FileOssInfo) {
FieldUtil.setFieldValue(ossInfo, "fileInfos", fileOssInfos);
}
if (ObjUtil.isNotEmpty(directoryInfos) && directoryInfos.get(0) instanceof DirectoryOssInfo) {
FieldUtil.setFieldValue(ossInfo, "directoryInfos", directoryInfos);
}
}
return ossInfo;
}
@Override
public Boolean isExist(String targetName) {
try {
ObjectProfile objectProfile = objectApiBuilder
.objectProfile(getKey(targetName, false), getBucket())
.execute();
return objectProfile.getContentLength() > 0;
} catch (Exception e) {
LogUtils.error("", e);
return false;
}
}
@Override
public String getBasePath() {
return uCloudOssConfig.getBasePath();
}
@Override
public Map getClientObject() {
return new HashMap() {
{
put(OBJECT_OBJECT_NAME, getObjectApiBuilder());
}
};
}
private String getBucket() {
return uCloudOssConfig.getBucketName();
}
public OssInfo getBaseInfo(String key) {
OssInfo ossInfo;
if (isFile(key)) {
ossInfo = new FileOssInfo();
try {
ObjectProfile objectProfile = getObjectApiBuilder()
.objectProfile(OssPathUtil.replaceKey(key, getBasePath(), false), getBucket())
.execute();
ossInfo.setLastUpdateTime(
DateUtil.parse(objectProfile.getLastModified()).toString(DatePattern.NORM_DATETIME_PATTERN));
ossInfo.setCreateTime(
DateUtil.parse(objectProfile.getCreateTime()).toString(DatePattern.NORM_DATETIME_PATTERN));
ossInfo.setLength(objectProfile.getContentLength());
} catch (Exception e) {
LogUtils.error("获取{}文件属性失败", key, e);
}
} else {
ossInfo = new DirectoryOssInfo();
}
return ossInfo;
}
public UfileClient getUfileClient() {
return ufileClient;
}
public void setUfileClient(UfileClient ufileClient) {
this.ufileClient = ufileClient;
}
public ObjectApiBuilder getObjectApiBuilder() {
return objectApiBuilder;
}
public void setObjectApiBuilder(ObjectApiBuilder objectApiBuilder) {
this.objectApiBuilder = objectApiBuilder;
}
public UCloudOssConfig getuCloudOssConfig() {
return uCloudOssConfig;
}
public void setuCloudOssConfig(UCloudOssConfig uCloudOssConfig) {
this.uCloudOssConfig = uCloudOssConfig;
}
}