use of com.qcloud.cos.model.BucketVersioningConfiguration in project cos-java-sdk-v5 by tencentyun.
the class BucketDemo method SetBucketVersioning.
// 开启 bucket 版本控制
public static void SetBucketVersioning() {
// 1 初始化用户身份信息(appid, 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 = "examplebucket-1251668577";
// 开启版本控制
BucketVersioningConfiguration bucketVersioningConfiguration = new BucketVersioningConfiguration(BucketVersioningConfiguration.ENABLED);
// 关闭版本控制
// BucketVersioningConfiguration bucketVersioningConfiguration = new BucketVersioningConfiguration(BucketVersioningConfiguration.SUSPENDED);
SetBucketVersioningConfigurationRequest setBucketVersioningConfigurationRequest = new SetBucketVersioningConfigurationRequest(bucketName, bucketVersioningConfiguration);
cosclient.setBucketVersioningConfiguration(setBucketVersioningConfigurationRequest);
cosclient.shutdown();
}
use of com.qcloud.cos.model.BucketVersioningConfiguration 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.BucketVersioningConfiguration 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.BucketVersioningConfiguration in project cos-java-sdk-v5 by tencentyun.
the class AbstractCOSClientTest method clearBucket.
private static void clearBucket() throws Exception {
abortAllNotFinishedMultipartUpload();
// 先判断bucket是否开启了版本控制
GetBucketVersioningConfigurationRequest getBucketVersioningConfigurationRequest = new GetBucketVersioningConfigurationRequest(bucket);
BucketVersioningConfiguration bucketVersioningConfiguration = cosclient.getBucketVersioningConfiguration(getBucketVersioningConfigurationRequest);
if (bucketVersioningConfiguration.getStatus().compareToIgnoreCase(BucketVersioningConfiguration.ENABLED) == 0) {
clearObjectVersions();
}
String nextMarker = "";
boolean isTruncated = false;
do {
ListObjectsRequest listObjectsRequest = new ListObjectsRequest();
listObjectsRequest.setBucketName(bucket);
listObjectsRequest.setMaxKeys(1000);
listObjectsRequest.setPrefix("");
listObjectsRequest.setDelimiter("");
listObjectsRequest.setMarker(nextMarker);
ObjectListing objectListing = cosclient.listObjects(listObjectsRequest);
for (COSObjectSummary cosObjectSummary : objectListing.getObjectSummaries()) {
String key = cosObjectSummary.getKey();
// 删除这个key
System.out.println(key);
cosclient.deleteObject(bucket, key);
}
nextMarker = objectListing.getNextMarker();
isTruncated = objectListing.isTruncated();
} while (isTruncated);
}
use of com.qcloud.cos.model.BucketVersioningConfiguration in project cos-java-sdk-v5 by tencentyun.
the class CreateDeleteHeadBucketTest method testCreateDeleteBucketPublicRead.
@Test
public void testCreateDeleteBucketPublicRead() throws Exception {
if (!judgeUserInfoValid()) {
return;
}
try {
String bucketName = String.format("java-pubr-%s", appid);
CreateBucketRequest createBucketRequest = new CreateBucketRequest(bucketName);
createBucketRequest.setCannedAcl(CannedAccessControlList.PublicRead);
Bucket bucket = cosclient.createBucket(createBucketRequest);
assertEquals(bucketName, bucket.getName());
cosclient.headBucket(new HeadBucketRequest(bucketName));
BucketVersioningConfiguration bucketVersioningConfiguration = cosclient.getBucketVersioningConfiguration(bucketName);
assertEquals(BucketVersioningConfiguration.OFF, bucketVersioningConfiguration.getStatus());
cosclient.deleteBucket(bucketName);
// 删除bucket后, 由于server端有缓存 需要稍后查询, 这里sleep 5 秒
Thread.sleep(5000L);
assertFalse(cosclient.doesBucketExist(bucketName));
} catch (CosServiceException cse) {
fail(cse.toString());
}
}
Aggregations