using COSXML.Model.Object;
using COSXML.Auth;
using COSXML.Transfer;
using System;
using COSXML;
using System.Threading.Tasks;
namespace COSSnippet
{
public class TransferUploadObjectModel {
private CosXml cosXml;
TransferUploadObjectModel() {
CosXmlConfig config = new CosXmlConfig.Builder()
.SetRegion("COS_REGION") // 设置默认的地域, COS 地域的简称请参照 https://cloud.tencent.com/document/product/436/6224
.Build();
string secretId = "SECRET_ID"; // 云 API 密钥 SecretId, 获取 API 密钥请参照 https://console.cloud.tencent.com/cam/capi
string secretKey = "SECRET_KEY"; // 云 API 密钥 SecretKey, 获取 API 密钥请参照 https://console.cloud.tencent.com/cam/capi
long durationSecond = 600; //每次请求签名有效时长,单位为秒
QCloudCredentialProvider qCloudCredentialProvider = new DefaultQCloudCredentialProvider(secretId,
secretKey, durationSecond);
this.cosXml = new CosXmlServer(config, qCloudCredentialProvider);
}
/// 高级接口上传文件
public async Task TransferUploadFile()
{
// 初始化 TransferConfig
TransferConfig transferConfig = new TransferConfig();
// 手动设置开始分块上传的大小阈值为10MB,默认值为5MB
transferConfig.DivisionForUpload = 10 * 1024 * 1024;
// 手动设置分块上传中每个分块的大小为2MB,默认值为1MB
transferConfig.SliceSizeForUpload = 2 * 1024 * 1024;
// 初始化 TransferManager
TransferManager transferManager = new TransferManager(cosXml, transferConfig);
// 存储桶名称,此处填入格式必须为 bucketname-APPID, 其中 APPID 获取参考 https://console.cloud.tencent.com/developer
String bucket = "examplebucket-1250000000";
String cosPath = "exampleobject"; //对象在存储桶中的位置标识符,即称对象键
String srcPath = @"temp-source-file";//本地文件绝对路径
// 上传对象
COSXMLUploadTask uploadTask = new COSXMLUploadTask(bucket, cosPath);
uploadTask.SetSrcPath(srcPath);
uploadTask.progressCallback = delegate (long completed, long total)
{
Console.WriteLine(String.Format("progress = {0:##.##}%", completed * 100.0 / total));
};
try {
COSXML.Transfer.COSXMLUploadTask.UploadTaskResult result = await
transferManager.UploadAsync(uploadTask);
Console.WriteLine(result.GetResultInfo());
string eTag = result.eTag;
} catch (Exception e) {
Console.WriteLine("CosException: " + e);
}
}
static void Main(string[] args)
{
TransferUploadObjectModel m = new TransferUploadObjectModel();
/// 高级接口上传对象
m.TransferUploadFile().Wait();
// .cssg-methods-pragma
}
}
}