本文介绍如何在受版本控制的存储空间(Bucket)中删除文件(Object)。

删除单个文件

对于开启版本控制的Bucket执行 DeleteObject操作时,如果未指定versionId ,则保留Object的当前版本,并插入删除标记(Delete Marker)。如果指定versionId,则永久删除该指定版本ID的Object。

说明
  • 未指定versionId:

    如果在未指定versionId的情况下执行DeleteObject操作时,默认仅作用于Object的当前版本,但不会直接删除该Object的当前版本,而是插入一个删除标记(Delete Marker),并保留原来的当前版本。当执行GetObject操作时,OSS会检测到当前版本为删除标记,并返回404 Not Found。此外,响应中会返回header:x-oss-delete-marker = true以及新生成的删除标记的版本号x-oss-version-id。

    x-oss-delete-marker的值为true,表示与返回的x-oss-version-id对应的版本为删除标记。

  • 指定versionId:

    如果在指定versionId的情况下调用DeleteObject接口时,OSS会根据params参数中指定的versionId参数永久删除该版本。如果要删除ID为“null”的版本,则调用DeleteObject接口时,在params参数中添加 params['versionId'] =“null”,OSS将“null”字符串当成“null”版本的ID号,从而删除版本ID为“null”的Object。

以下提供了永久删除指定版本Object及临时删除Object的示例:
  • 指定versionId永久删除Object

    以下代码用于永久删除指定版本的Object:

    #include <alibabacloud/oss/OssClient.h>
    using namespace AlibabaCloud::OSS;
    
    int main(void)
    {
        /*初始化OSS账号信息*/
        std::string AccessKeyId = "yourAccessKeyId";
        std::string AccessKeySecret = "yourAccessKeySecret";
        std::string Endpoint = "yourEndpoint";
        std::string BucketName = "yourBucketName";
        std::string ObjectName = "yourObjectName";
    
        /* 初始化网络等资源 */
        InitializeSdk();
    
        ClientConfiguration conf;
        OssClient client(Endpoint, AccessKeyId, AccessKeySecret, conf);
    
        /* 删除指定版本id的object或删除指定删除标记版本id的object */
        auto outcome = client.DeleteObject(BucketName, ObjectName, "yourObjectVersionIdOrDeleteMarkerVersionId");
        /* 如果指定的是object的版本id,则返回的delete_marker为None且返回的versionid为前面指定的object版本id */
        /* 如果指定的是删除标记的版本id,则返回的delete_marker为True且返回的versionid为前面指定的删除标记的版本id */
        if (!outcome.isSuccess()) {
            /* 异常处理 */
            std::cout << "DeleteObject fail" <<
            ",code:" << outcome.error().Code() <<
            ",message:" << outcome.error().Message() <<
            ",requestId:" << outcome.error().RequestId() << std::endl;
            ShutdownSdk();
            return -1;
        }
    
        /* 释放网络等资源 */
        ShutdownSdk();
        return 0;
    }
  • 不指定versionId临时删除Object

    以下代码用于临时删除Object:

    #include <alibabacloud/oss/OssClient.h>
    using namespace AlibabaCloud::OSS;
    
    int main(void)
    {
        /*初始化OSS账号信息*/
        std::string AccessKeyId = "yourAccessKeyId";
        std::string AccessKeySecret = "yourAccessKeySecret";
        std::string Endpoint = "yourEndpoint";
        std::string BucketName = "yourBucketName";
        std::string ObjectName = "yourObjectName";
    
        /* 初始化网络等资源 */
        InitializeSdk();
    
        ClientConfiguration conf;
        OssClient client(Endpoint, AccessKeyId, AccessKeySecret, conf);
    
        DeleteObjectRequest request(BucketName, ObjectName);
    
        /* 不指定版本id删除object */
        auto outcome = client.DeleteObject(request);
        /* 查看返回删除标记的版本id */
        if (outcome.isSuccess()) {
            std::cout << "versionid:" << outcome.result().VersionId() << ",DeleteMarker:" << outcome.result().DeleteMarker() << std::endl;
        }
        else {
            /* 异常处理 */
            std::cout << "PutObject fail" <<
            ",code:" << outcome.error().Code() <<
            ",message:" << outcome.error().Message() <<
            ",requestId:" << outcome.error().RequestId() << std::endl;
            ShutdownSdk();
            return -1;
        }
    
        /* 释放网络等资源 */
        ShutdownSdk();
        return 0;
    }

删除单个文件的详细信息请参见DeleteObject

删除多个文件

在开启版本控制的Bucket中,您可以使用DeleteObjectVersions接口来批量删除Object。在Delete请求中如果没有指定versionId,将插入删除标记。如果指定了versionId,将永久删除该Object的指定版本。

以下提供了不指定versionID和指定versionId删除多个Object的示例:

  • 不指定versionID删除多个Object

    以下代码用于不指定versionId删除多个Object:

    #include <alibabacloud/oss/OssClient.h>
    using namespace AlibabaCloud::OSS;
    
    int main(void)
    {
        /*初始化OSS账号信息*/
        std::string AccessKeyId = "yourAccessKeyId";
        std::string AccessKeySecret = "yourAccessKeySecret";
        std::string Endpoint = "yourEndpoint";
        std::string BucketName = "yourBucketName";
        std::string ObjectName = "yourObjectName";
    
        /* 初始化网络等资源 */
        InitializeSdk();
    
        ClientConfiguration conf;
        OssClient client(Endpoint, AccessKeyId, AccessKeySecret, conf);
    
        DeleteObjectVersionsRequest request(BucketName);
        /* 添加要删除文件的文件名称 */
        ObjectIdentifier obj1(ObjectName1);
        ObjectIdentifier obj2(ObjectName2);
        ObjectIdentifier obj3(ObjectName3);
    
        request.addObject(obj1);
        request.addObject(obj2);
        request.addObject(obj3);
    
        /* 删除文件 */
        auto outcome = client.DeleteObjectVersions(request);
    
        if (outcome.isSuccess()) {
            for (auto const &obj : outcome.result().DeletedObjects()) {
                std::cout << "versionid:" << obj.VersionId() << ",DeleteMarker:" << obj.DeleteMarker() << std::endl;
            }
        }
        else {
            /* 异常处理 */
            std::cout << "DeleteObjectVersions fail" <<
            ",code:" << outcome.error().Code() <<
            ",message:" << outcome.error().Message() <<
            ",requestId:" << outcome.error().RequestId() << std::endl;
            ShutdownSdk();
            return -1;
        }
    
    
        /* 释放网络等资源 */
        ShutdownSdk();
        return 0;
    }
  • 指定versionId删除多个Object

    以下代码用于删除多个指定versionId的Object或指定删除标记versionId关联的Object:

    #include <alibabacloud/oss/OssClient.h>
    using namespace AlibabaCloud::OSS;
    
    int main(void)
    {
        /*初始化OSS账号信息*/
        std::string AccessKeyId = "yourAccessKeyId";
        std::string AccessKeySecret = "yourAccessKeySecret";
        std::string Endpoint = "yourEndpoint";
        std::string BucketName = "yourBucketName";
        std::string ObjectName = "yourObjectName";
    
        /* 初始化网络等资源 */
        InitializeSdk();
    
        ClientConfiguration conf;
        OssClient client(Endpoint, AccessKeyId, AccessKeySecret, conf);
    
        DeleteObjectVersionsRequest  request(BucketName);
    
        /*添加需要删除的object的版本id或者删除标记版本id*/
        ObjectIdentifier obj1("yourObject1Name");
        obj1.setVersionId("yourVersionId");
    
        ObjectIdentifier obj2("yourObject2Name");
        obj2.setVersionId("obj2_del_marker_versionid");
    
        request.addObject(obj1);
        request.addObject(obj2);
    
        /*批量删除指定版本id的object或批量删除指定删除标记版本id的object*/
        auto outcome = client.DeleteObjectVersions(request);
    
        if (!outcome.isSuccess()) {
            /* 异常处理 */
            std::cout << "DeleteObjectVersions fail" <<
            ",code:" << outcome.error().Code() <<
            ",message:" << outcome.error().Message() <<
            ",requestId:" << outcome.error().RequestId() << std::endl;
            ShutdownSdk();
            return -1;
        }
    
        /* 释放网络等资源 */
        ShutdownSdk();
        return 0;
    }

    删除多个文件的详细信息请参见DeleteMultipleObjects