Este tópico descreve como adicionar parâmetros em uma solicitação de upload ou download de objetos para definir o limite de largura de banda de upload ou download. Isso garante largura de banda suficiente para outras aplicações.
Observações
Neste tópico, é usado o endpoint público da região China (Hangzhou). Para acessar o OSS a partir de outros serviços Alibaba Cloud na mesma região do OSS, use um endpoint interno. Para mais informações sobre regiões e endpoints do OSS, consulte Regions and endpoints.
Este tópico demonstra a criação de uma instância OSSClient com um endpoint do OSS. Para configurações alternativas, como uso de domínio personalizado ou autenticação com credenciais do Security Token Service (STS), consulte Initialization (C# SDK V1).
Configurar limitação de largura de banda por conexão única para upload e download simples
O código de exemplo a seguir mostra como configurar a limitação de largura de banda por conexão única para upload e download simples:
using System.Text;
using Aliyun.OSS;
using Aliyun.OSS.Common;
// Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com.
var endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
// Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.
var accessKeyId = Environment.GetEnvironmentVariable("OSS_ACCESS_KEY_ID");
var accessKeySecret = Environment.GetEnvironmentVariable("OSS_ACCESS_KEY_SECRET");
// Specify the name of the bucket. Example: examplebucket.
var bucketName = "examplebucket";
// Specify the full path of the object. Do not include the bucket name in the full path. Example: exampledir/exampleobject.txt.
var objectName = "exampledir/exampleobject.txt";
var objectContent = "More than just cloud.";
// Specify the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to cn-hangzhou.
const string region = "cn-hangzhou";
// Create a ClientConfiguration instance and modify parameters as required.
var conf = new ClientConfiguration();
// Use the signature algorithm V4.
conf.SignatureVersion = SignatureVersion.V4;
// Create an OSSClient instance.
var client = new OssClient(endpoint, accessKeyId, accessKeySecret, conf);
client.SetRegion(region);
try
{
byte[] binaryData = Encoding.ASCII.GetBytes(objectContent);
MemoryStream requestContent = new MemoryStream(binaryData);
// Set the bandwidth limit for object upload to 100 KB/s, which is equal to 819,200 bit/s.
var putRequest = new PutObjectRequest(bucketName, objectName, requestContent)
{
TrafficLimit = 100*1024*8
};
client.PutObject(putRequest);
Console.WriteLine("Put object succeeded");
// Set the bandwidth limit for object download to 100 KB/s, which is equal to 819,200 bit/s.
var getRequest = new GetObjectRequest(bucketName, objectName)
{
TrafficLimit = 100 * 1024 * 8
};
var getResult = client.GetObject(getRequest);
Console.WriteLine("Get object succeeded");
}
catch (Exception ex)
{
Console.WriteLine("Put object failed, {0}", ex.Message);
}
Configurar limitação de largura de banda para upload e download com URLs assinadas
O código de exemplo a seguir mostra como configurar a limitação de largura de banda por conexão única ao usar URLs assinadas para fazer upload ou download de objetos:
using System.Text;
using Aliyun.OSS;
using Aliyun.OSS.Common;
// Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com.
var endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
// Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.
var accessKeyId = Environment.GetEnvironmentVariable("OSS_ACCESS_KEY_ID");
var accessKeySecret = Environment.GetEnvironmentVariable("OSS_ACCESS_KEY_SECRET");
// Specify the name of the bucket. Example: examplebucket.
var bucketName = "examplebucket";
// Specify the full path of the object. Do not include the bucket name in the full path. Example: exampledir/exampleobject.txt.
var objectName = "exampledir/exampleobject.txt";
var objectContent = "More than just cloud.";
var downloadFilename = "D:\\localpath\\examplefile.txt";
// Specify the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to cn-hangzhou.
const string region = "cn-hangzhou";
// Create a ClientConfiguration instance and modify parameters as required.
var conf = new ClientConfiguration();
// Use the signature algorithm V4.
conf.SignatureVersion = SignatureVersion.V4;
// Create an OSSClient instance.
var client = new OssClient(endpoint, accessKeyId, accessKeySecret, conf);
client.SetRegion(region);
try
{
// Generate a signed URL to upload the object.
var generatePresignedUriRequest = new GeneratePresignedUriRequest(bucketName, objectName, SignHttpMethod.Put)
{
Expiration = DateTime.Now.AddHours(1),
};
// Set the bandwidth limit to 100 KB/s, which is equal to 819,200 bit/s.
generatePresignedUriRequest.AddQueryParam("x-oss-traffic-limit", "819200");
var signedUrl = client.GeneratePresignedUri(generatePresignedUriRequest);
// Use the signed URLs to upload the object.
var buffer = Encoding.UTF8.GetBytes(objectContent);
using (var ms = new MemoryStream(buffer))
{
client.PutObject(signedUrl, ms);
}
Console.WriteLine("Put object by signatrue succeeded. {0} ", signedUrl.ToString());
var metadata = client.GetObjectMetadata(bucketName, objectName);
var etag = metadata.ETag;
// Generate a signed URL to download the object.
// Set the bandwidth limit to 100 KB/s, which is equal to 819,200 bit/s.
var req = new GeneratePresignedUriRequest(bucketName, objectName, SignHttpMethod.Get);
req.AddQueryParam("x-oss-traffic-limit", "819200");
var uri = client.GeneratePresignedUri(req);
// Use the signed URL to download the object.
OssObject ossObject = client.GetObject(uri);
using (var file = File.Open(downloadFilename, FileMode.OpenOrCreate))
{
using (Stream stream = ossObject.Content)
{
int length = 4 * 1024;
var buf = new byte[length];
do
{
length = stream.Read(buf, 0, length);
file.Write(buf, 0, length);
} while (length != 0);
}
}
Console.WriteLine("Get object by signatrue succeeded. {0} ", uri.ToString());
}
catch (Exception ex)
{
Console.WriteLine("Put object failed, {0}", ex.Message);
}