/// <summary> /// [异步async]上传数据 /// </summary> /// <param name="data">待上传的数据</param> /// <param name="saveKey">要保存的文件名称</param> /// <param name="token">上传凭证</param> /// <returns>上传文件后的返回结果</returns> public async Task <HttpResult> UploadDataAsync(byte[] data, string saveKey, string token) { HttpResult result = new HttpResult(); if (data.Length > PUT_THRESHOLD) { ResumableUploader ru = new ResumableUploader(UPLOAD_FROM_CDN); result = await ru.UploadDataAsync(data, saveKey, token, upph); } else { FormUploader fu = new FormUploader(UPLOAD_FROM_CDN); result = await fu.UploadDataAsync(data, saveKey, token); } return(result); }
protected override async Task UploadFileAsync(FileInfo fileInfo, int?expireIn = null, CancellationToken cancellationToken = default) { Mac mac = new Mac(Options.AccessKey, Options.SecretKey); PutPolicy putPolicy = new PutPolicy { Scope = fileInfo.Directory + ":" + fileInfo.Name, CallbackBody = Options.UploadCallbackBody, CallbackBodyType = Options.UploadCallbackBodyType, CallbackHost = Options.UploadCallbackHost, CallbackUrl = Options.UploadCallbackUrl }; if (expireIn.HasValue) { putPolicy.SetExpires(expireIn.Value); } if (Options.DeleteAfterDays > 0) { putPolicy.DeleteAfterDays = Options.DeleteAfterDays; } string jstr = putPolicy.ToJsonString(); string token = Auth.CreateUploadToken(mac, jstr); UploadProgressHandler handler = (uploadByte, totalByte) => { OnFileUploadProgressChanged(uploadByte, totalByte); }; // 带进度的上传 ResumableUploader uploader = new ResumableUploader(); var httpResult = await uploader.UploadDataAsync(fileInfo.Data, fileInfo.Name, token, handler); // 普通上传 //FormUploader fu = new FormUploader(); //var httpResult = await fu.UploadDataAsync(fileInfo.Data, fileInfo.Name, token); // TODO: 处理响应代码 Console.WriteLine(httpResult.Code); }
public async Task UploadDataTest() { Mac mac = new Mac(AccessKey, SecretKey); StorageFile localFile = await StorageFile.GetFileFromPathAsync(LocalFile2); byte[] data = await ResumableUploader.ReadToByteArrayAsync(localFile); string key = FileKey2; PutPolicy putPolicy = new PutPolicy(); putPolicy.Scope = Bucket1 + ":" + key; putPolicy.SetExpires(3600); putPolicy.DeleteAfterDays = 1; string token = Auth.CreateUploadToken(mac, putPolicy.ToJsonString()); ResumableUploader target = new ResumableUploader(); HttpResult result = await target.UploadDataAsync(data, key, token, null); Assert.AreEqual((int)HttpCode.OK, result.Code); }