Use the C++ SDK to download an OSS object directly into memory as a stream, without writing to disk.
Prerequisites
Before you begin, make sure you have:
An OSS bucket with at least one object
The required permissions (see Permissions)
The OSS C++ SDK installed and initialized
Permissions
By default, an Alibaba Cloud account has full permissions. RAM users and RAM roles have no permissions by default. Grant permissions through RAM Policy or bucket policies.
| API | Required action | When required |
|---|---|---|
| GetObject | oss:GetObject | Always — downloads an object |
| GetObject | oss:GetObjectVersion | Only when specifying an object version via versionId |
| GetObject | kms:Decrypt | Only when the object metadata contains X-Oss-Server-Side-Encryption: KMS |
Download an object to memory
The following example downloads a specified object and reads its content into a buffer.
#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;
int main(void)
{
// Set the endpoint for the region where your bucket is located.
// Example: https://oss-cn-hangzhou.aliyuncs.com for China (Hangzhou).
std::string Endpoint = "yourEndpoint";
// Set the region ID. Example: cn-hangzhou.
std::string Region = "yourRegion";
// Bucket name. Example: examplebucket.
std::string BucketName = "examplebucket";
// Full object path, excluding the bucket name. Example: desrfolder/exampleobject.txt.
std::string ObjectName = "yourObjectName";
// Initialize network resources.
InitializeSdk();
ClientConfiguration conf;
conf.signatureVersion = SignatureVersionType::V4;
// Load credentials from environment variables OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET.
// For local development, environment variables are a safe way to avoid hardcoding credentials.
// For production, use STS or a RAM role instead.
auto credentialsProvider = std::make_shared<EnvironmentVariableCredentialsProvider>();
OssClient client(Endpoint, credentialsProvider, conf);
client.SetRegion(Region);
// Submit the download request.
GetObjectRequest request(BucketName, ObjectName);
auto outcome = client.GetObject(request);
if (outcome.isSuccess()) {
std::cout << "Download succeeded. Content-Length: "
<< outcome.result().Metadata().ContentLength() << std::endl;
// Read the object content from the response stream into a 256-byte buffer.
// Typical use: append each chunk to a string or pass it to a parser.
auto& stream = outcome.result().Content();
char buffer[256];
while (stream->good()) {
stream->read(buffer, 256);
auto count = stream->gcount();
// Process buffer[0..count-1] here.
}
} else {
// Print error details. Include requestId when contacting support.
std::cout << "getObjectToBuffer fail"
<< " Code: " << outcome.error().Code()
<< " Message: " << outcome.error().Message()
<< " RequestId: " << outcome.error().RequestId() << std::endl;
return -1;
}
// Release network resources.
ShutdownSdk();
return 0;
}Replace the following placeholders with your actual values:
| Placeholder | Description | Example |
|---|---|---|
yourEndpoint | Endpoint for the region where the bucket is located | https://oss-cn-hangzhou.aliyuncs.com |
yourRegion | Region ID | cn-hangzhou |
examplebucket | Bucket name | examplebucket |
yourObjectName | Full object path, excluding the bucket name | desrfolder/exampleobject.txt |
Usage notes
The example uses the public endpoint for the China (Hangzhou) region. If you access OSS from another Alibaba Cloud service in the same region, use the internal endpoint instead. For endpoint details, see Regions and endpoints.
The example creates an
OssClientinstance using an OSS endpoint. To create anOssClientusing a custom domain or Security Token Service (STS), see Create an OSSClient instance.