You need to enable JavaScript to run this app.
导航

目录场景(C++ SDK)

最近更新时间2024.02.04 18:31:04

首次发布时间2023.01.29 15:35:46

TOS 只有对象的概念,内部使用扁平结构存储数据。为方便您对对象进行分组并简化管理,您可以使用目录层次来组织对象。

创建目录

TOS 只有对象的概念, 可通过创建一个大小为 0 并且以斜线 / 结尾的对象, 模拟目录的功能。
以下代码用于在桶 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";
    // 填写Object完整路径,完整路径中不能包含Bucket名称,例如exampledir/。
    std::string objectName = "exampledir/";

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

    auto ss = std::make_shared<std::stringstream>("");
    PutObjectV2Input input(bucketName, objectName, ss);
    auto output = client.putObject(input);
    if (!output.isSuccess()) {
        // 异常处理
        std::cout << "PutObject failed." << output.error().String() << std::endl;
        // 释放网络等资源
        CloseClient();
        return -1;
    }
     std::cout << "PutObject success. the object etag:" << output.result().getETag() << std::endl;
    // 释放网络等资源
    CloseClient();
    return 0;
}

模拟目录查询

通过 delimiterprefix 两个参数可以模拟目录查询功能:

  1. 首先设置 delimiter/ 同时设置 prefix 为空,可返回根目录下的对象和子目录信息。
  2. 再设置 delimiter/ 同时设置 prefix 为子目录(subfiledir),可返回子目录的对象和次级目录。

以下代码用于列举桶 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/";
    input.setDelimiter("/");
    bool isTruncated = true;
    std::string nextContinuationToken = "";
    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();

        std::cout << "ListObjects success." << std::endl;
        for (const auto& object : output.result().getContents()) {
            std::cout << "object"<<
                    ",name:" << object.getKey() <<
                    ",size:" << object.getSize() <<
                    ",lastmodify time:" << object.getStringFormatLastModified() <<
                    ",storage class:" << object.getStringFormatStorageClass() << std::endl;
        }
        for (const auto& prefix : output.result().getCommonPrefixes()) {
            std::cout << "subDir is:"<< prefix.getPrefix() << std::endl;
        }
    }

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

删除目录

以下代码用于删除桶 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(output.error().getStatusCode() != 404 && output.error().getCode() != "NoSuchKey"){
                    std::cout << "DeleteObject failed:" << deleteOutput.error().String() << std::endl;
                    // 释放网络等资源
                    CloseClient();
                    return -1;
                }
            }
        }
    }

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