Search in sources :

Example 1 with KeyVersion

use of com.qcloud.cos.model.DeleteObjectsRequest.KeyVersion in project cos-java-sdk-v5 by tencentyun.

the class DelFileDemo method BatchDelFileWithVersion.

// 批量删除带有版本号的文件(即bucket开启了多版本)
public static void BatchDelFileWithVersion() {
    // 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";
    DeleteObjectsRequest deleteObjectsRequest = new DeleteObjectsRequest(bucketName);
    // 设置要删除的key列表, 最多一次删除1000个
    ArrayList<KeyVersion> keyList = new ArrayList<>();
    // 传入要删除的文件名
    keyList.add(new KeyVersion("aaa.txt", "axbefagagaxxfafa"));
    keyList.add(new KeyVersion("bbb.mp4", "awcafa1faxg0lx"));
    keyList.add(new KeyVersion("ccc/ddd.jpg", "kafa1kxxaa2ymh"));
    deleteObjectsRequest.setKeys(keyList);
    // 批量删除文件
    try {
        DeleteObjectsResult deleteObjectsResult = cosclient.deleteObjects(deleteObjectsRequest);
        List<DeletedObject> deleteObjectResultArray = deleteObjectsResult.getDeletedObjects();
    } catch (MultiObjectDeleteException mde) {
        // 如果部分产出成功部分失败, 返回MultiObjectDeleteException
        List<DeletedObject> deleteObjects = mde.getDeletedObjects();
        List<DeleteError> deleteErrors = mde.getErrors();
    } catch (CosServiceException e) {
        // 如果是其他错误, 比如参数错误, 身份验证不过等会抛出CosServiceException
        e.printStackTrace();
    } catch (CosClientException e) {
        // 如果是客户端错误,比如连接不上COS
        e.printStackTrace();
    }
    // 关闭客户端
    cosclient.shutdown();
}
Also used : COSCredentials(com.qcloud.cos.auth.COSCredentials) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) 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) COSClient(com.qcloud.cos.COSClient) CosServiceException(com.qcloud.cos.exception.CosServiceException) MultiObjectDeleteException(com.qcloud.cos.exception.MultiObjectDeleteException) Region(com.qcloud.cos.region.Region) ArrayList(java.util.ArrayList) List(java.util.List) ClientConfig(com.qcloud.cos.ClientConfig) DeletedObject(com.qcloud.cos.model.DeleteObjectsResult.DeletedObject)

Example 2 with KeyVersion

use of com.qcloud.cos.model.DeleteObjectsRequest.KeyVersion 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 3 with KeyVersion

use of com.qcloud.cos.model.DeleteObjectsRequest.KeyVersion 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)

Example 4 with KeyVersion

use of com.qcloud.cos.model.DeleteObjectsRequest.KeyVersion in project cos-java-sdk-v5 by tencentyun.

the class MultiObjectDeleteXmlFactory method convertToXmlByteArray.

/**
 * Converts the specified {@link DeleteObjectsRequest} object to an XML fragment that
 * can be sent to Qcloud COS.
 *
 * @param rq
 *            The {@link DeleteObjectsRequest}
 */
public byte[] convertToXmlByteArray(DeleteObjectsRequest rq) throws CosClientException {
    XmlWriter xml = new XmlWriter();
    xml.start("Delete");
    if (rq.getQuiet()) {
        xml.start("Quiet").value("true").end();
    }
    for (KeyVersion keyVersion : rq.getKeys()) {
        writeKeyVersion(xml, keyVersion);
    }
    xml.end();
    return xml.getBytes();
}
Also used : KeyVersion(com.qcloud.cos.model.DeleteObjectsRequest.KeyVersion)

Example 5 with KeyVersion

use of com.qcloud.cos.model.DeleteObjectsRequest.KeyVersion in project cloud-sdk by mizhousoft.

the class COSObjectStorageServiceImpl method doDeleteObjects.

private void doDeleteObjects(String bucketName, Collection<String> objectNames) throws CloudSDKException {
    DeleteObjectsRequest deleteObjectsRequest = new DeleteObjectsRequest(bucketName);
    Set<String> names = new HashSet<>(objectNames);
    // 设置要删除的key列表, 最多一次删除1000个
    List<KeyVersion> keyList = new ArrayList<>();
    for (String objectName : names) {
        // 传入要删除的文件名
        keyList.add(new KeyVersion(objectName));
    }
    deleteObjectsRequest.setKeys(keyList);
    try {
        cosClient.deleteObjects(deleteObjectsRequest);
    // List<DeletedObject> deletedObjects = deleteResult.getDeletedObjects();
    } catch (MultiObjectDeleteException e) {
        // List<DeleteError> deleteErrors = mde.getErrors();
        throw new CloudSDKException(e.getMessage(), e);
    } catch (CosServiceException e) {
        throw new CloudSDKException(e.getMessage(), e);
    } catch (Throwable e) {
        throw new CloudSDKException(e.getMessage(), e);
    }
}
Also used : CloudSDKException(com.mizhousoft.cloudsdk.CloudSDKException) CosServiceException(com.qcloud.cos.exception.CosServiceException) KeyVersion(com.qcloud.cos.model.DeleteObjectsRequest.KeyVersion) MultiObjectDeleteException(com.qcloud.cos.exception.MultiObjectDeleteException) ArrayList(java.util.ArrayList) DeleteObjectsRequest(com.qcloud.cos.model.DeleteObjectsRequest) HashSet(java.util.HashSet)

Aggregations

KeyVersion (com.qcloud.cos.model.DeleteObjectsRequest.KeyVersion)6 DeleteObjectsRequest (com.qcloud.cos.model.DeleteObjectsRequest)5 ArrayList (java.util.ArrayList)5 CosServiceException (com.qcloud.cos.exception.CosServiceException)4 MultiObjectDeleteException (com.qcloud.cos.exception.MultiObjectDeleteException)4 DeleteObjectsResult (com.qcloud.cos.model.DeleteObjectsResult)4 CosClientException (com.qcloud.cos.exception.CosClientException)3 DeletedObject (com.qcloud.cos.model.DeleteObjectsResult.DeletedObject)3 List (java.util.List)3 COSClient (com.qcloud.cos.COSClient)2 ClientConfig (com.qcloud.cos.ClientConfig)2 BasicCOSCredentials (com.qcloud.cos.auth.BasicCOSCredentials)2 COSCredentials (com.qcloud.cos.auth.COSCredentials)2 BucketVersioningConfiguration (com.qcloud.cos.model.BucketVersioningConfiguration)2 PutObjectResult (com.qcloud.cos.model.PutObjectResult)2 SetBucketVersioningConfigurationRequest (com.qcloud.cos.model.SetBucketVersioningConfigurationRequest)2 Region (com.qcloud.cos.region.Region)2 File (java.io.File)2 Test (org.junit.Test)2 CloudSDKException (com.mizhousoft.cloudsdk.CloudSDKException)1