普通下载是指通过 getObject 方法下载单个对象(Object),支持将对象下载到内存中、下载到本地文件两种方式,同时下载对象时支持进度条、客户端限速以及重写 HTTP 响应头。
tos:GetObject
权限,具体操作,请参见权限配置指南。tos:GetObjectVersion
权限,具体操作,请参见权限配置指南。以下代码用于下载桶 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;
}
以下代码用于下载桶 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。