This topic describes how to ensure other applications have sufficient bandwidth when you perform upload and download operations on OSS. You can include the x-oss-traffic-limit parameter in your requests and set bandwidth limits.

Note For more information about the scenarios and precautions of the single-connection bandwidth throttling feature, see Single-connection bandwidth throttling in the OSS Developer Guide.

Configure bandwidth throttling for common uploads and downloads

Use the following code to configure bandwidth throttling when you upload and download objects to and from OSS:

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

int main(void)
{
    /* Initialize the OSS account information. */
    std::string AccessKeyId = "yourAccessKeyId";
    std::string AccessKeySecret = "yourAccessKeySecret";
    std::string Endpoint = "yourEndpoint";
    std::string BucketName = "yourBucketName";
    std::string ObjectName = "yourObjectName";

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

    ClientConfiguration conf;
    OssClient client(Endpoint, AccessKeyId, AccessKeySecret, conf);

    /* Upload the object. */
    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. */
    getrequest.setTrafficLimit(819200);
    auto putoutcome = client.PutObject(putrequest);

    /* Download the object 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 bandwidth throttling for uploads and downloads that use signed URLs

Use the following code to configure bandwidth throttling when you upload and download objects from and to OSS by using signed URLs.

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

int main(void)
{
    /* Initialize the OSS account information. */
    std::string AccessKeyId = "yourAccessKeyId";
    std::string AccessKeySecret = "yourAccessKeySecret";
    std::string Endpoint = "yourEndpoint";
    std::string BucketName = "yourBucketName";
    std::string PutobjectUrlName = "yourPutobjectUrlName";

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

    ClientConfiguration conf;
    OssClient client(Endpoint, AccessKeyId, AccessKeySecret, conf);

    /* Specify the valid period for the URL.*/
    std::time_t t = std::time(nullptr) + 1200;
    /* Generate the signed URL for uploading an object. */
    GeneratePresignedUrlRequest putrequest(BucketName, PutobjectUrlName, 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";

    /* Use the signed URL to upload the object. */
    auto outcome = client.PutObjectByUrl(genOutcome.result(), content);

    /* Generate the signed URL for downloading an object. */
    GeneratePresignedUrlRequest getrequest(BucketName, PutobjectUrlName, Http::Get);
    getrequest.setExpires(expires);
    /* Set the download bandwidth limit to 100 KB/s. */
    getrequest.setTrafficLimit(819200);
    genOutcome = client.GeneratePresignedUrl(getrequest);

    /* Use the signed URL to download the object. */
    outcome = client.GetObjectByUrl(genOutcome.result());

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