This topic describes how to add parameters in an object upload or download request to set the limit of upload or download bandwidth. This ensures sufficient bandwidth for other applications.
Usage notes
In this topic, the public endpoint of the China (Hangzhou) region is used. If you want to access OSS from other Alibaba Cloud services in the same region as OSS, use an internal endpoint. For more information about OSS regions and endpoints, see Regions and endpoints.
In this topic, an OSSClient instance is created by using an OSS endpoint. If you want to create an OSSClient by using custom domain names or Security Token Service (STS), see Create an OSSClient instance.
Configure single-connection bandwidth throttling for simple upload and download
The following sample code provides an example on how to configure single-connection bandwidth throttling for simple upload and download:
#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 bandwidth throttling for upload and download that use signed URLs
The following sample code provides an example on how to configure single-connection bandwidth throttling when you use signed URLs to upload or download objects:
#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;
}