Search in sources :

Example 1 with SetBucketVersioningConfigurationRequest

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();
}
Also used : COSClient(com.qcloud.cos.COSClient) COSCredentials(com.qcloud.cos.auth.COSCredentials) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) SetBucketVersioningConfigurationRequest(com.qcloud.cos.model.SetBucketVersioningConfigurationRequest) Region(com.qcloud.cos.region.Region) BucketVersioningConfiguration(com.qcloud.cos.model.BucketVersioningConfiguration) ClientConfig(com.qcloud.cos.ClientConfig)

Example 2 with SetBucketVersioningConfigurationRequest

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();
}
Also used : VersionListing(com.qcloud.cos.model.VersionListing) COSVersionSummary(com.qcloud.cos.model.COSVersionSummary) SetBucketVersioningConfigurationRequest(com.qcloud.cos.model.SetBucketVersioningConfigurationRequest) BucketVersioningConfiguration(com.qcloud.cos.model.BucketVersioningConfiguration) ListVersionsRequest(com.qcloud.cos.model.ListVersionsRequest) Test(org.junit.Test)

Example 3 with SetBucketVersioningConfigurationRequest

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();
}
Also used : VersionListing(com.qcloud.cos.model.VersionListing) COSVersionSummary(com.qcloud.cos.model.COSVersionSummary) SetBucketVersioningConfigurationRequest(com.qcloud.cos.model.SetBucketVersioningConfigurationRequest) BucketVersioningConfiguration(com.qcloud.cos.model.BucketVersioningConfiguration) ListVersionsRequest(com.qcloud.cos.model.ListVersionsRequest) File(java.io.File) Test(org.junit.Test)

Example 4 with SetBucketVersioningConfigurationRequest

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());
}
Also used : PutObjectResult(com.qcloud.cos.model.PutObjectResult) KeyVersion(com.qcloud.cos.model.DeleteObjectsRequest.KeyVersion) SetBucketVersioningConfigurationRequest(com.qcloud.cos.model.SetBucketVersioningConfigurationRequest) ArrayList(java.util.ArrayList) BucketVersioningConfiguration(com.qcloud.cos.model.BucketVersioningConfiguration) DeleteObjectsResult(com.qcloud.cos.model.DeleteObjectsResult) File(java.io.File) DeleteObjectsRequest(com.qcloud.cos.model.DeleteObjectsRequest) Test(org.junit.Test)

Example 5 with SetBucketVersioningConfigurationRequest

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();
    }
}
Also used : PutObjectResult(com.qcloud.cos.model.PutObjectResult) KeyVersion(com.qcloud.cos.model.DeleteObjectsRequest.KeyVersion) CosClientException(com.qcloud.cos.exception.CosClientException) ArrayList(java.util.ArrayList) DeleteObjectsResult(com.qcloud.cos.model.DeleteObjectsResult) DeleteObjectsRequest(com.qcloud.cos.model.DeleteObjectsRequest) CosServiceException(com.qcloud.cos.exception.CosServiceException) MultiObjectDeleteException(com.qcloud.cos.exception.MultiObjectDeleteException) SetBucketVersioningConfigurationRequest(com.qcloud.cos.model.SetBucketVersioningConfigurationRequest) BucketVersioningConfiguration(com.qcloud.cos.model.BucketVersioningConfiguration) ArrayList(java.util.ArrayList) List(java.util.List) File(java.io.File) DeletedObject(com.qcloud.cos.model.DeleteObjectsResult.DeletedObject) Test(org.junit.Test)

Aggregations

BucketVersioningConfiguration (com.qcloud.cos.model.BucketVersioningConfiguration)9 SetBucketVersioningConfigurationRequest (com.qcloud.cos.model.SetBucketVersioningConfigurationRequest)9 Test (org.junit.Test)7 File (java.io.File)3 ArrayList (java.util.ArrayList)3 COSVersionSummary (com.qcloud.cos.model.COSVersionSummary)2 DeleteObjectsRequest (com.qcloud.cos.model.DeleteObjectsRequest)2 KeyVersion (com.qcloud.cos.model.DeleteObjectsRequest.KeyVersion)2 DeleteObjectsResult (com.qcloud.cos.model.DeleteObjectsResult)2 ListVersionsRequest (com.qcloud.cos.model.ListVersionsRequest)2 PutObjectResult (com.qcloud.cos.model.PutObjectResult)2 VersionListing (com.qcloud.cos.model.VersionListing)2 COSClient (com.qcloud.cos.COSClient)1 ClientConfig (com.qcloud.cos.ClientConfig)1 BasicCOSCredentials (com.qcloud.cos.auth.BasicCOSCredentials)1 COSCredentials (com.qcloud.cos.auth.COSCredentials)1 CosClientException (com.qcloud.cos.exception.CosClientException)1 CosServiceException (com.qcloud.cos.exception.CosServiceException)1 MultiObjectDeleteException (com.qcloud.cos.exception.MultiObjectDeleteException)1 NoncurrentVersionTransition (com.qcloud.cos.model.BucketLifecycleConfiguration.NoncurrentVersionTransition)1