このトピックでは、オブジェクトのアップロードまたはダウンロード要求にパラメーターを追加して、アップロードまたはダウンロード帯域幅の制限を設定する方法について説明します。 これにより、他のアプリケーションに十分な帯域幅が確保されます。
使用上の注意
このトピックでは、中国 (杭州) リージョンのパブリックエンドポイントを使用します。 OSSと同じリージョンにある他のAlibaba CloudサービスからOSSにアクセスする場合は、内部エンドポイントを使用します。 OSSリージョンとエンドポイントの詳細については、「OSSリージョンとエンドポイント」をご参照ください。
このトピックでは、OSSエンドポイントを使用してOSSClientインスタンスを作成します。 カスタムドメイン名またはSTS (Security Token Service) を使用してOSSClientインスタンスを作成する場合は、「初期化」をご参照ください。
単純なアップロードとダウンロードのための単一接続帯域幅調整の設定
次のサンプルコードは、単純なアップロードとダウンロードのために単一接続帯域幅スロットリングを構成する方法の例を示しています。
using System.Text;
using Aliyun.OSS;
// 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);
c.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);
}
署名付きURLを使用するアップロードとダウンロードの帯域幅調整を構成する
次のサンプルコードでは、署名付きURLを使用してオブジェクトをアップロードまたはダウンロードする場合に、単一接続帯域幅スロットリングを構成する方法の例を示します。
using System.Text;
using Aliyun.OSS;
// 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);
c.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);
}