use of com.qcloud.cos.model.PartETag in project cos-java-sdk-v5 by tencentyun.
the class MultipartUploadDemo method UploadPartDemo.
// 分块上传(上传某一个分片的数据)
public static List<PartETag> UploadPartDemo(String uploadId) {
// 1 初始化用户身份信息(secretId, secretKey)
COSCredentials cred = new BasicCOSCredentials("AKIDXXXXXXXX", "1A2Z3YYYYYYYYYY");
// 2 设置bucket的区域, COS地域的简称请参照 https://www.qcloud.com/document/product/436/6224
ClientConfig clientConfig = new ClientConfig(new Region("ap-guangzhou"));
// 3 生成cos客户端
COSClient cosclient = new COSClient(cred, clientConfig);
// bucket名需包含appid
String bucketName = "mybucket-1251668577";
String key = "aaa/bbb.txt";
// uploadid(通过initiateMultipartUpload或者ListMultipartUploads获取)
boolean userTrafficLimit = false;
List<PartETag> partETags = new LinkedList<>();
// 生成要上传的数据, 这里初始化一个10M的数据
for (int i = 0; i < 10; i++) {
byte[] data = new byte[1024 * 1024];
UploadPartRequest uploadPartRequest = new UploadPartRequest();
uploadPartRequest.setBucketName(bucketName);
uploadPartRequest.setKey(key);
uploadPartRequest.setUploadId(uploadId);
// 设置分块的数据来源输入流
uploadPartRequest.setInputStream(new ByteArrayInputStream(data));
// 设置分块的长度
// 设置数据长度
uploadPartRequest.setPartSize(data.length);
// 假设要上传的part编号是10
uploadPartRequest.setPartNumber(i + 1);
if (userTrafficLimit) {
uploadPartRequest.setTrafficLimit(8 * 1024 * 1024);
}
try {
UploadPartResult uploadPartResult = cosclient.uploadPart(uploadPartRequest);
PartETag partETag = uploadPartResult.getPartETag();
partETags.add(partETag);
String crc64 = uploadPartResult.getCrc64Ecma();
} catch (CosServiceException e) {
throw e;
} catch (CosClientException e) {
throw e;
}
}
cosclient.shutdown();
return partETags;
}
use of com.qcloud.cos.model.PartETag in project cos-java-sdk-v5 by tencentyun.
the class MultipartUploadDemo method copyPartUploadDemo.
// 分块copy, 表示该块的数据来自另外一个文件的某一范围, 支持跨园区, 跨bucket copy
public static void copyPartUploadDemo(String uploadId) {
// 1 初始化用户身份信息(secretId, secretKey)
COSCredentials cred = new BasicCOSCredentials("AKIDXXXXXXXX", "1A2Z3YYYYYYYYYY");
// 2 设置bucket的区域, COS地域的简称请参照 https://www.qcloud.com/document/product/436/6224
ClientConfig clientConfig = new ClientConfig(new Region("ap-guangzhou"));
// 3 生成cos客户端
COSClient cosclient = new COSClient(cred, clientConfig);
// bucket名需包含appid
String bucketName = "mybucket-1251668577";
String key = "aaa/bbb.txt";
CopyPartRequest copyPartRequest = new CopyPartRequest();
// 要拷贝的源文件所在的region
copyPartRequest.setSourceBucketRegion(new Region("ap-guangzhou"));
// 要拷贝的源文件的bucket名称
copyPartRequest.setSourceBucketName(bucketName);
// 要拷贝的源文件的路径
copyPartRequest.setSourceKey("aaa/ccc.txt");
// 指定要拷贝的源文件的数据范围(类似content-range)
copyPartRequest.setFirstByte(0L);
copyPartRequest.setLastByte(1048575L);
// 目的bucket名称
copyPartRequest.setDestinationBucketName(bucketName);
// 目的路径名称
copyPartRequest.setDestinationKey(key);
copyPartRequest.setPartNumber(1);
// uploadId
copyPartRequest.setUploadId(uploadId);
try {
CopyPartResult copyPartResult = cosclient.copyPart(copyPartRequest);
PartETag partETag = copyPartResult.getPartETag();
} catch (CosServiceException e) {
e.printStackTrace();
} catch (CosClientException e) {
e.printStackTrace();
}
cosclient.shutdown();
}
use of com.qcloud.cos.model.PartETag in project cos-java-sdk-v5 by tencentyun.
the class MultipartUploadDemo method listPartDemo.
// list part用于获取已上传的分片, 如果已上传的分片数量较多, 需要循环多次调用list part获取已上传的所有的分片
public static List<PartETag> listPartDemo(String uploadId) {
// 1 初始化用户身份信息(secretId, secretKey)
COSCredentials cred = new BasicCOSCredentials("AKIDXXXXXXXX", "1A2Z3YYYYYYYYYY");
// 2 设置bucket的区域, COS地域的简称请参照 https://www.qcloud.com/document/product/436/6224
ClientConfig clientConfig = new ClientConfig(new Region("ap-guangzhou"));
// 3 生成cos客户端
COSClient cosclient = new COSClient(cred, clientConfig);
// bucket名需包含appid
String bucketName = "mybucket-1251668577";
String key = "aaa/bbb.txt";
// uploadid(通过initiateMultipartUpload或者ListMultipartUploads获取)
// 用于保存已上传的分片信息
List<PartETag> partETags = new LinkedList<>();
PartListing partListing = null;
ListPartsRequest listPartsRequest = new ListPartsRequest(bucketName, key, uploadId);
do {
try {
partListing = cosclient.listParts(listPartsRequest);
} catch (CosServiceException e) {
throw e;
} catch (CosClientException e) {
throw e;
}
for (PartSummary partSummary : partListing.getParts()) {
partETags.add(new PartETag(partSummary.getPartNumber(), partSummary.getETag()));
}
listPartsRequest.setPartNumberMarker(partListing.getNextPartNumberMarker());
} while (partListing.isTruncated());
cosclient.shutdown();
return partETags;
}
use of com.qcloud.cos.model.PartETag in project flink-cos-fs by tencentyun.
the class RecoverableMultipartUploadImpl method awaitPendingPartsUpload.
private void awaitPendingPartsUpload() throws IOException {
checkState(this.currentUploadInfo.getRemainingParts() == this.uploadsInProgress.size());
while (this.currentUploadInfo.getRemainingParts() > 0) {
CompletableFuture<PartETag> next = this.uploadsInProgress.peekFirst();
PartETag nextPart = awaitPendingPartUploadToComplete(next);
this.currentUploadInfo.registerCompletePart(nextPart);
this.uploadsInProgress.removeFirst();
}
}
use of com.qcloud.cos.model.PartETag in project flink-cos-fs by tencentyun.
the class COSRecoverableSerializer method deserializeV1.
private static COSRecoverable deserializeV1(byte[] serialized) throws IOException {
final ByteBuffer bb = ByteBuffer.wrap(serialized).order(ByteOrder.LITTLE_ENDIAN);
if (bb.getInt() != MAGIC_NUMBER) {
throw new IOException("Corrupt data: Unexpected magic number.");
}
final byte[] keyBytes = new byte[bb.getInt()];
bb.get(keyBytes);
final byte[] uploadIdBytes = new byte[bb.getInt()];
bb.get(uploadIdBytes);
final int numParts = bb.getInt();
final ArrayList<PartETag> parts = new ArrayList<>(numParts);
for (int i = 0; i < numParts; i++) {
final int partNum = bb.getInt();
final byte[] buffer = new byte[bb.getInt()];
bb.get(buffer);
parts.add(new PartETag(partNum, new String(buffer, CHARSET)));
}
final long numBytes = bb.getLong();
final String lastPart;
final int lastObjectArraySize = bb.getInt();
if (lastObjectArraySize == 0) {
lastPart = null;
} else {
byte[] lastPartBytes = new byte[lastObjectArraySize];
bb.get(lastPartBytes);
lastPart = new String(lastPartBytes, CHARSET);
}
final long lastPartLength = bb.getLong();
return new COSRecoverable(new String(uploadIdBytes, CHARSET), new String(keyBytes, CHARSET), parts, numBytes, lastPart, lastPartLength);
}
Aggregations