Cette rubrique explique comment ajouter des paramètres à une requête de téléchargement ou de téléversement d'objet afin de définir une limite de bande passante. Cette approche garantit que les autres applications disposent d'une bande passante suffisante.
Notes
Dans cette rubrique, le point de terminaison public de la région Chine (Hangzhou) est utilisé. Si vous souhaitez accéder à OSS depuis d'autres services Alibaba Cloud situés dans la même région, utilisez un point de terminaison interne. Pour plus d'informations sur les régions et les points de terminaison OSS, consultez Régions et points de terminaison.
Cette rubrique illustre la création d'une instance OssClient avec un point de terminaison OSS. Pour d'autres configurations, telles que l'utilisation d'un nom de domaine personnalisé ou l'authentification via des identifiants du Security Token Service (STS), reportez-vous à la section Créer une instance OssClient.
Configurer la limitation de la bande passante par connexion unique pour les téléversements et téléchargements simples
L'exemple de code suivant montre comment configurer la limitation de la bande passante par connexion unique pour les opérations de téléversement et de téléchargement 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;
}
Configurer la limitation de la bande passante pour les téléversements et téléchargements utilisant des URL signées
L'exemple de code suivant montre comment configurer la limitation de la bande passante par connexion unique lors de l'utilisation d'URL signées pour téléverser ou télécharger des objets :
#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;
}