You need to enable JavaScript to run this app.
导航
删除场景(C++ SDK)
最近更新时间:2024.02.04 18:31:04首次发布时间:2023.01.29 15:35:46

本文介绍删除对象的常用场景代码。

注意事项

  • 要删除对象,您的账号必须具备 tos:DeleteObject 权限,具体操作请参见权限配置指南
  • 要删除多版本对象,您的账号必须具备tos:DeleteObjectVersion权限,具体操作请参见权限配置指南
  • 请您谨慎使用删除操作,文件删除后将无法恢复。

删除目录

以下代码用于删除桶 examplebucket 中目录 exampledir/ 的所有数据。

#include "TosClientV2.h"
using namespace VolcengineTos;

int main(void){
    // 初始化 TOS 账号信息
    // Your Region 填写 Bucket 所在 Region
    std::string region = "Your Region";
    std::string accessKey = std::getenv("TOS_ACCESS_KEY");
    std::string secretKey = std::getenv("TOS_SECRET_KEY");
    // 填写 Bucket 名称,例如 examplebucket
    std::string bucketName = "examplebucket";
    // 初始化网络等资源
    InitializeClient();
    // 创建交互的 client
    TosClientV2 client(region, accessKey, secretKey);

    ListObjectsType2Input input(bucketName);
    std::string prefix = "exampledir/";
    bool isTruncated = true;
    std::string nextContinuationToken = "";

    DeleteObjectInput deleteInput;
    deleteInput.setBucket(bucketName);
    while(isTruncated){
        input.setContinuationToken(nextContinuationToken);
        input.setPrefix(prefix);
        auto output = client.listObjectsType2(input);
        if (!output.isSuccess()) {
            // 异常处理
            std::cout << "ListObjects failed." << output.error().String() << std::endl;
            // 释放网络等资源
            CloseClient();
            return -1;
        }

        nextContinuationToken = output.result().getNextContinuationToken();
        isTruncated = output.result().isTruncated();
        
        for (const auto& object : output.result().getContents()) {
            deleteInput.setKey(object.getKey());
            auto deleteOutput = client.deleteObject(deleteInput);
            if (!deleteOutput.isSuccess()) {
                if(deleteOutput.error().getStatusCode() != 404 && output.error().getCode() != "NoSuchKey"){
                    std::cout << "DeleteObject failed:" << deleteOutput.error().String() << std::endl;
                    // 释放网络等资源
                    CloseClient();
                    return -1;
                }
            }
        }
    }

    // 释放网络等资源
    CloseClient();
    return 0;
}

清空桶

以下代码用于清空整个桶,包括删除所有对象、删除所有未合并的对象。

#include "TosClientV2.h"
using namespace VolcengineTos;

int main(void){
    // 初始化 TOS 账号信息
    // Your Region 填写 Bucket 所在 Region
    std::string region = "Your Region";
    std::string accessKey = std::getenv("TOS_ACCESS_KEY");
    std::string secretKey = std::getenv("TOS_SECRET_KEY");
    // 填写 Bucket 名称,例如 examplebucket
    std::string bucketName = "examplebucket";
    // 初始化网络等资源
    InitializeClient();
    // 创建交互的 client
    TosClientV2 client(region, accessKey, secretKey);

    ListObjectVersionsV2Input input(bucketName);
    bool isTruncated = true;
    std::string nextKeyMarker = "";

    DeleteObjectInput deleteObjectInput;
    deleteObjectInput.setBucket(bucketName);
    while(isTruncated){
        input.setKeyMarker(nextKeyMarker);
        auto output = client.listObjectVersions(input);
        if (!output.isSuccess()) {
            // 异常处理
            std::cout << "ListObjects failed." << output.error().String() << std::endl;
            // 释放网络等资源
            CloseClient();
            return -1;
        }
        nextKeyMarker = output.result().getNextKeyMarker();
        isTruncated = output.result().isTruncated();


        for (const auto& versions : output.result().getVersions()) {
            deleteObjectInput.setKey(versions.getKey());
            auto deleteOutput = client.deleteObject(deleteObjectInput);
            if (!deleteOutput.isSuccess()){
                if(deleteOutput.error().getStatusCode() != 404 && output.error().getCode() != "NoSuchKey"){
                    std::cout << "DeleteObject failed:" << deleteOutput.error().String() << std::endl;
                    // 释放网络等资源
                    CloseClient();
                    return -1;
                }
            }

        }
        for (const auto& deleteMarker : output.result().getDeleteMarkers()) {
            deleteObjectInput.setKey(deleteMarker.getKey());
            auto deleteOutput = client.deleteObject(deleteObjectInput);
            if (!deleteOutput.isSuccess()){
                if(deleteOutput.error().getStatusCode() != 404 && output.error().getCode() != "NoSuchKey"){
                    std::cout << "DeleteObject failed:" << deleteOutput.error().String() << std::endl;
                    // 释放网络等资源
                    CloseClient();
                    return -1;
                }
            }
        }
    }

    // 列举已上传分片
    ListMultipartUploadsV2Input input2(bucketName);
    AbortMultipartUploadInput abortMultipartUploadInput;
    abortMultipartUploadInput.setBucket(bucketName);
    // isTruncated用于控制是否列举完全,nextKeyMarker用于递归列举
    isTruncated = true;
    nextKeyMarker = "";
    while(isTruncated){
        input2.setKeyMarker(nextKeyMarker);
        auto output = client.listMultipartUploads(input2);
        if (!output.isSuccess()) {
            // 异常处理
            std::cout << "ListMultipartUploads failed." << output.error().String() << std::endl;
            // 释放网络等资源
            CloseClient();
            return -1;
        }

        nextKeyMarker = output.result().getNextKeyMarker();
        isTruncated = output.result().isTruncated();

        std::cout << "ListMultipartUploads success." << std::endl;
        for (const auto& part : output.result().getUploads()) {
            abortMultipartUploadInput.setKey(part.getKey());
            abortMultipartUploadInput.setUploadId(part.getUploadId());
            auto abortOutput = client.abortMultipartUpload(abortMultipartUploadInput);
            if (!abortOutput.isSuccess()){
                std::cout << "DeletePart failed:" << abortOutput.error().String() << std::endl;
                // 释放网络等资源
                CloseClient();
                return -1;
            }
        }
    }
    
    // 释放网络等资源
    CloseClient();
    return 0;
}

清空开启多版本的桶

以下代码用于清空整个开启多版本桶的示例代码,包括删除所有对象的多个版本、删除删除标记的多个版本、删除所有未合并的对象。

#include "TosClientV2.h"
using namespace VolcengineTos;

int main(void){
    // 初始化 TOS 账号信息
    // Your Region 填写 Bucket 所在 Region
    std::string region = "Your Region";
    std::string accessKey = std::getenv("TOS_ACCESS_KEY");
    std::string secretKey = std::getenv("TOS_SECRET_KEY");
    // 填写 Bucket 名称,例如 examplebucket
    std::string bucketName = "examplebucket";
    // 初始化网络等资源
    InitializeClient();
    // 创建交互的 client
    TosClientV2 client(region, accessKey, secretKey);

    ListObjectVersionsV2Input input(bucketName);
    bool isTruncated = true;
    std::string nextKeyMarker = "";
    std::string nextVersionIdMarker = "";
    DeleteObjectInput deleteObjectInput;
    deleteObjectInput.setBucket(bucketName);
    while(isTruncated){
        input.setKeyMarker(nextKeyMarker);
        input.setVersionIdMarker(nextVersionIdMarker);
        auto output = client.listObjectVersions(input);
        if (!output.isSuccess()) {
            // 异常处理
            std::cout << "ListObjects failed." << output.error().String() << std::endl;
            // 释放网络等资源
            CloseClient();
            return -1;
        }
        nextKeyMarker = output.result().getNextKeyMarker();
        nextVersionIdMarker = output.result().getNextVersionIdMarker();
        isTruncated = output.result().isTruncated();

        for (const auto& versions : output.result().getVersions()) {
            deleteObjectInput.setKey(versions.getKey());
            deleteObjectInput.setVersionID(versions.getVersionId());
            auto deleteOutput = client.deleteObject(deleteObjectInput);
            if (!deleteOutput.isSuccess()){
                if(deleteOutput.error().getStatusCode() != 404 && output.error().getCode() != "NoSuchKey"){
                    std::cout << "DeleteObject failed:" << deleteOutput.error().String() << std::endl;
                    // 释放网络等资源
                    CloseClient();
                    return -1;
                }
            }

        }
        for (const auto& deleteMarker : output.result().getDeleteMarkers()) {
            deleteObjectInput.setKey(deleteMarker.getKey());
            deleteObjectInput.setVersionID(deleteMarker.getVersionId());
            auto deleteOutput = client.deleteObject(deleteObjectInput);
            if (!deleteOutput.isSuccess()){
                if(deleteOutput.error().getStatusCode() != 404 && output.error().getCode() != "NoSuchKey"){
                    std::cout << "DeleteObject failed:" << deleteOutput.error().String() << std::endl;
                    // 释放网络等资源
                    CloseClient();
                    return -1;
                }
            }
        }
    }

    // 列举已上传分片
    ListMultipartUploadsV2Input input2(bucketName);
    AbortMultipartUploadInput abortMultipartUploadInput;
    abortMultipartUploadInput.setBucket(bucketName);
    // isTruncated用于控制是否列举完全,nextKeyMarker用于递归列举
    isTruncated = true;
    nextKeyMarker = "";
    while(isTruncated){
        input2.setKeyMarker(nextKeyMarker);
        auto output = client.listMultipartUploads(input2);
        if (!output.isSuccess()) {
            // 异常处理
            std::cout << "ListMultipartUploads failed." << output.error().String() << std::endl;
            // 释放网络等资源
            CloseClient();
            return -1;
        }
        nextKeyMarker = output.result().getNextKeyMarker();
        isTruncated = output.result().isTruncated();

        std::cout << "ListMultipartUploads success." << std::endl;
        for (const auto& part : output.result().getUploads()) {
            abortMultipartUploadInput.setKey(part.getKey());
            abortMultipartUploadInput.setUploadId(part.getUploadId());
            auto abortOutput = client.abortMultipartUpload(abortMultipartUploadInput);
            if (!abortOutput.isSuccess()){
                std::cout << "DeletePart failed:" << abortOutput.error().String() << std::endl;
                // 释放网络等资源
                CloseClient();
                return -1;
            }
        }
    }
    // 释放网络等资源
    CloseClient();
    return 0;
}