Delete objects (OSS C++ SDK V2)

更新时间:
复制 MD 格式

Delete objects that you no longer need to avoid unnecessary storage costs. You can delete a single object or multiple objects at a time by using the OSS C++ SDK V2.

Usage notes

  • Before you run the sample code, replace <region> with the actual region, such as cn-hangzhou.

  • The sample code obtains access credentials from environment variables.

  • To delete an object, you must have the oss:DeleteObject permission.

Warning

Deleted objects cannot be restored unless versioning is enabled for the bucket. Proceed with caution.

Sample code

Delete a single object

The following code deletes a specified object from a bucket:

#include <iostream>
#include "alibabacloud/oss2/ClientConfiguration.h"
#include "alibabacloud/oss2/OSSClient.h"
#include "alibabacloud/oss2/credentials/CredentialsProvider.h"

namespace oss = alibabacloud::oss2;

int main() {
    auto conf = oss::ClientConfiguration::loadDefault();
    conf.region = "<region>";
    conf.credentialsProvider = std::make_shared<oss::EnvironmentVariableCredentialsProvider>();

    oss::OSSClient client(conf);

    auto outcome = client.deleteObject(
        oss::models::DeleteObjectRequest()
            .setBucket("examplebucket")
            .setKey("exampledir/exampleobject.txt"));

    if (!outcome.has_value()) {
        auto& e = outcome.error();
        std::cerr << "Failed to delete the object"
                  << ", code: " << e.getCode()
                  << ", message: " << e.getMessage()
                  << ", requestId: " << e.getRequestId() << std::endl;
        return 1;
    }

    auto& result = outcome.value();
    std::cout << "The object was deleted successfully"
              << ", status: " << result.getStatusCode()
              << ", requestId: " << result.getRequestId() << std::endl;
    return 0;
}
          

Delete multiple objects

The following code deletes multiple objects in a single request:

#include <iostream>
#include "alibabacloud/oss2/ClientConfiguration.h"
#include "alibabacloud/oss2/OSSClient.h"
#include "alibabacloud/oss2/credentials/CredentialsProvider.h"

namespace oss = alibabacloud::oss2;

int main() {
    auto conf = oss::ClientConfiguration::loadDefault();
    conf.region = "<region>";
    conf.credentialsProvider = std::make_shared<oss::EnvironmentVariableCredentialsProvider>();

    oss::OSSClient client(conf);

    oss::models::Delete del;
    del.objects.push_back(oss::models::ObjectIdentifier().setKey("exampleobject1.txt"));
    del.objects.push_back(oss::models::ObjectIdentifier().setKey("exampleobject2.txt"));
    del.objects.push_back(oss::models::ObjectIdentifier().setKey("exampleobject3.txt"));
    del.quiet = false;

    auto outcome = client.deleteMultipleObjects(
        oss::models::DeleteMultipleObjectsRequest()
            .setBucket("examplebucket")
            .setDelete(del));

    if (!outcome.has_value()) {
        auto& e = outcome.error();
        std::cerr << "Failed to delete the objects"
                  << ", code: " << e.getCode()
                  << ", message: " << e.getMessage()
                  << ", requestId: " << e.getRequestId() << std::endl;
        return 1;
    }

    auto& result = outcome.value();
    std::cout << "The objects were deleted successfully, status: " << result.getStatusCode() << std::endl;
    for (const auto& d : result.getDeletedObjects()) {
        std::cout << "Deleted: " << d.key << std::endl;
    }
    return 0;
}
          

Related documents