You need to enable JavaScript to run this app.
导航
删除对象(C++ SDK)
最近更新时间:2024.02.04 18:30:56首次发布时间:2022.04.08 10:56:30

当对象不会再使用时,您可以删除对象,降低成本开销。目前 TOS C++ SDK 支持删除桶中的单个对象、删除指定的多个对象,对于开启多版本的桶,你可指定删除对象的任意版本数据。

注意事项

  • 删除对象时,您必须具备 tos:DeleteObject 权限,具体操作请参见权限配置指南
  • 删除多版本对象时,您必须具备 tos:DeleteObjectVersion 权限,具体操作请参见权限配置指南
  • 当桶的多版本状态是开启时,如果删除对象时不指定对象的版本,不会删除对象,将会产生一个新的版本号,并插入删除标记(DeleteMarker);当桶的多版本状态是 Suspended 时,删除版本为 null 的对象,并产生一个版本为 null 的删除标记。多版本状态下,指定版本删除将删除指定版本号的对象。

示例代码

删除指定对象

以下代码用于删除指定桶 examplebucket 中的 exampledir/exampleobject.txt 对象。

#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";
    // 填写 Object 完整路径,完整路径中不能包含 Bucket 名称,例如 exampledir/exampleobject.txt。
    std::string objectName = "exampledir/exampleobject.txt";
    
    // 初始化网络等资源
    InitializeClient();
    // 创建交互的 client
    TosClientV2 client(region, accessKey, secretKey);

    DeleteObjectInput input(bucketName,objectName);
    auto output = client.deleteObject(input);
    if (!output.isSuccess()) {
        // 异常处理
        std::cout << "DeleteObject failed." << output.error().String() << std::endl;
        // 释放网络等资源
        CloseClient();
        return -1;
    }

    std::cout << "DeleteObject success." << std::endl;
    std::cout << " version id is:" <<  output.result().getVersionId()
              << " request id is:" << output.result().getRequestInfo().getRequestId()
              << " is delete marker:" << output.result().isDeleteMarker() << std::endl;
    // 释放网络等资源
    CloseClient();
    return 0;
}

删除指定版本对象

以下代码用于删除指定开启多版本桶 examplebucket 中的 exampledir/exampleobject.txt 对象的指定版本数据。

#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";
    // 填写 Object 完整路径,完整路径中不能包含 Bucket 名称,例如 exampledir/exampleobject.txt。
    std::string objectName = "exampledir/exampleobject.txt";
    // 填写对象的 versionId
    std::string versionId = "your object version";

    // 初始化网络等资源
    InitializeClient();
    // 创建交互的 client
    TosClientV2 client(region, accessKey, secretKey);
    
    DeleteObjectInput input(bucketName,objectName);
    input.setVersionID(versionId);
    auto output = client.deleteObject(input);
    if (!output.isSuccess()) {
        // 异常处理
        std::cout << "DeleteObject failed." << output.error().String() << std::endl;
        // 释放网络等资源
        CloseClient();
        return -1;
    }

    std::cout << "DeleteObject success." << std::endl;
    std::cout << " version id is:" <<  output.result().getVersionId()
              << " is delete marker:" << output.result().isDeleteMarker() << std::endl;
    // 释放网络等资源
    CloseClient();
    return 0;
}

批量删除对象

注意

  • 在开启版本控制的桶中,调用 DeleteMultiObjects 接口来批量删除对象时,如果在 Delete 请求中未指定versionId,将插入删除标记。如果指定了versionId,将永久删除该对象的指定版本。
  • 批量删除对象支持的响应方式可以通过quiet进行设置:
    • quietfalse时,是指在返回响应时,不管对象是否删除成功都将删除结果包含在响应里。
    • quiettrue时,是指在返回响应时,只返回删除失败的对象结果,没有返回的认为删除成功。

批量删除对象
以下代码用于删除桶 examplebucket 指定对象名的多个对象数据。

#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";
    // 填写 Object 完整路径,完整路径中不能包含 Bucket 名称,例如 exampledir/exampleobject1.txt和exampledir/exampleobject2.txt。
    std::string objectName1 = "exampledir/exampleobject1.txt";
    std::string objectName2 = "exampledir/exampleobject2.txt";
    // 初始化网络等资源
    InitializeClient();
    // 创建交互的 client
    TosClientV2 client(region, accessKey, secretKey);

    DeleteMultiObjectsInput input;
    input.setBucket(bucketName);
    ObjectTobeDeleted object1(objectName1);
    ObjectTobeDeleted object2(objectName2);
    input.addObjectTobeDeleted(object1);
    input.addObjectTobeDeleted(object2);

    auto output = client.deleteMultiObjects(input);
    if (!output.isSuccess()) {
        // 异常处理
        std::cout << "DeleteMultiObjects failed." << output.error().String() << std::endl;
        // 释放网络等资源
        CloseClient();
        return -1;
    }

    std::cout << "DeleteMultiObjects success." << std::endl;
    for(auto& deleted : output.result().getDeleteds()){
        std::cout << " object key is:" << deleted.getKey()
                  << " object version id is:" <<  deleted.getVersionId()
                  << " is delete marker:" <<  deleted.isDeleteMarker()
                  << " delete marker version id is:" <<  deleted.getDeleteMarkerVersionId()<< std::endl;
    }
    for(auto& error : output.result().getErrors()){
        std::cout << " delete err object key is:" << error.getKey()
                  << " delete err object version id is:" <<  error.getVersionId()
                  << " delete err message:" <<  error.getMessage() << std::endl;
    }
    // 释放网络等资源
    CloseClient();
    return 0;
}

批量删除指定版本的对象
以下代码用于删除桶 examplebucket 指定对象名与版本号的多个对象数据。

#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";
    // 填写 Object 完整路径,完整路径中不能包含 Bucket 名称,例如exampledir/exampleobject1.txt和exampledir/exampleobject2.txt。
    std::string objectName1 = "exampledir/exampleobject1.txt";
    std::string objectName2 = "exampledir/exampleobject2.txt";
    // 填写对象的 versionId
    std::string versionId1 = "your object1 version";
    std::string versionId2 = "your object2 version";

    // 初始化网络等资源
    InitializeClient();
    // 创建交互的 client
    TosClientV2 client(region, accessKey, secretKey);

    DeleteMultiObjectsInput input;
    input.setBucket(bucketName);
    ObjectTobeDeleted object1(objectName1,versionId1);
    ObjectTobeDeleted object2(objectName2,versionId2);
    input.addObjectTobeDeleted(object1);
    input.addObjectTobeDeleted(object2);
    // 桶开启多版本并指定删除对象的版本号,则为直接标删除
    auto output = client.deleteMultiObjects(input);
    if (!output.isSuccess()) {
        // 异常处理
        std::cout << "DeleteMultiObjects failed." << output.error().String() << std::endl;
        // 释放网络等资源
        CloseClient();
        return -1;
    }

    std::cout << "DeleteMultiObjects success." << std::endl;
    for(auto& deleted : output.result().getDeleteds()){
        std::cout << " object key is:" << deleted.getKey()
                  << " object version id is:" <<  deleted.getVersionId()
                  << " is delete marker:" <<  deleted.isDeleteMarker()
                  << " delete marker version id is:" <<  deleted.getDeleteMarkerVersionId()<< std::endl;
    }
    for(auto& error : output.result().getErrors()){
        std::cout << " delete err object key is:" << error.getKey()
                  << " delete err object version id is:" <<  error.getVersionId()
                  << " delete err message:" <<  error.getMessage() << std::endl;
    }
    // 释放网络等资源
    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";
    // 填写列举的文件前缀(Prefix)。
    std::string prefix = "yourkeyPrefix";
    // 初始化网络等资源
    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());
            deleteObjectInput.setVersionID(versions.getVersionId());
            auto deleteOutput = client.deleteObject(deleteObjectInput);
            if (!deleteOutput.isSuccess()){
                if(output.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(output.error().getStatusCode() != 404 && output.error().getCode() != "NoSuchKey"){
                    std::cout << "DeleteObject failed:" << deleteOutput.error().String() << std::endl;
                    // 释放网络等资源
                    CloseClient();
                    return -1;
                }
            }
        }
    }

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

相关文档