Search in sources :

Example 1 with VersionListing

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();
}
Also used : COSClient(com.qcloud.cos.COSClient) COSCredentials(com.qcloud.cos.auth.COSCredentials) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) CosServiceException(com.qcloud.cos.exception.CosServiceException) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) VersionListing(com.qcloud.cos.model.VersionListing) CosClientException(com.qcloud.cos.exception.CosClientException) COSVersionSummary(com.qcloud.cos.model.COSVersionSummary) Region(com.qcloud.cos.region.Region) ClientConfig(com.qcloud.cos.ClientConfig) ListVersionsRequest(com.qcloud.cos.model.ListVersionsRequest)

Example 2 with VersionListing

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();
}
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 VersionListing

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();
}
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 VersionListing

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

Aggregations

COSVersionSummary (com.qcloud.cos.model.COSVersionSummary)4 ListVersionsRequest (com.qcloud.cos.model.ListVersionsRequest)4 VersionListing (com.qcloud.cos.model.VersionListing)4 BucketVersioningConfiguration (com.qcloud.cos.model.BucketVersioningConfiguration)2 SetBucketVersioningConfigurationRequest (com.qcloud.cos.model.SetBucketVersioningConfigurationRequest)2 Test (org.junit.Test)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 Region (com.qcloud.cos.region.Region)1 File (java.io.File)1