com.taotao.boot.oss.tencent.TencentOssClient 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-tencent Show documentation
Show all versions of taotao-boot-starter-oss-tencent Show documentation
taotao-boot-starter-oss-tencent
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.tencent;
import com.qcloud.cos.COSClient;
import com.qcloud.cos.model.COSObject;
import com.qcloud.cos.model.COSObjectSummary;
import com.qcloud.cos.model.CompleteMultipartUploadRequest;
import com.qcloud.cos.model.GetObjectRequest;
import com.qcloud.cos.model.InitiateMultipartUploadRequest;
import com.qcloud.cos.model.InitiateMultipartUploadResult;
import com.qcloud.cos.model.ObjectListing;
import com.qcloud.cos.model.ObjectMetadata;
import com.qcloud.cos.model.PartETag;
import com.qcloud.cos.model.UploadPartRequest;
import com.qcloud.cos.model.UploadPartResult;
import com.taotao.boot.common.utils.log.LogUtils;
import com.taotao.boot.oss.common.constant.OssConstant;
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.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://cloud.tencent.com/document/product/436
*
* @author shuigedeng
* @version 2022.04
* @since 2022-04-27 17:43:09
*/
public class TencentOssClient implements StandardOssClient {
public static final String COS_OBJECT_NAME = "cosClient";
private COSClient cosClient;
private TencentOssConfig tencentOssConfig;
public TencentOssClient(COSClient cosClient, TencentOssConfig tencentOssConfig) {
this.cosClient = cosClient;
this.tencentOssConfig = tencentOssConfig;
}
@Override
public OssInfo upLoad(InputStream is, String targetName, Boolean isOverride) {
String bucketName = getBucket();
String key = getKey(targetName, false);
if (isOverride || !cosClient.doesObjectExist(bucketName, key)) {
cosClient.putObject(bucketName, key, is, new ObjectMetadata());
}
return getInfo(targetName);
}
@Override
public OssInfo upLoadCheckPoint(File file, String targetName) {
uploadFile(file, targetName, tencentOssConfig.getSliceConfig(), OssConstant.OssType.TENCENT);
return getInfo(targetName);
}
@Override
public void completeUpload(UpLoadCheckPoint upLoadCheckPoint, List partEntityTags) {
List eTags = partEntityTags.stream()
.sorted(Comparator.comparingInt(UpLoadPartEntityTag::getPartNumber))
.map(partEntityTag -> new PartETag(partEntityTag.getPartNumber(), partEntityTag.getETag()))
.toList();
CompleteMultipartUploadRequest completeMultipartUploadRequest = new CompleteMultipartUploadRequest(
upLoadCheckPoint.getBucket(), upLoadCheckPoint.getKey(), upLoadCheckPoint.getUploadId(), eTags);
cosClient.completeMultipartUpload(completeMultipartUploadRequest);
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);
InitiateMultipartUploadRequest request = new InitiateMultipartUploadRequest(bucket, key);
InitiateMultipartUploadResult result = cosClient.initiateMultipartUpload(request);
uploadCheckPoint.setUploadId(result.getUploadId());
}
@Override
public UpLoadPartResult uploadPart(UpLoadCheckPoint upLoadCheckPoint, int partNum, InputStream inputStream) {
UpLoadPartResult partResult = null;
UploadPart uploadPart = upLoadCheckPoint.getUploadParts().get(partNum);
long partSize = uploadPart.getSize();
partResult = new UpLoadPartResult(partNum + 1, uploadPart.getOffset(), partSize);
try {
inputStream.skip(uploadPart.getOffset());
UploadPartRequest uploadPartRequest = new UploadPartRequest();
uploadPartRequest.setBucketName(upLoadCheckPoint.getBucket());
uploadPartRequest.setKey(upLoadCheckPoint.getKey());
uploadPartRequest.setUploadId(upLoadCheckPoint.getUploadId());
uploadPartRequest.setInputStream(inputStream);
uploadPartRequest.setPartSize(partSize);
uploadPartRequest.setPartNumber(uploadPart.getNumber());
UploadPartResult uploadPartResponse = cosClient.uploadPart(uploadPartRequest);
partResult.setNumber(uploadPartResponse.getPartNumber());
UpLoadPartEntityTag upLoadPartEntityTag = new UpLoadPartEntityTag();
upLoadPartEntityTag.setETag(uploadPartResponse.getETag());
upLoadPartEntityTag.setPartNumber(uploadPartResponse.getPartNumber());
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) {
COSObject cosObject = cosClient.getObject(getBucket(), getKey(targetName, false));
IoUtil.copy(cosObject.getObjectContent(), os);
}
@Override
public void downLoadCheckPoint(File localFile, String targetName) {
downLoadFile(localFile, targetName, tencentOssConfig.getSliceConfig(), OssConstant.OssType.TENCENT);
}
@Override
public DownloadObjectStat getDownloadObjectStat(String targetName) {
ObjectMetadata objectMetadata = cosClient.getObjectMetadata(getBucket(), getKey(targetName, false));
DateTime date = DateUtil.date(objectMetadata.getLastModified());
long contentLength = objectMetadata.getContentLength();
String eTag = objectMetadata.getETag();
DownloadObjectStat downloadObjectStat = new DownloadObjectStat();
downloadObjectStat.setSize(contentLength);
downloadObjectStat.setLastModified(date);
downloadObjectStat.setDigest(eTag);
return downloadObjectStat;
}
@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 = tencentOssConfig.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) {
GetObjectRequest request = new GetObjectRequest(getBucket(), key);
request.setRange(start, end);
COSObject object = cosClient.getObject(request);
return object.getObjectContent();
}
@Override
public void delete(String targetName) {
cosClient.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 || !cosClient.doesObjectExist(bucketName, targetKey)) {
cosClient.copyObject(getBucket(), getKey(sourceName, false), getBucket(), targetKey);
}
}
@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);
ObjectListing listObjects =
cosClient.listObjects(getBucket(), prefix.endsWith("/") ? prefix : prefix + CharPool.SLASH);
List fileOssInfos = new ArrayList<>();
List directoryInfos = new ArrayList<>();
for (COSObjectSummary cosObjectSummary : listObjects.getObjectSummaries()) {
if (FileNameUtil.getName(cosObjectSummary.getKey()).equals(FileNameUtil.getName(key))) {
ossInfo.setLastUpdateTime(DateUtil.date(cosObjectSummary.getLastModified())
.toString(DatePattern.NORM_DATETIME_PATTERN));
ossInfo.setCreateTime(DateUtil.date(cosObjectSummary.getLastModified())
.toString(DatePattern.NORM_DATETIME_PATTERN));
ossInfo.setLength(cosObjectSummary.getSize());
} else {
fileOssInfos.add(
getInfo(OssPathUtil.replaceKey(cosObjectSummary.getKey(), getBasePath(), false), false));
}
}
for (String commonPrefix : listObjects.getCommonPrefixes()) {
String target = OssPathUtil.replaceKey(commonPrefix, getBasePath(), false);
if (isDirectory(commonPrefix)) {
directoryInfos.add(getInfo(target, true));
} else {
fileOssInfos.add(getInfo(target, 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) {
return cosClient.doesObjectExist(getBucket(), getKey(targetName, false));
}
@Override
public String getBasePath() {
return tencentOssConfig.getBasePath();
}
@Override
public Map getClientObject() {
return new HashMap() {
{
put(COS_OBJECT_NAME, getCosClient());
}
};
}
private String getBucket() {
return tencentOssConfig.getBucketName();
}
public OssInfo getBaseInfo(String key) {
OssInfo ossInfo;
if (isFile(key)) {
ossInfo = new FileOssInfo();
try {
ObjectMetadata objectMetadata =
cosClient.getObjectMetadata(getBucket(), OssPathUtil.replaceKey(key, getBasePath(), false));
ossInfo.setLastUpdateTime(
DateUtil.date(objectMetadata.getLastModified()).toString(DatePattern.NORM_DATETIME_PATTERN));
ossInfo.setCreateTime(
DateUtil.date(objectMetadata.getLastModified()).toString(DatePattern.NORM_DATETIME_PATTERN));
ossInfo.setLength(objectMetadata.getContentLength());
} catch (Exception e) {
LogUtils.error("获取{}文件属性失败", key, e);
}
} else {
ossInfo = new DirectoryOssInfo();
}
return ossInfo;
}
public COSClient getCosClient() {
return cosClient;
}
public void setCosClient(COSClient cosClient) {
this.cosClient = cosClient;
}
public TencentOssConfig getTencentOssConfig() {
return tencentOssConfig;
}
public void setTencentOssConfig(TencentOssConfig tencentOssConfig) {
this.tencentOssConfig = tencentOssConfig;
}
}