Use the OSS C++ SDK V2 GetObject method to download files from OSS to memory, local disk, or a custom buffer.
Usage notes
-
Before you run the sample code, replace
<region>with your region, such ascn-hangzhou. -
The sample code obtains access credentials from environment variables.
-
To download a file, you must have the
oss:GetObjectpermission.
Sample code
Download a file to memory
The following code downloads file content to memory:
#include <iostream>
#include <sstream>
#include "alibabacloud/oss2/ClientConfiguration.h"
#include "alibabacloud/oss2/OSSClient.h"
#include "alibabacloud/oss2/credentials/CredentialsProvider.h"
namespace oss = alibabacloud::oss2;
int main() {
auto conf = oss::ClientConfiguration::loadDefault();
conf.region = "<region>";
conf.credentialsProvider = std::make_shared<oss::EnvironmentVariableCredentialsProvider>();
oss::OSSClient client(conf);
auto outcome = client.getObject(
oss::models::GetObjectRequest()
.setBucket("examplebucket")
.setKey("exampledir/exampleobject.txt"));
if (!outcome.has_value()) {
auto& e = outcome.error();
std::cerr << "Failed to download the file"
<< ", code: " << e.getCode()
<< ", message: " << e.getMessage()
<< ", requestId: " << e.getRequestId() << std::endl;
return 1;
}
auto& result = outcome.value();
std::cout << "status: " << result.getStatusCode()
<< ", requestId: " << result.getRequestId()
<< ", contentLength: " << result.getContentLength()
<< ", contentType: " << result.getContentType() << std::endl;
// Read the file content.
auto& body = result.getBody();
if (body) {
std::stringstream ss;
ss << body->rdbuf();
std::cout << "File content: " << ss.str() << std::endl;
}
return 0;
}
Download to a local file
The following code downloads a file to local disk:
#include <iostream>
#include "alibabacloud/oss2/ClientConfiguration.h"
#include "alibabacloud/oss2/OSSClient.h"
#include "alibabacloud/oss2/credentials/CredentialsProvider.h"
namespace oss = alibabacloud::oss2;
int main() {
auto conf = oss::ClientConfiguration::loadDefault();
conf.region = "<region>";
conf.credentialsProvider = std::make_shared<oss::EnvironmentVariableCredentialsProvider>();
oss::OSSClient client(conf);
std::string filePath = "/path/to/localfile.txt";
auto outcome = client.getObjectToFile(
oss::models::GetObjectRequest()
.setBucket("examplebucket")
.setKey("exampledir/exampleobject.txt"),
filePath);
if (!outcome.has_value()) {
auto& e = outcome.error();
std::cerr << "Failed to download the file"
<< ", code: " << e.getCode()
<< ", message: " << e.getMessage()
<< ", requestId: " << e.getRequestId() << std::endl;
return 1;
}
auto& result = outcome.value();
std::cout << "File downloaded to the local path"
<< ", status: " << result.getStatusCode()
<< ", contentLength: " << result.getContentLength() << std::endl;
return 0;
}
Download to a custom buffer (SinkFactory)
To download a file directly into pre-allocated memory, use SinkFactory with MemoryWriter. This approach eliminates one memory copy compared to reading the std::iostream response body. The following example calls HeadObject to obtain the object size, allocates a buffer, and writes to it directly:
#include <iostream>
#include <vector>
#include "alibabacloud/oss2/ClientConfiguration.h"
#include "alibabacloud/oss2/OSSClient.h"
#include "alibabacloud/oss2/credentials/CredentialsProvider.h"
#include "alibabacloud/oss2/io/ByteWriter.h"
namespace oss = alibabacloud::oss2;
int main() {
auto conf = oss::ClientConfiguration::loadDefault();
conf.region = "<region>";
conf.credentialsProvider = std::make_shared<oss::EnvironmentVariableCredentialsProvider>();
oss::OSSClient client(conf);
// 1. Call the HeadObject method to get the object size.
auto headOutcome = client.headObject(
oss::models::HeadObjectRequest()
.setBucket("examplebucket")
.setKey("exampledir/exampleobject.txt"));
if (!headOutcome.has_value()) {
auto& e = headOutcome.error();
std::cerr << "HeadObject failed"
<< ", code: " << e.getCode()
<< ", message: " << e.getMessage()
<< ", requestId: " << e.getRequestId() << std::endl;
return 1;
}
std::size_t contentLength = static_cast<std::size_t>(headOutcome.value().getContentLength());
std::cout << "Object size: " << contentLength << " bytes" << std::endl;
// 2. Pre-allocate the buffer.
std::vector<std::uint8_t> buffer(contentLength);
// 3. Provide a MemoryWriter through SinkFactory.
std::shared_ptr<oss::MemoryWriter> memWriter;
oss::SinkFactory factory;
factory.isOneShot = false;
factory.supplier = [&](std::int64_t, const oss::HeaderCollection&) -> std::shared_ptr<oss::ByteWriter> {
memWriter = std::make_shared<oss::MemoryWriter>(buffer.data(), buffer.size());
return memWriter;
};
auto outcome = client.getObject(
oss::models::GetObjectRequest()
.setBucket("examplebucket")
.setKey("exampledir/exampleobject.txt")
.setSinkFactory(factory));
if (!outcome.has_value()) {
auto& e = outcome.error();
std::cerr << "Failed to download the file"
<< ", code: " << e.getCode()
<< ", message: " << e.getMessage()
<< ", requestId: " << e.getRequestId() << std::endl;
return 1;
}
auto& result = outcome.value();
std::cout << "File downloaded successfully"
<< ", status: " << result.getStatusCode()
<< ", requestId: " << result.getRequestId()
<< ", bytes: " << memWriter->written() << std::endl;
return 0;
}
References
-
For the complete sample code for downloading a file to memory, see GetObject.cpp.
-
For the complete sample code for downloading a file to local disk, see GetObjectToFile.cpp.
-
For the complete sample code for downloading a file to a custom buffer, see DownloadToMemory.cpp.