All Products
Search
Document Center

Object Storage Service:Download a file (C++ SDK)

Last Updated:Mar 20, 2026

Use the GetObject operation to download an object from a bucket to local memory. By default, GetObject returns the current version of an object in a versioning-enabled bucket.

How it works

GetObject returns a GetObjectResult on success, or error details on failure. The result exposes:

  • Content — a stream you read into a string or buffer

  • Metadata — object properties, including ContentLength

  • VersionId — the version ID of the downloaded object

Before calling GetObject, set the endpoint, region, bucket name, and object path on the client and request. To download a specific version of an object, set the version ID on the request.

OutcomeDescription
Current version is a delete markerOSS returns 404 Not Found
Version ID specifiedOSS returns that specific version
Version ID set to nullOSS returns the version whose version ID is null
Version ID is a delete markerOSS returns 405 Method Not Allowed

Prerequisites

Before you begin, ensure that you have:

  • An OSS bucket with at least one object

  • The oss:GetObject permission on the target object

  • If downloading a versioned object: the oss:GetObjectVersion permission

  • If the object is encrypted with KMS (X-Oss-Server-Side-Encryption: KMS): the kms:Decrypt permission

By default, an Alibaba Cloud account has full permissions. RAM users and RAM roles have no permissions by default. Grant permissions using RAM policies or bucket policies.

Usage notes

  • This example uses the public endpoint for the China (Hangzhou) region. To access OSS from another Alibaba Cloud service in the same region, use an internal endpoint instead. For the full list of regions and endpoints, see Regions and endpoints.

  • This example creates an OSSClient instance using an OSS endpoint. To create one using a custom domain or Security Token Service (STS), see Create an OSSClient instance.

Download an object to memory

The following example downloads exampledir/exampleobject.txt from examplebucket and reads its content into a string. It also requests a specific version of the object and prints the downloaded version ID.

The call to client.GetObject(request) returns an outcome object. On success, read the content stream using *(outcome.result().Content()) >> content — this stream extraction operator reads the full response body into a std::string. On failure, retrieve the error code, message, and request ID from outcome.error().

#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;

int main(void)
{
    // Replace the placeholders below with your actual values.
    // Set the endpoint for the region where your bucket is located.
    // Example: https://oss-cn-hangzhou.aliyuncs.com
    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: exampledir/exampleobject.txt
    std::string ObjectName = "exampledir/exampleobject.txt";

    // Initialize network resources.
    InitializeSdk();

    ClientConfiguration conf;
    conf.signatureVersion = SignatureVersionType::V4;

    // Load credentials from environment variables OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET.
    auto credentialsProvider = std::make_shared<EnvironmentVariableCredentialsProvider>();
    OssClient client(Endpoint, credentialsProvider, conf);
    client.SetRegion(Region);

    // Build the download request.
    // To download a specific version, call request.setVersionId() with the target version ID.
    GetObjectRequest request(BucketName, ObjectName);
    request.setVersionId("yourObjectVersionId");

    // Execute the download. The object is returned as a stream in memory.
    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 stream.
        std::string content;
        *(outcome.result().Content()) >> content;
        std::cout << "Content: " << content << std::endl;

        // Print the version ID of the downloaded object.
        std::cout << "Version ID: " << outcome.result().VersionId() << std::endl;
    }
    else {
        // Print the error code, message, and request ID for debugging.
        std::cout << "Download failed."
                  << " 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 before running the code:

PlaceholderDescriptionExample
yourEndpointThe endpoint for the region where your bucket is locatedhttps://oss-cn-hangzhou.aliyuncs.com
yourRegionThe region IDcn-hangzhou
examplebucketYour bucket nameexamplebucket
exampledir/exampleobject.txtThe full path of the object, excluding the bucket nameexampledir/exampleobject.txt
yourObjectVersionIdThe version ID of the object to download. Remove this line to download the current version—

References