Tous les produits
Search
Centre de documentation

Object Storage Service:Télécharger un objet en mémoire (SDK C++)

Dernière mise à jour :Sep 25, 2026

Cette rubrique explique comment télécharger un objet Object Storage Service (OSS) en mémoire.

Notes

  • Cette rubrique utilise l'endpoint public de la région Chine (Hangzhou). Si vous souhaitez accéder à OSS depuis d'autres services Alibaba Cloud situés dans la même région, utilisez un endpoint interne. Pour plus d'informations sur les régions et les endpoints OSS, consultez Régions et endpoints.

  • Cette rubrique montre comment créer une instance OssClient avec un endpoint OSS. Pour d'autres configurations, comme l'utilisation d'un nom de domaine personnalisé ou l'authentification via des identifiants du Security Token Service (STS), consultez Créer une instance OssClient.

Permissions

Par défaut, un compte Alibaba Cloud dispose de toutes les autorisations. Les utilisateurs RAM ou les rôles RAM associés à un compte Alibaba Cloud ne disposent d'aucune autorisation par défaut. Le compte Alibaba Cloud ou l'administrateur du compte doit accorder les autorisations d'exécution nécessaires via des politiques RAM ou une Bucket Policy.

API

Action

Description

GetObject

oss:GetObject

Télécharge un objet.

oss:GetObjectVersion

Cette autorisation est requise pour télécharger un objet dont vous spécifiez la version via versionId.

kms:Decrypt

Cette autorisation est requise pour télécharger un objet dont les métadonnées contiennent X-Oss-Server-Side-Encryption: KMS.

Exemple de code

Le code suivant montre comment télécharger un objet OSS spécifique en mémoire :

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

int main(void)
{
    /* Initialize the OSS account information. */
            
    /* Set yourEndpoint to the Endpoint of the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the Endpoint to https://oss-cn-hangzhou.aliyuncs.com. */
    std::string Endpoint = "yourEndpoint";
    /* Set yourRegion to the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the region to cn-hangzhou. */
    std::string Region = "yourRegion";
    /* Specify the bucket name. Example: examplebucket. */
    std::string BucketName = "examplebucket";
    /* Specify the full path of the object. The path cannot contain the bucket name. Example: desrfolder/exampleobject.txt. */
    std::string ObjectName = "yourObjectName";

    /* Initialize network resources. */
    InitializeSdk();

    ClientConfiguration conf;
    conf.signatureVersion = SignatureVersionType::V4;
    /* Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. */
    auto credentialsProvider = std::make_shared<EnvironmentVariableCredentialsProvider>();
    OssClient client(Endpoint, credentialsProvider, conf);
    client.SetRegion(Region);

    /* Download the object to memory. */
    GetObjectRequest request(BucketName, ObjectName);
    auto outcome = client.GetObject(request);
    if (outcome.isSuccess()) {
      std::cout << "getObjectToBuffer" << " success, Content-Length:" << outcome.result().Metadata().ContentLength() << std::endl;
        /* Read data using the read interface. */
        auto& stream = outcome.result().Content();
        char buffer[256];
        while (stream->good()) {
            stream->read(buffer, 256);
            auto count = stream->gcount();
            /* Process the data as needed. */
        }
    }
    else {
        /* Handle the exception. */
        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;
}