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

普通下载(C++ SDK)

最近更新时间2024.02.04 18:30:56

首次发布时间2022.04.08 10:56:30

普通下载是指通过 getObject 方法下载单个对象(Object),支持将对象下载到内存中、下载到本地文件两种方式,同时下载对象时支持进度条、客户端限速以及重写 HTTP 响应头。

注意事项

  • 下载对象前,您必须具有 tos:GetObject 权限,具体操作,请参见权限配置指南
  • 对于开启多版本的桶,下载指定版本对象时,您必须具有 tos:GetObjectVersion 权限,具体操作,请参见权限配置指南
  • 如果应用程序会在同一时刻大量下载同一个对象,您的访问速度会受到 TOS 带宽及地域的限制。建议您使用 CDN 产品,提升性能的同时也能降低您的成本。通过 CDN 访问 TOS 的详细信息,请参见使用 CDN 加速访问 TOS 资源

示例代码

下载对象到内存

以下代码用于下载桶 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);
    
    GetObjectV2Input input(bucketName, objectName);
    auto output = client.getObject(input);
    if (!output.isSuccess()) {
        // 异常处理
        std::cout << "GetObject failed." << output.error().String() << std::endl;
        // 释放网络等资源
        CloseClient();
        return -1;
    }
    auto stream = output.result().getContent();
    std::string ss;

    char streamBuffer[256];
    memset(streamBuffer, 0, 256);
    while (stream->good()) {
        stream->read(streamBuffer, 256);
        // 根据实际情况处理数据。
    }
    std::cout << "GetObject success. the object etag:" << output.result().getETags()  << 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";
    // 下载Object到本地文件examplefile.txt,并保存到指定的本地路径中,例如/localpath/examplefile.txt,如果指定的本地文件存在会覆盖,不存在则新建。
    std::string filePath = "/localpath/examplefile.txt";
    // 初始化网络等资源
    InitializeClient();
    // 创建交互的 client
    TosClientV2 client(region, accessKey, secretKey);

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

    std::cout << "GetObjectToFile success. the object etag:" << output.result().getETags()  << std::endl;

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

下载时重写 HTTP 响应头

以下代码用于下载桶 examplebucket 中的对象 exampledir/exampleobject.txt,并重写 HTTP 响应头。

#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);

    GetObjectV2Input input(bucketName, objectName);
    // 重写响应头
    input.setResponseCacheControl("no-cache");
    input.setResponseContentDisposition("attachment; filename=123.txt");
    //  input.setResponseContentEncoding("gzip");
    //  input.setResponseContentLanguage("en-US");
    //  input.setResponseContentType("text/plain");
    // 重写响应头中的 ResponseExpires
    // 方式一:通过 transGMTFormatStringToTime 将 string 类型的时间转换为 time_t 类型的 expires
    //    time_t expires = TimeUtils::transGMTFormatStringToTime("Sat, 1 Jan 2022 00:00:00 GMT");
    //    if (expires == -1){
    //        std::cout << "Check expires, transfer string type to time_t failed." << std::endl;
    //    }
    // 方式二:通过修改 time_t 结构体,得到 expires
    //        time_t expirationTime = time(nullptr);
    //        tm* gmtmExpiration = gmtime(&expirationTime);
    //        gmtmExpiration->tm_min = 0;
    //        gmtmExpiration->tm_hour = 0;
    //        gmtmExpiration->tm_sec = 0;
    //        gmtmExpiration->tm_mday = 1;
    //        gmtmExpiration->tm_mon = 0;
    //        gmtmExpiration->tm_wday = 6;
    //        gmtmExpiration->tm_year = gmtmExpiration->tm_year;
    //        auto expires = timegm(gmtmExpiration);
    //
    //        input.setResponseExpires(expires);
    auto output = client.getObject(input);
    if (!output.isSuccess()) {
        // 异常处理
        std::cout << "GetObject failed." << output.error().String() << std::endl;
        // 释放网络等资源
        CloseClient();
        return -1;
    }

    std::cout << "GetObject success. the object etag:" << output.result().getETags()  << std::endl;

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

配置进度条

以下代码用于配置下载进度条。

#include "TosClientV2.h"
using namespace VolcengineTos;

static void ProgressCallback(std::shared_ptr<DataTransferStatus> datatransferstatus) {
    int64_t consumedBytes = datatransferstatus->consumedBytes_;
    int64_t totalBytes = datatransferstatus->totalBytes_;
    int64_t rwOnceBytes = datatransferstatus->rwOnceBytes_;
    DataTransferType type = datatransferstatus->type_;
    int64_t rate = 100 * consumedBytes / totalBytes;
    std::cout << "rate:" << rate << ","
              << "ConsumedBytes:" << consumedBytes << ","
              << "totalBytes:" << totalBytes << ","
              << "rwOnceBytes:" << rwOnceBytes << ","
              << "DataTransferType:" << type << std::endl;
}

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);
    GetObjectV2Input input(bucketName, objectName);

    // 设置进度条
    DataTransferListener processHandler = {ProgressCallback, nullptr};
    input.setDataTransferListener(processHandler);

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

    std::cout << "GetObject success. the object etag:" << output.result().getETags()  << 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";
    // 填写Object完整路径,完整路径中不能包含Bucket名称,例如exampledir/exampleobject.txt。
    std::string objectName = "exampledir/exampleobject.txt";

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

    GetObjectV2Input input(bucketName, objectName);
    // 设置 rateLimiter
    std::shared_ptr<RateLimiter> RateLimiter(NewRateLimiter(20 * 1024 * 1024, 5 * 1024 * 1024));
    input.setRateLimiter(RateLimiter);

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

    std::cout << "GetObject success. the object etag:" << output.result().getETags()  << std::endl;

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

相关文档

关于下载对象的 API 文档,请参见 GetObject