Search in sources :

Example 1 with GetBucketVersioningConfigurationRequest

use of com.qcloud.cos.model.GetBucketVersioningConfigurationRequest 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);
}
Also used : ListObjectsRequest(com.qcloud.cos.model.ListObjectsRequest) COSObjectSummary(com.qcloud.cos.model.COSObjectSummary) GetBucketVersioningConfigurationRequest(com.qcloud.cos.model.GetBucketVersioningConfigurationRequest) BucketVersioningConfiguration(com.qcloud.cos.model.BucketVersioningConfiguration) ObjectListing(com.qcloud.cos.model.ObjectListing)

Aggregations

BucketVersioningConfiguration (com.qcloud.cos.model.BucketVersioningConfiguration)1 COSObjectSummary (com.qcloud.cos.model.COSObjectSummary)1 GetBucketVersioningConfigurationRequest (com.qcloud.cos.model.GetBucketVersioningConfigurationRequest)1 ListObjectsRequest (com.qcloud.cos.model.ListObjectsRequest)1 ObjectListing (com.qcloud.cos.model.ObjectListing)1