All Products
Search
Document Center

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

Last Updated:Mar 20, 2026

Set a per-connection bandwidth limit on upload and download requests to reserve network capacity for other applications. The limit applies at the request level using setTrafficLimit().

Supported operations: PutObject (simple upload), GetObject (simple download), and presigned URL uploads and downloads.

Usage notes

  • The examples in this topic use the public endpoint for the China (Hangzhou) region. To access OSS from other Alibaba Cloud services in the same region, use an internal endpoint. For more information, see Regions and endpoints.

  • The examples create an OSSClient instance using an OSS endpoint. To create an OSSClient using custom domain names or Security Token Service (STS), see Create an OSSClient instance.

  • All examples read access credentials from environment variables. Before running the code, set the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables.

Throttle simple upload and download

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

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

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

    ClientConfiguration conf;
    conf.signatureVersion = SignatureVersionType::V4;
    /* Read credentials from environment variables. */
    auto credentialsProvider = std::make_shared<EnvironmentVariableCredentialsProvider>();
    OssClient client(Endpoint, credentialsProvider, conf);
    client.SetRegion(Region);

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

    /* Download the file with a bandwidth limit of 819200 bit/s (100 KB/s). */
    GetObjectRequest getrequest(BucketName, ObjectName);
    getrequest.setTrafficLimit(819200);
    auto getoutcome = client.GetObject(getrequest);

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

Throttle upload and download using signed URLs

Use GeneratePresignedUrlRequest with setTrafficLimit() to embed the bandwidth limit in the signed URL. The limit is enforced when the URL is used to upload or download an object.

The maximum validity period for a signed URL is 32,400 seconds.

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

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

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

    ClientConfiguration conf;
    conf.signatureVersion = SignatureVersionType::V4;
    /* Read credentials from environment variables. */
    auto credentialsProvider = std::make_shared<EnvironmentVariableCredentialsProvider>();
    OssClient client(Endpoint, credentialsProvider, conf);
    client.SetRegion(Region);

    /* Set the URL validity period to 1200 seconds. */
    std::time_t expires = std::time(nullptr) + 1200;

    /* Generate a signed URL for upload with a bandwidth limit of 819200 bit/s (100 KB/s). */
    GeneratePresignedUrlRequest putrequest(BucketName, ObjectName, Http::Put);
    putrequest.setExpires(expires);
    putrequest.setTrafficLimit(819200);
    auto genOutcome = client.GeneratePresignedUrl(putrequest);
    std::cout << "Signed URL for upload: " << genOutcome.result() << std::endl;

    std::shared_ptr<std::iostream> content = std::make_shared<std::stringstream>();
    *content << "test cpp sdk";
    auto outcome = client.PutObjectByUrl(genOutcome.result(), content);

    /* Generate a signed URL for download with a bandwidth limit of 819200 bit/s (100 KB/s). */
    GeneratePresignedUrlRequest getrequest(BucketName, ObjectName, Http::Get);
    getrequest.setExpires(expires);
    getrequest.setTrafficLimit(819200);
    genOutcome = client.GeneratePresignedUrl(getrequest);
    std::cout << "Signed URL for download: " << genOutcome.result() << std::endl;

    auto goutcome = client.GetObjectByUrl(genOutcome.result());

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