use of com.qcloud.cos.model.VersionListing in project cos-java-sdk-v5 by tencentyun.
the class ListObjectsDemo method listObjectsVersions.
public static void listObjectsVersions() {
// 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-beijing-1"));
// 3 生成cos客户端
COSClient cosclient = new COSClient(cred, clientConfig);
// bucket名需包含appid
String bucketName = "mybucket-1251668577";
ListVersionsRequest listVersionsRequest = new ListVersionsRequest();
listVersionsRequest.setBucketName(bucketName);
listVersionsRequest.setPrefix("");
VersionListing versionListing = null;
do {
try {
versionListing = cosclient.listVersions(listVersionsRequest);
} catch (CosServiceException e) {
e.printStackTrace();
return;
} catch (CosClientException e) {
e.printStackTrace();
return;
}
List<COSVersionSummary> cosVersionSummaries = versionListing.getVersionSummaries();
for (COSVersionSummary cosVersionSummary : cosVersionSummaries) {
System.out.println(cosVersionSummary.getKey() + ":" + cosVersionSummary.getVersionId());
}
String keyMarker = versionListing.getNextKeyMarker();
String versionIdMarker = versionListing.getNextVersionIdMarker();
listVersionsRequest.setKeyMarker(keyMarker);
listVersionsRequest.setVersionIdMarker(versionIdMarker);
} while (versionListing.isTruncated());
cosclient.shutdown();
}
use of com.qcloud.cos.model.VersionListing in project cos-java-sdk-v5 by tencentyun.
the class ListVersionsTest method testListVersionsForSuspendedVersions.
@Test
public void testListVersionsForSuspendedVersions() throws Exception {
if (!judgeUserInfoValid()) {
return;
}
BucketVersioningConfiguration bucketVersioningEnabled = new BucketVersioningConfiguration(BucketVersioningConfiguration.SUSPENDED);
cosclient.setBucketVersioningConfiguration(new SetBucketVersioningConfigurationRequest(bucket, bucketVersioningEnabled));
Thread.sleep(5000L);
putTestFile(false);
final int maxKeyNum = 2;
ListVersionsRequest listVersionsRequest = new ListVersionsRequest();
listVersionsRequest.withBucketName(bucket).withPrefix(keyPrefix).withMaxResults(maxKeyNum);
VersionListing versionListing = cosclient.listVersions(listVersionsRequest);
int keyIndex = 0;
while (true) {
List<COSVersionSummary> versionSummaries = versionListing.getVersionSummaries();
assertTrue(versionSummaries.size() <= maxKeyNum);
for (COSVersionSummary versionInfo : versionSummaries) {
delVersion(versionInfo.getKey(), versionInfo.getVersionId());
assertTrue(versionInfo.getVersionId().equals("null"));
assertFalse(versionInfo.isDeleteMarker());
assertEquals(bucket, versionInfo.getBucketName());
int localFileIndex = (keyIndex + 1) * eachFileVersionNum - 1;
assertEquals(keyArray[localFileIndex], versionInfo.getKey());
// 应该取该尺寸大小的最后一个覆盖的文件的Etag信息
assertEquals(Md5Utils.md5Hex(localFileArray[localFileIndex]), versionInfo.getETag());
assertEquals(localFileArray[localFileIndex].length(), versionInfo.getSize());
++keyIndex;
}
if (!versionListing.isTruncated()) {
break;
}
versionListing = cosclient.listNextBatchOfVersions(versionListing);
}
assertEquals(fileNum, keyIndex);
delTestFile();
}
use of com.qcloud.cos.model.VersionListing in project cos-java-sdk-v5 by tencentyun.
the class ListVersionsTest method testListVersionsForEnabledVersions.
@Test
public void testListVersionsForEnabledVersions() throws Exception {
if (!judgeUserInfoValid()) {
return;
}
BucketVersioningConfiguration bucketVersioningEnabled = new BucketVersioningConfiguration(BucketVersioningConfiguration.ENABLED);
cosclient.setBucketVersioningConfiguration(new SetBucketVersioningConfigurationRequest(bucket, bucketVersioningEnabled));
Thread.sleep(5000L);
putTestFile(true);
final int maxKeyNum = 20;
ListVersionsRequest listVersionsRequest = new ListVersionsRequest();
listVersionsRequest.withBucketName(bucket).withPrefix(keyPrefix).withMaxResults(maxKeyNum);
VersionListing versionListing = cosclient.listVersions(listVersionsRequest);
int keyIndex = 0;
while (true) {
List<COSVersionSummary> versionSummaries = versionListing.getVersionSummaries();
assertTrue(versionSummaries.size() <= maxKeyNum);
for (COSVersionSummary versionInfo : versionSummaries) {
// 对相同可key的文件list出来的顺序是版本号由近到远
int localFileIndex = (keyIndex / eachFileVersionNum + 1) * eachFileVersionNum - 1 - (keyIndex % eachFileVersionNum);
File downFile = new File(localFileArray[localFileIndex].getAbsolutePath() + ".down");
String versionId = versionInfo.getVersionId();
String key = versionInfo.getKey();
long expectedLength = versionInfo.getSize();
String expectedEtag = versionInfo.getETag();
headAndGetVersion(key, versionId, expectedEtag, expectedLength, downFile);
delVersion(versionInfo.getKey(), versionInfo.getVersionId());
// 对于开启了多版本的 versionid不是null
assertFalse(versionInfo.getVersionId().equals("null"));
assertFalse(versionInfo.isDeleteMarker());
assertEquals(bucket, versionInfo.getBucketName());
assertEquals(keyArray[localFileIndex], versionInfo.getKey());
assertEquals(Md5Utils.md5Hex(localFileArray[localFileIndex]), versionInfo.getETag());
assertEquals(localFileArray[localFileIndex].length(), versionInfo.getSize());
++keyIndex;
}
if (!versionListing.isTruncated()) {
break;
}
versionListing = cosclient.listNextBatchOfVersions(versionListing);
}
assertEquals(fileNum * eachFileVersionNum, keyIndex);
delTestFile();
}
use of com.qcloud.cos.model.VersionListing in project cos-java-sdk-v5 by tencentyun.
the class AbstractCOSClientTest method clearObjectVersions.
private static void clearObjectVersions() throws Exception {
ListVersionsRequest listVersionsReq = new ListVersionsRequest();
listVersionsReq.setBucketName(bucket);
VersionListing versionListing = null;
while (true) {
versionListing = cosclient.listVersions(listVersionsReq);
List<COSVersionSummary> versionSummaries = versionListing.getVersionSummaries();
for (COSVersionSummary summary : versionSummaries) {
String key = summary.getKey();
String versionId = summary.getVersionId();
cosclient.deleteVersion(bucket, key, versionId);
}
if (!versionListing.isTruncated()) {
break;
}
listVersionsReq.setKeyMarker(versionListing.getNextKeyMarker());
listVersionsReq.setVersionIdMarker(versionListing.getNextVersionIdMarker());
}
}
Aggregations