Todos os produtos
Search
Central de documentação

Object Storage Service:Single-connection bandwidth throttling (C++ SDK)

Última atualização: Sep 22, 2026

Este tópico descreve como adicionar parâmetros a uma solicitação de upload ou download de objeto para definir o limite de largura de banda. Isso garante largura de banda suficiente para outras aplicações.

Observações

  • Este tópico usa o endpoint público da região China (Hangzhou). Para acessar o OSS a partir de outros serviços do Alibaba Cloud na mesma região do OSS, use um endpoint interno. Para mais informações sobre as regiões e os endpoints do OSS, consulte Regions and endpoints.

  • Este tópico demonstra como criar uma instância OSSClient com um endpoint do OSS. Para configurações alternativas, como usar um domínio personalizado ou autenticar com credenciais do Security Token Service (STS), consulte Create an OssClient instance.

Configure a limitação de largura de banda de conexão única para upload e download simples

O exemplo de código a seguir mostra como configurar a limitação de largura de banda de conexão única para upload e download simples:

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

int main(void)
{
    /* Initialize OSS account information. */
    
    /* Set yourEndpoint to the endpoint of the region where the bucket is located. For example, for 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, for the China (Hangzhou) region, set the region to cn-hangzhou. */
    std::string Region = "yourRegion";
    /* Specify the bucket name, for example, examplebucket. */
    std::string BucketName = "examplebucket";
    /* Specify the full path of the object. The full path cannot contain the bucket name. For example, exampledir/exampleobject.txt. */
    std::string ObjectName = "exampledir/exampleobject.txt";

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

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

    /* Upload the file. */
    std::shared_ptr<std::iostream> content = std::make_shared<std::fstream>("yourLocalFilename", std::ios::in|std::ios::binary);
    PutObjectRequest putrequest(BucketName, ObjectName,content);
    /* Set the upload bandwidth limit to 100 KB/s. */
    putrequest.setTrafficLimit(819200);
    auto putoutcome = client.PutObject(putrequest);

    /* Download the file to local memory. */
    GetObjectRequest getrequest(BucketName, ObjectName);
    /* Set the download bandwidth limit to 100 KB/s. */
    getrequest.setTrafficLimit(819200);
    auto getoutcome = client.GetObject(getrequest);

    /* Release network resources. */
    ShutdownSdk();
    return 0;
}

Configure a limitação de largura de banda para upload e download com URLs assinadas

O exemplo de código a seguir mostra como configurar a limitação de largura de banda de conexão única ao usar URLs assinadas para fazer upload ou download de objetos:

#include <alibabacloud/oss/OssClient.h>
#include <fstream>
using namespace AlibabaCloud::OSS;
int main(void)
{
    /* Initialize OSS account information. */
    
    /* Set yourEndpoint to the endpoint of the region where the bucket is located. For example, for 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, for the China (Hangzhou) region, set the region to cn-hangzhou. */
    std::string Region = "yourRegion";
    /* Specify the bucket name, for example, examplebucket. */
    std::string BucketName = "examplebucket";
    /* Specify the full path of the object. The full path cannot contain the bucket name. For example, exampledir/exampleobject.txt. */
    std::string ObjectName = "exampledir/exampleobject.txt"; 

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

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

    /* Set the validity period of the signature. The maximum validity period is 32,400 seconds. */
    std::time_t expires = std::time(nullptr) + 1200; 
    /* Generate a URL for uploading the file. */
    GeneratePresignedUrlRequest putrequest(BucketName, ObjectName, Http::Put);
    putrequest.setExpires(expires);
    /* Set the upload bandwidth limit to 100 KB/s. */
    putrequest.setTrafficLimit(819200);
    auto genOutcome = client.GeneratePresignedUrl(putrequest);
    std::shared_ptr<std::iostream> content = std::make_shared<std::stringstream>();
    *content << "test cpp sdk";
    /* Print the result of the signed URL for the upload. */
    std::cout << "Signed URL for upload: " << genOutcome.result() << std::endl;

    /* Use the signed URL to upload the file. */
    auto outcome = client.PutObjectByUrl(genOutcome.result(), content);
    /* Generate a URL for downloading the file. */
    GeneratePresignedUrlRequest getrequest(BucketName, ObjectName, Http::Get);
    getrequest.setExpires(expires);
    /* Set the download bandwidth limit to 100 KB/s. */
    getrequest.setTrafficLimit(819200);
    genOutcome = client.GeneratePresignedUrl(getrequest);
    /* Print the result of the signed URL for the download. */
    std::cout << "Signed URL for download: " << genOutcome.result() << std::endl;
    /* Use the signed URL to download the file. */
    auto goutcome = client.GetObjectByUrl(genOutcome.result());
    
    /* Release network resources. */
    ShutdownSdk();
    return 0;
}