public void PutFileProgress(string bucketName, string filePath, string objcetKey, Action<int, string, string> progressCallback, Action<string, string> errorCallBack)
{
this.bucketName = bucketName;
PutFileProgressEx(filePath, objcetKey, progressCallback, errorCallBack);
}
public bool PutFileProgressEx(string filePath, string objectKey, Action<int, string, string> progressCallback, Action<string, string> errorCallBack)
{
try
{
string folderPath = filePath.Substring(0, filePath.LastIndexOf("\\"));
// 通过DownloadObjectRequest设置多个参数。
UploadObjectRequest request = new UploadObjectRequest(bucketName, objectKey, filePath)
{
// 指定上传的分片大小。
PartSize = 8 * 1024 * 1024,
// 指定并发线程数。
ParallelThreadCount = 3,
CheckpointDir = folderPath,
// RequestPayer = RequestPayer.Requester
};
request.StreamTransferProgress = (sender, e) =>
{
Task.Run(() => {
progressCallback.Invoke(e.PercentDone, taskId, filePath);
});
};
// 断点续传下载。
var data = client.ResumableUploadObject(request);
return true;
}
catch (OssException ex)
{
log.Error(ex.StackTrace);
lastError = ex;
errorCallBack?.Invoke(taskId, ex.Message);
return false;
}
catch (Exception ex)
{
log.Error(ex.StackTrace);
errorCallBack?.Invoke(taskId, ex.Message);
return false;
}
}
调用部分:
string zipName = “E:\\upload\\1.zip”;
Action<int, string, string> action = (progress, tId, fileName) =>
{
#region action
#endregion
});
PutFileProgress(ossConfig.BucketName, zipName, ossConfig.OssObjectKey, action, (taskid, errormsg) =>
{
#region error
#endregion
});
想用ResumableUploadObject实现断点续传一直上传失败,这是为什么,用法有什么不对吗?请指教
Aliyun.OSS版本是Aliyun.OSS.SDK.2.13.0\lib\net45\Aliyun.OSS.dll
我可以为您提供以下指导和示例代码:
首先,让我们检查一些可能导致上传失败的原因:
在这些可能的问题中,第二个问题是最常见的,因此检查您的证书是否正确配置通常是第一步。
为了正常使用ResumableUploadObject方法,您需要首先安装Aliyun.OSS.SDK和Aliyun.OSS.Tools Nuget包。然后,请按照以下步骤使用该方法:
var endpoint = "http://your-oss-region.aliyuncs.com";
var accessKeyId = "your-access-key-id";
var accessKeySecret = "your-access-key-secret";
var client = new OssClient(endpoint, accessKeyId, accessKeySecret);
var partSize = 5 * 1024 * 1024;
var configuration = new ResumableUploaderConfiguration(partSize);
var bucketName = "your-bucket-name";
var objectName = "your-object-name";
var filePath = "C:\path\to\your\file";
using (var fs = new FileStream(filePath, FileMode.Open))
{
var resumableUploader = new ResumableUploader(client, bucketName, objectName, fs, configuration);
// 如果需要,您可以在这里注册进度事件
resumableUploader.Upload();
}
在上面的代码中,当您调用resumableUploader.Upload()方法时,将上传文件并触发一些进度事件(如果您注册了这些事件)。
以上是一个基本的示例代码,更详细的使用方式可以查看官方文档:
https://help.aliyun.com/document_detail/85633.html?spm=a2c4g.11186623.6.610.28855f54AvvJ11