本文介绍如何在上传、下载文件(Object)时,通过在请求中携带限速参数并设置限速值,以保证其他应用的正常带宽。
注意事项
简单上传和下载限速
以下代码用于简单上传和下载文件时设置单链接限速:
using System.Text;
using Aliyun.OSS;
// 填写Bucket所在地域对应的Endpoint。以华东1(杭州)为例,Endpoint填写为https://oss-cn-hangzhou.aliyuncs.com。
var endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
// 从环境变量中获取访问凭证。运行本代码示例之前,请确保已设置环境变量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。
var accessKeyId = Environment.GetEnvironmentVariable("OSS_ACCESS_KEY_ID");
var accessKeySecret = Environment.GetEnvironmentVariable("OSS_ACCESS_KEY_SECRET");
// 填写Bucket名称,例如examplebucket。
var bucketName = "examplebucket";
// 填写Object完整路径,完整路径中不能包含Bucket名称,例如exampledir/exampleobject.txt。
var objectName = "exampledir/exampleobject.txt";
var objectContent = "More than just cloud.";
// 创建OssClient实例。
var client = new OssClient(endpoint, accessKeyId, accessKeySecret);
try
{
byte[] binaryData = Encoding.ASCII.GetBytes(objectContent);
MemoryStream requestContent = new MemoryStream(binaryData);
// 上传文件限速100 KB/s,即819200 bit/s。
var putRequest = new PutObjectRequest(bucketName, objectName, requestContent)
{
TrafficLimit = 100*1024*8
};
client.PutObject(putRequest);
Console.WriteLine("Put object succeeded");
// 下载文件限速100KB/s,即819200bit/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;
// 填写Bucket所在地域对应的Endpoint。以华东1(杭州)为例,Endpoint填写为https://oss-cn-hangzhou.aliyuncs.com。
var endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
// 从环境变量中获取访问凭证。运行本代码示例之前,请确保已设置环境变量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。
var accessKeyId = Environment.GetEnvironmentVariable("OSS_ACCESS_KEY_ID");
var accessKeySecret = Environment.GetEnvironmentVariable("OSS_ACCESS_KEY_SECRET");
// 填写Bucket名称,例如examplebucket。
var bucketName = "examplebucket";
// 填写Object完整路径,完整路径中不能包含Bucket名称,例如exampledir/exampleobject.txt。
var objectName = "exampledir/exampleobject.txt";
var objectContent = "More than just cloud.";
var downloadFilename = "D:\\localpath\\examplefile.txt";
// 创建OssClient实例。
var client = new OssClient(endpoint, accessKeyId, accessKeySecret);
try
{
// 生成上传签名URL。
var generatePresignedUriRequest = new GeneratePresignedUriRequest(bucketName, objectName, SignHttpMethod.Put)
{
Expiration = DateTime.Now.AddHours(1),
};
// 限速100 KB/s,即819200 bit/s。
generatePresignedUriRequest.AddQueryParam("x-oss-traffic-limit", "819200");
var signedUrl = client.GeneratePresignedUri(generatePresignedUriRequest);
// 使用签名URL上传文件。
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;
// 生成下载签名URL。
// 限速100 KB/s,即 819200bit/s。
var req = new GeneratePresignedUriRequest(bucketName, objectName, SignHttpMethod.Get);
req.AddQueryParam("x-oss-traffic-limit", "819200");
var uri = client.GeneratePresignedUri(req);
// 使用签名URL下载文件。
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);
}
文档内容是否对您有帮助?