use of com.qcloud.cos.model.SetBucketVersioningConfigurationRequest 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.SetBucketVersioningConfigurationRequest 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.SetBucketVersioningConfigurationRequest 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.SetBucketVersioningConfigurationRequest in project cos-java-sdk-v5 by tencentyun.
the class BatchDeleteTest method batchDeleteAllExistFileForVersionEnabled.
@Test
public void batchDeleteAllExistFileForVersionEnabled() throws IOException, InterruptedException {
if (!judgeUserInfoValid()) {
return;
}
BucketVersioningConfiguration bucketVersioningEnabled = new BucketVersioningConfiguration(BucketVersioningConfiguration.ENABLED);
cosclient.setBucketVersioningConfiguration(new SetBucketVersioningConfigurationRequest(bucket, bucketVersioningEnabled));
Thread.sleep(5000L);
DeleteObjectsRequest deleteObjectsRequest = new DeleteObjectsRequest(bucket);
ArrayList<KeyVersion> keyList = new ArrayList<>();
long deleteFileCount = 30;
for (long fileIndex = 0; fileIndex < deleteFileCount; ++fileIndex) {
File localFile = buildTestFile(fileIndex * 1024);
String key = "ut/" + localFile.getName();
PutObjectResult putObjectResult = putObjectFromLocalFile(localFile, key);
keyList.add(new KeyVersion(key, putObjectResult.getVersionId()));
}
deleteObjectsRequest.setKeys(keyList);
DeleteObjectsResult deleteObjectsResult = cosclient.deleteObjects(deleteObjectsRequest);
assertEquals(deleteFileCount, deleteObjectsResult.getDeletedObjects().size());
}
use of com.qcloud.cos.model.SetBucketVersioningConfigurationRequest in project cos-java-sdk-v5 by tencentyun.
the class BatchDeleteTest method batchDeletePartExistFileForVersionSuspended.
@Test
public void batchDeletePartExistFileForVersionSuspended() throws IOException, InterruptedException {
if (!judgeUserInfoValid()) {
return;
}
BucketVersioningConfiguration bucketVersioningEnabled = new BucketVersioningConfiguration(BucketVersioningConfiguration.SUSPENDED);
cosclient.setBucketVersioningConfiguration(new SetBucketVersioningConfigurationRequest(bucket, bucketVersioningEnabled));
Thread.sleep(5000L);
DeleteObjectsRequest deleteObjectsRequest = new DeleteObjectsRequest(bucket);
ArrayList<KeyVersion> keyList = new ArrayList<>();
long deleteFileCount = 5;
for (long fileIndex = 0; fileIndex < deleteFileCount; ++fileIndex) {
File localFile = buildTestFile(fileIndex * 1024);
String key = "ut/" + localFile.getName();
PutObjectResult putObjectResult = putObjectFromLocalFile(localFile, key);
keyList.add(new KeyVersion(key, "null"));
}
keyList.add(new KeyVersion("ut/not_exist_key.txt", "null"));
deleteObjectsRequest.setKeys(keyList);
try {
DeleteObjectsResult deleteObjectsResult = cosclient.deleteObjects(deleteObjectsRequest);
List<DeletedObject> deleteObjectResultArray = deleteObjectsResult.getDeletedObjects();
} catch (MultiObjectDeleteException mde) {
List<DeletedObject> deleteObjects = mde.getDeletedObjects();
List<DeleteError> deleteErrors = mde.getErrors();
} catch (CosServiceException e) {
e.printStackTrace();
} catch (CosClientException e) {
e.printStackTrace();
}
}
Aggregations