本文介绍如何在受版本控制的存储空间(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的情况下调用delete_object接口时,OSS会根据params参数中指定的versionId参数永久删除该版本。如果要删除ID为“null”的版本,则调用delete_object接口时,在params参数中添加params['versionId'] = “null”,OSS将“null”字符串当成“null”版本的ID号,从而删除版本ID为“null”的Object。

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

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

    using Aliyun.OSS;
    var endpoint = "<yourEndpoint>";
    var accessKeyId = "<yourAccessKeyId>";
    var accessKeySecret = "<yourAccessKeySecret>";
    var bucketName = "<yourBucketName>";
    var objectName = "<yourObjectName>";
    var versionid = "<yourObjectVersionidOrDelMarkerVersionid>";
    // 创建OssClient实例。
    var client = new OssClient(endpoint, accessKeyId, accessKeySecret);
    try
    {
        //可以传入文件版本id,也可以传入删除标记的版本id。
        var request = new DeleteObjectRequest(bucketName, key)
        {
            VersionId = versionid
        };
        client.DeleteObject(request);
        Console.WriteLine("Delete object succeeded");
    }
    catch (Exception ex)
    {
        Console.WriteLine("Delete object failed. {0}", ex.Message);
    }
  • 不指定versionId临时删除Object

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

    using Aliyun.OSS;
    var endpoint = "<yourEndpoint>";
    var accessKeyId = "<yourAccessKeyId>";
    var accessKeySecret = "<yourAccessKeySecret>";
    var bucketName = "<yourBucketName>";
    var objectName = "<yourObjectName>";
    // 创建OssClient实例。
    var client = new OssClient(endpoint, accessKeyId, accessKeySecret);
    try
    {
        // 在受版本控制状态下的Bucket中,此方法为临时删除,将会给object添加删除标记。
        var result = client.DeleteObject(bucketName, objectName);
        Console.WriteLine("Delete object succeeded, versionid: {0}, DeleteMarker: {1}", result.VersionId, result.DeleteMarker);
    }
    catch (Exception ex)
    {
        Console.WriteLine("Delete object failed. {0}", ex.Message);
    }

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

删除多个文件

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

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

  • 不指定versionID删除多个Object

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

    using Aliyun.OSS;
    var endpoint = "<yourEndpoint>";
    var accessKeyId = "<yourAccessKeyId>";
    var accessKeySecret = "<yourAccessKeySecret>";
    var bucketName = "<yourBucketName>";
    // 创建OssClient实例。
    var client = new OssClient(endpoint, accessKeyId, accessKeySecret);
    try
    {
        var keys = new List<string>();
        var listResult = client.ListObjects(bucketName);
        foreach (var summary in listResult.ObjectSummaries)
        {
            keys.Add(summary.Key);
        }
        // quietMode为true表示简单模式,即返回删除失败的文件列表。quietMode为false表示详细模式,即返回删除成功的文件列表。默认为详细模式。
        var quietMode = false;
        // DeleteObjectsRequest的第三个参数指定返回模式。
        var request = new DeleteObjectsRequest(bucketName, keys, quietMode);
        // 删除多个文件。
        var result = client.DeleteObjects(request);
        if ((!quietMode) && (result.Keys != null))
        {
            foreach (var obj in result.Keys)
            {
                Console.WriteLine("Delete successfully : {0} ", obj.Key);
            }
        }
        Console.WriteLine("Delete objects succeeded");
    }
    catch (Exception ex)
    {
        Console.WriteLine("Delete objects failed. {0}", ex.Message);
    }
  • 指定versionId删除多个Object

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

    using Aliyun.OSS;
    using Aliyun.OSS.Common;
    var endpoint = "<yourEndpoint>";
    var accessKeyId = "<yourAccessKeyId>";
    var accessKeySecret = "<yourAccessKeySecret>";
    var bucketName = "<yourBucketName>";
    // 创建OSSClient实例。
    var client = new OssClient(endpoint, accessKeyId, accessKeySecret);
    try
    {
        //删除指定版本的object或指定删除标记版本关联的object。
        var obj1 = new ObjectIdentifier
        {
            Key = "yourObject1Name",
            VersionId  = "yourObject1NameVersionid"
        };
    
        var obj2 = new ObjectIdentifier
        {
            Key = "yourObject2Name",
            VersionId  = "yourObject2DelMarkerVersionid"
        };
    
        IList<ObjectIdentifier> objects = new List<ObjectIdentifier>();
        objects.Add(obj1);
        objects.Add(obj2);
    
        var request = new DeleteObjectVersionsRequest(bucketName, objects);
    
        //发起deleteVersions请求。
        client.DeleteObjectVersions(request);
        Console.WriteLine("DeleteObjectVersions succeeded ");
    }
    catch (OssException ex)
    {
        Console.WriteLine("Failed with error code: {0}; Error info: {1}. \nRequestID:{2}\tHostID:{3}",
            ex.ErrorCode, ex.Message, ex.RequestId, ex.HostId);
    }
    catch (Exception ex)
    {
        Console.WriteLine("Failed with error info: {0}", ex.Message);
    }

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

删除指定前缀的文件

以下代码用于删除指定前缀(prefix)的文件:

using Aliyun.OSS;
using Aliyun.OSS.Common;
var endpoint = "<yourEndpoint>";
var accessKeyId = "<yourAccessKeyId>";
var accessKeySecret = "<yourAccessKeySecret>";
var bucketName = "<yourBucketName>";
var prefix = "<yourkeyPrefix>";
// 创建OssClient实例。
var client = new OssClient(endpoint, accessKeyId, accessKeySecret);
try
{   
    ObjectVersionList result = null;
    var request = new ListObjectVersionsRequest(bucketName)
    {
        //指定前缀。
        Prefix = prefix;
    };

    //列举所有指定前缀的文件的版本信息并删除。
    do {        
        result = client.ListObjectVersions(request);
        Console.WriteLine("ListObjectVersions succeeded");
        foreach (var deleteversion in result.DeleteMarkerSummaries)
        {
            var request = new DeleteObjectRequest(bucketName, deleteversion.Key)
            {
                VersionId = deleteversion.VersionId
            };
            client.DeleteObject(request);
        }

        foreach (var objectversion in result.ObjectVersionSummaries)
        {
            var request = new DeleteObjectRequest(bucketName, objectversion.Key)
            {
                VersionId = objectversion.VersionId
            };
            client.DeleteObject(request);            
        }
        request.KeyMarker = result.NextKeyMarker;
        request.NextVersionIdMarker = result.NextVersionIdMarker ;
    } while (result.IsTruncated)
}
catch (OssException ex)
{
    Console.WriteLine("Failed with error code: {0}; Error info: {1}. \nRequestID:{2}\tHostID:{3}",
        ex.ErrorCode, ex.Message, ex.RequestId, ex.HostId);
}
catch (Exception ex)
{
    Console.WriteLine("Failed with error info: {0}", ex.Message);
}