Tüm Ürünler
Search
Belge Merkezi

Object Storage Service:Delete files (C++ SDK)

Son Güncelleme:Oct 01, 2026

Bu konu, sürüm oluşturmanın etkinleştirildiği bir paket içinde tek bir dosyanın (nesne), birden fazla dosyanın ve belirtilen bir öneke sahip dosyaların nasıl silineceğini açıklar.

Notlar

  • Bu konuda Çin (Hangzhou) bölgesinin genel uç noktası kullanılmaktadır. OSS ile aynı bölgedeki diğer Alibaba Cloud hizmetlerinden OSS'e erişmek için dahili bir uç nokta kullanın. OSS bölgeleri ve uç noktaları hakkında daha fazla bilgi için bkz. Bölgeler ve uç noktalar.

  • Bu konu, bir OSS uç noktası kullanarak bir OssClient örneğinin nasıl oluşturulacağını göstermektedir. Özel alan adı kullanma veya Security Token Service (STS) kimlik bilgileriyle kimlik doğrulama gibi alternatif yapılandırmalar için bkz. OssClient örneği oluşturma.

  • Bir nesneyi silmek için oss:DeleteObject iznine sahip olmanız gerekir. Daha fazla bilgi için bkz. Özel ilke atama.

Sürümlü paketlerde silme davranışı

Sürümlü bir paketten bir nesne sildiğinizde, bir sürüm kimliği belirtip belirtmeyeceğinize karar vermeniz gerekir.

  • Sürüm kimliği belirtmeden bir nesneyi silme (geçici silme)

    Bir sürüm kimliği belirtmezseniz OSS mevcut sürümü silmez. Bunun yerine, en son sürüm olarak bir silme belirteci ekler. Sonraki GetObject istekleri, yanıtta 404 Not Found durumuyla birlikte header:x-oss-delete-marker = true ve silme belirtecinin x-oss-version-id değerini döndürür.

    x-oss-delete-marker değeri true olduğunda, x-oss-version-id silme belirtecinin sürüm kimliğini döndürür.

  • Sürüm kimliği belirterek bir nesneyi silme (kalıcı silme)

    Bir sürüm kimliği belirtirseniz, OSS params içindeki versionId parametresini kullanarak bu sürümü kalıcı olarak siler. Null kimliğe sahip sürümü silmek için params içinde params['versionId'] = "null" ayarlayın. OSS, "null" dizesini hedef sürüm kimliği olarak değerlendirir.

Ada göre tek bir dosyayı silme

Bir dosyayı kalıcı olarak silme

Aşağıdaki kod, versionId'sini belirterek bir nesnenin belirli bir sürümünü kalıcı olarak nasıl sileceğinizi gösterir. Kalıcı olarak silinen nesne sürümleri kurtarılamaz.

#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;

int main(void)
{
    /* Initialize the OSS account information. */
            
    /* Set yourEndpoint to the endpoint of the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. */
    std::string Endpoint = "yourEndpoint";
    /* Set yourRegion to the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the region to cn-hangzhou. */
    std::string Region = "yourRegion";
    /* Specify the bucket name, for example, examplebucket. */
    std::string BucketName = "examplebucket";
    /* Specify the full path of the object. The full path cannot contain the bucket name. For example, exampledir/exampleobject.txt. */
    std::string ObjectName = "exampledir/exampleobject.txt";

    /* Initialize network resources. */
    InitializeSdk();

    ClientConfiguration conf;
    conf.signatureVersion = SignatureVersionType::V4;
    /* Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set. */
    auto credentialsProvider = std::make_shared<EnvironmentVariableCredentialsProvider>();
    OssClient client(Endpoint, credentialsProvider, conf);
    client.SetRegion(Region);

    /* Delete the object with the specified versionId or the object associated with the delete marker that has the specified versionId. */
    auto outcome = client.DeleteObject(DeleteObjectRequest(BucketName, ObjectName, "yourObjectVersionIdOrDeleteMarkerVersionId"));
    /* If the versionId of an object is specified, the returned delete_marker is None and the returned versionId is the versionId of the specified object. */
    /* If the versionId of a delete marker is specified, the returned delete_marker is True and the returned versionId is the versionId of the specified delete marker. */
    if (!outcome.isSuccess()) {
        /* Handle exceptions. */
        std::cout << "DeleteObject fail" <<
        ",code:" << outcome.error().Code() <<
        ",message:" << outcome.error().Message() <<
        ",requestId:" << outcome.error().RequestId() << std::endl;
        return -1;
    }

    /* Release network resources. */
    ShutdownSdk();
    return 0;
}

Bir dosyayı geçici olarak silme

Aşağıdaki kod, bir versionId belirtmeden bir nesneyi geçici olarak nasıl sileceğinizi gösterir. Geçici olarak silinen nesne sürümleri kurtarılabilir.

#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;

int main(void)
{
    /* Initialize the OSS account information. */
            
    /* Set yourEndpoint to the endpoint of the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. */
    std::string Endpoint = "yourEndpoint";
    /* Set yourRegion to the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the region to cn-hangzhou. */
    std::string Region = "yourRegion";
    /* Specify the bucket name, for example, examplebucket. */
    std::string BucketName = "examplebucket";
    /* Specify the full path of the object. The full path cannot contain the bucket name. For example, exampledir/exampleobject.txt. */
    std::string ObjectName = "exampledir/exampleobject.txt";

    /* Initialize network resources. */
    InitializeSdk();

    ClientConfiguration conf;
    conf.signatureVersion = SignatureVersionType::V4;
    /* Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set. */
    auto credentialsProvider = std::make_shared<EnvironmentVariableCredentialsProvider>();
    OssClient client(Endpoint, credentialsProvider, conf);
    client.SetRegion(Region);

    DeleteObjectRequest request(BucketName, ObjectName);

    /* Temporarily delete the object without specifying a versionId. This operation adds a delete marker to the object. */
    auto outcome = client.DeleteObject(request);
    /* View the versionId of the returned delete marker. */
    if (outcome.isSuccess()) {
        std::cout << "versionid:" << outcome.result().VersionId() << ",DeleteMarker:" << outcome.result().DeleteMarker() << std::endl;
    }
    else {
        /* Handle exceptions. */
        std::cout << "DeleteObject fail" <<
        ",code:" << outcome.error().Code() <<
        ",message:" << outcome.error().Message() <<
        ",requestId:" << outcome.error().RequestId() << std::endl;
        return -1;
    }

    /* Release network resources. */
    ShutdownSdk();
    return 0;
}

Ada göre birden fazla dosyayı silme

Dosyaları kalıcı olarak silme

Aşağıdaki kod, versionId'lerini belirterek birden fazla nesne sürümünü veya silme belirteçlerini versionId'lerini belirterek nasıl kalıcı olarak sileceğinizi gösterir. Kalıcı olarak silinen nesne sürümleri kurtarılamaz.

#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;

int main(void)
{
    /* Initialize the OSS account information. */
            
    /* Set yourEndpoint to the endpoint of the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. */
    std::string Endpoint = "yourEndpoint";
    /* Set yourRegion to the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the region to cn-hangzhou. */
    std::string Region = "yourRegion";
    /* Specify the bucket name, for example, examplebucket. */
    std::string BucketName = "examplebucket";

    /* Initialize network resources. */
    InitializeSdk();

    ClientConfiguration conf;
    conf.signatureVersion = SignatureVersionType::V4;
    /* Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set. */
    auto credentialsProvider = std::make_shared<EnvironmentVariableCredentialsProvider>();
    OssClient client(Endpoint, credentialsProvider, conf);
    client.SetRegion(Region);

    DeleteObjectVersionsRequest  request(BucketName);

    /* Add the versionIds of the objects or delete markers to delete. */
    ObjectIdentifier obj1("yourObject1Name");
    obj1.setVersionId("yourVersionId");

    ObjectIdentifier obj2("yourObject2Name");
    obj2.setVersionId("obj2_del_marker_versionid");

    request.addObject(obj1);
    request.addObject(obj2);

    /* Batch delete the objects with the specified versionIds or the objects associated with the specified delete marker versionIds. */
    auto outcome = client.DeleteObjectVersions(request);

    if (!outcome.isSuccess()) {
        /* Handle exceptions. */
        std::cout << "DeleteObjectVersions fail" <<
        ",code:" << outcome.error().Code() <<
        ",message:" << outcome.error().Message() <<
        ",requestId:" << outcome.error().RequestId() << std::endl;
        return -1;
    }

    /* Release network resources. */
    ShutdownSdk();
    return 0;
}

Dosyaları geçici olarak silme

Aşağıdaki kod, versionId'lerini belirtmeden birden fazla nesneyi geçici olarak nasıl sileceğinizi gösterir. Geçici olarak silinen nesne sürümleri kurtarılabilir.

#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;

int main(void)
{
    /* Initialize the OSS account information. */
            
    /* Set yourEndpoint to the endpoint of the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. */
    std::string Endpoint = "yourEndpoint";
    /* Set yourRegion to the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the region to cn-hangzhou. */
    std::string Region = "yourRegion";
    /* Specify the bucket name, for example, examplebucket. */
    std::string BucketName = "examplebucket";

    /* Initialize network resources. */
    InitializeSdk();

    ClientConfiguration conf;
    conf.signatureVersion = SignatureVersionType::V4;
    /* Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set. */
    auto credentialsProvider = std::make_shared<EnvironmentVariableCredentialsProvider>();
    OssClient client(Endpoint, credentialsProvider, conf);
    client.SetRegion(Region);

    DeleteObjectVersionsRequest request(BucketName);
    /* Specify the names of the objects to delete. */
    ObjectIdentifier obj1("ObjectName1");
    ObjectIdentifier obj2("ObjectName2");
    ObjectIdentifier obj3("ObjectName3");

    request.addObject(obj1);
    request.addObject(obj2);
    request.addObject(obj3);

    /* Delete the objects. */
    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 {
        /* Handle exceptions. */
        std::cout << "DeleteObjectVersions fail" <<
        ",code:" << outcome.error().Code() <<
        ",message:" << outcome.error().Message() <<
        ",requestId:" << outcome.error().RequestId() << std::endl;
        return -1;
    }

    /* Release network resources. */
    ShutdownSdk();
    return 0;
}

Belirtilen bir öneke sahip dosyaları silme

Aşağıdaki kod, belirtilen bir öneke sahip dosyaların nasıl silineceğini gösterir.

#include <alibabacloud/oss/OssClient.h>

using namespace AlibabaCloud::OSS;

int main(void)
{
    /* Initialize the OSS account information. */
            
    /* Set yourEndpoint to the endpoint of the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. */
    std::string Endpoint = "yourEndpoint";
    /* Set yourRegion to the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the region to cn-hangzhou. */
    std::string Region = "yourRegion";
    /* Specify the bucket name, for example, examplebucket. */
    std::string BucketName = "examplebucket";

    /* Initialize network resources. */
    InitializeSdk();

    ClientConfiguration conf;
    conf.signatureVersion = SignatureVersionType::V4;
    /* Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set. */
    auto credentialsProvider = std::make_shared<EnvironmentVariableCredentialsProvider>();
    OssClient client(Endpoint, credentialsProvider, conf);
    client.SetRegion(Region);

    ListObjectVersionsRequest request(BucketName);
    bool IsTruncated = false;

    do {
            request.setPrefix("yourkeyPrefix");
            auto outcome = client.ListObjectVersions(request);

            if (outcome.isSuccess()) {
                /* View the version information of the listed objects and delete markers. */
                for (auto const &marker : outcome.result().DeleteMarkerSummarys()) {
                   client.DeleteObject(DeleteObjectRequest(BucketName, marker.Key(), marker.VersionId()));
                }

                /* List the version information of all objects with the specified prefix and delete these files. */
                for (auto const &obj : outcome.result().ObjectVersionSummarys()) {
                     client.DeleteObject(DeleteObjectRequest(BucketName, obj.Key(), obj.VersionId()));
                }
            }
            else {
                std::cout << "ListObjectVersions fail" <<
                ",code:" << outcome.error().Code() <<
                ",message:" << outcome.error().Message() <<
                ",requestId:" << outcome.error().RequestId() << std::endl;
                break;
            }
            request.setKeyMarker(outcome.result().NextKeyMarker());
            request.setVersionIdMarker(outcome.result().NextVersionIdMarker());
            IsTruncated = outcome.result().IsTruncated();
    } while (IsTruncated);

    /* Release network resources. */
    ShutdownSdk();
    return 0;
}

İlgili konular

  • Tek bir dosyayı silmek için kullanılan API işlemi hakkında daha fazla bilgi için bkz. DeleteObject.

  • Birden fazla dosyayı silmek için kullanılan API işlemi hakkında daha fazla bilgi için bkz. DeleteMultipleObjects.