/// <summary> /// 根据文件字节数组上传 /// </summary> /// <param name="param">文件流上传配置</param> /// <param name="isResume">是否允许续传(续传采用非表单提交方式)</param> /// <returns></returns> public UploadResultDto UploadByteArray(UploadByByteArrayParam param, bool isResume = false) { var uploadPersistentOps = GetUploadPersistentOps(param.UploadPersistentOps); string token = GetUploadCredentials(QiNiuConfig, new UploadPersistentOpsParam(param.Key, uploadPersistentOps)); if (isResume) { ResumableUploader target = new ResumableUploader(Core.Tools.GetConfig(QiNiuConfig, uploadPersistentOps)); HttpResult result = target.UploadStream(param.ByteArray.ConvertToStream(), param.Key, token, GetPutExtra(uploadPersistentOps)); bool res = result.Code == (int)HttpCode.OK; return(new UploadResultDto(res, result, res ? "成功" : result.ToString())); } else { FormUploader target = new FormUploader(Core.Tools.GetConfig(QiNiuConfig, uploadPersistentOps)); HttpResult result = target.UploadData(param.ByteArray, param.Key, token, GetPutExtra(uploadPersistentOps)); bool res = result.Code == (int)HttpCode.OK; return(new UploadResultDto(res, result, res ? "成功" : result.ToString())); } }
public void ResumeUploadFileTest() { Mac mac = new Mac(AccessKey, SecretKey); Random rand = new Random(); string key = string.Format("UploadFileTest_{0}.dat", rand.Next()); string filePath = LocalFile; System.IO.Stream fs = System.IO.File.OpenRead(filePath); PutPolicy putPolicy = new PutPolicy(); putPolicy.Scope = Bucket + ":" + key; putPolicy.SetExpires(3600); putPolicy.DeleteAfterDays = 1; string token = Auth.CreateUploadToken(mac, putPolicy.ToJsonString()); Config config = new Config(); config.UseHttps = true; config.Zone = Zone.ZONE_CN_East; config.UseCdnDomains = true; config.ChunkSize = ChunkUnit.U512K; ResumableUploader target = new ResumableUploader(config); PutExtra extra = new PutExtra(); //设置断点续传进度记录文件 extra.ResumeRecordFile = ResumeHelper.GetDefaultRecordKey(filePath, key); Console.WriteLine("record file:" + extra.ResumeRecordFile); extra.ResumeRecordFile = "test.progress"; HttpResult result = target.UploadStream(fs, key, token, extra); Console.WriteLine("resume upload: " + result.ToString()); Assert.AreEqual((int)HttpCode.OK, result.Code); }
/// <summary> /// 上传到七牛 /// </summary> /// <param name="myStream"></param> /// <param name="filename"></param> /// <param name="servePath">上传成功的路径</param> /// <returns></returns> public static HttpResult UploadFile(Stream myStream, string filename, out string servePath) { //code为200成功 HttpResult result = new HttpResult() { Code = 1 }; servePath = ""; try { // 上传文件名 servePath = $"{Guid.NewGuid().ToString().Replace("-", "")}{filename}"; Qiniu.Storage.Config config = new Qiniu.Storage.Config(); // 设置上传区域 config.Zone = Zone.ZONE_CN_South; // 设置 http 或者 https 上传 config.UseHttps = true; config.UseCdnDomains = true; config.ChunkSize = ChunkUnit.U512K; ResumableUploader target = new ResumableUploader(config); PutExtra extra = new PutExtra(); string token = GetToken(); result = target.UploadStream(myStream, servePath, token, extra); } catch (Exception ex) { LogHelper.WriteLog(ex, "QiniutokenHelper/UploadFile"); } return(result); }
/// <summary> /// 文件流上传 /// </summary> /// <param name="key"></param> /// <param name="stream"></param> /// <returns></returns> public string StreamUpload(string key) { ResumableUploader resumableUploader = new ResumableUploader(ConfigUtil); HttpResult httpResult = resumableUploader.UploadStream(QiniuStream, key, TokenUtil, null); QiniuStream.Close(); return(httpResult.Text); }
private bool UploadFileOverlay(string file, bool overLay = false) { // string filePath = LocalFile; if (uploadResult == null) { uploadResult = new StringBuilder(); } if (putPolicy != null) { string key = Path.GetFileName(file); System.IO.Stream fs = System.IO.File.OpenRead(file); if (overLay) { putPolicy.Scope = bucket + ":" + key; } else { putPolicy.Scope = bucket; } putPolicy.SetExpires(3600); putPolicy.DeleteAfterDays = qiNiuClientCfg.DeleteAfterDays; string token = Auth.CreateUploadToken(mac, putPolicy.ToJsonString()); ResumableUploader target = new ResumableUploader(config); PutExtra extra = new PutExtra { ResumeRecordFile = ResumeHelper.GetDefaultRecordKey(file, key) }; //设置断点续传进度记录文件 uploadResult.AppendLine("record file:" + extra.ResumeRecordFile); // extra.ResumeRecordFile = "test.progress"; //todo:未实现上传进度 HttpResult result = target.UploadStream(fs, key, token, extra); if (result.Code == 200) { uploadResult.AppendLine("上传成功! "); return(true); } else { string s = $"Code={result.Code},Text={result.Text}"; uploadResult.AppendLine("uploadResult:" + s); return(false); } } uploadResult.AppendLine("成员变量putPolicy为空!"); return(false); }
public static HttpResult UploadFile(Stream filestream, string fileName) { Mac mac = new Mac(_config.AccessKey, _config.SecretKey); PutPolicy putPolicy = new PutPolicy(); putPolicy.Scope = _config.Scope; putPolicy.SetExpires(3600); //putPolicy.DeleteAfterDays = 1; string token = Auth.CreateUploadToken(mac, putPolicy.ToJsonString()); Config config = new Config(); // 设置上传区域 switch (_config.Zone) { case QiniuConfiguration.UploadZone.CN_East: config.Zone = Zone.ZONE_CN_East; break; case QiniuConfiguration.UploadZone.CN_North: config.Zone = Zone.ZONE_CN_North; break; case QiniuConfiguration.UploadZone.CN_South: config.Zone = Zone.ZONE_CN_South; break; case QiniuConfiguration.UploadZone.US_North: config.Zone = Zone.ZONE_US_North; break; } // 设置 http 或者 https 上传 config.UseHttps = true; config.UseCdnDomains = true; config.ChunkSize = ChunkUnit.U512K; ResumableUploader target = new ResumableUploader(config); HttpResult result = target.UploadStream(filestream, fileName, token, null); if (result.Code != (int)HttpCode.OK) { Log.Error(result.Text); throw new Exception(result.Text); } return(result); }
/// <summary> /// 上传数据流,根据流长度以及设置的阈值(用户初始化UploadManager时可指定该值)自动选择表单或者分片上传 /// </summary> /// <param name="stream">待上传的数据流,要求:流长度(Stream.Length)是确定的</param> /// <param name="saveKey">要保存的文件名称</param> /// <param name="token">上传凭证</param> /// <returns>上传数据后的返回结果</returns> public HttpResult UploadStream(Stream stream, string saveKey, string token) { HttpResult result = new HttpResult(); if (stream.Length > PUT_THRESHOLD) { ResumableUploader ru = new ResumableUploader(UPLOAD_FROM_CDN); result = ru.UploadStream(stream, saveKey, token, null); } else { FormUploader fu = new FormUploader(UPLOAD_FROM_CDN); result = fu.UploadStream(stream, saveKey, token); } return(result); }
public void UploadStreamTest() { Mac mac = new Mac(AccessKey, SecretKey); string key = FileKey2; string filePath = LocalFile2; Stream fs = File.OpenRead(filePath); 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 = target.UploadStream(fs, key, token, null); Assert.AreEqual((int)HttpCode.OK, result.Code); }
protected virtual XElement WordImageHandler(ImageInfo info) { var dto = imageService.GenerateQiNiuImageToken(null, Guid.Empty, SaveImageType.ArticelContent).Result; string token = dto.token; PutExtra extra = new PutExtra(); using var stream = new MemoryStream(); info.Bitmap.Save(stream, ImageFormat.Jpeg); HttpResult result = target.UploadStream(stream, dto.resource_key, token, extra); if (result.Code == 200) { var dto2 = JsonConvert.DeserializeObject <QiniuReturnDto>(result.Text); return(new XElement(Xhtml.img, new XAttribute(NoNamespace.src, Path.Combine(imageService.baseUrl, dto2.key)))); } else { return(new XElement(Xhtml.img, new XAttribute(NoNamespace.alt, "图片转化失败"))); } }
/// <summary> /// 断点续传 /// </summary> /// <param name="localFile"></param> /// <returns></returns> public string ResumeUploadFile(string localFile) { if (!File.Exists(localFile)) { return("文件不存在"); } var filename = Path.GetFileName(localFile); //文件名 var extension = Path.GetExtension(localFile); //扩展名 var mac = new Mac(AccessKey, SecretKey); var rand = new Random(); string key = $"jnkj_{rand.Next()}{extension}"; Stream fs = File.OpenRead(localFile); var putPolicy = new PutPolicy { Scope = Bucket + ":" + key }; putPolicy.SetExpires(3600); putPolicy.DeleteAfterDays = 1; var token = Auth.CreateUploadToken(mac, putPolicy.ToJsonString()); var config = new Config { UseHttps = true, Zone = Zone.ZONE_CN_East, UseCdnDomains = true, ChunkSize = ChunkUnit.U512K }; var target = new ResumableUploader(config); var extra = new PutExtra { ResumeRecordFile = ResumeHelper.GetDefaultRecordKey(localFile, key) }; //设置断点续传进度记录文件 Console.WriteLine("record file:" + extra.ResumeRecordFile); extra.ResumeRecordFile = "test.progress"; var result = target.UploadStream(fs, key, token, extra); Console.WriteLine("resume upload: " + result); return(result.ToString()); }
public async Task ResumeUploadFileTest() { var mac = new Mac(AccessKey, SecretKey); var rand = new Random(); var key = $"UploadFileTest_{rand.Next()}.dat"; var filePath = LocalFile; Stream fs = File.OpenRead(filePath); var putPolicy = new PutPolicy { Scope = Bucket + ":" + key, DeleteAfterDays = 1 }; putPolicy.SetExpires(3600); var token = Auth.CreateUploadToken(mac, putPolicy.ToJsonString()); var config = new Config { UseHttps = true, Zone = Zone.ZoneCnEast, UseCdnDomains = true, ChunkSize = ChunkUnit.U512K }; var target = new ResumableUploader(config); //设置断点续传进度记录文件 var extra = new PutExtra { ResumeRecordFile = ResumeHelper.GetDefaultRecordKey(filePath, key) }; Console.WriteLine("record file:" + extra.ResumeRecordFile); extra.ResumeRecordFile = "test.progress"; var result = await target.UploadStream(fs, key, token, extra); Console.WriteLine("resume upload: " + result); Assert.Equal((int)HttpCode.OK, result.Code); }
public HttpResult UploadStream(string FileName, Stream stream, PutExtra putExtra) { Mac mac = new Mac(AccessKey, SecretKey); PutPolicy pp = new PutPolicy();//上传参数 pp.Scope = $"{this.Bucket}:{FileName}"; pp.SetExpires(3600); //pp.DeleteAfterDays = 1; string Token = Auth.CreateUploadToken(mac, pp.ToJsonString()); Config config = new Config() { Zone = Zone.ZONE_CN_East, UseHttps = true, UseCdnDomains = true, ChunkSize = ChunkUnit.U1024K }; // FormUploader upload = new FormUploader(config); ResumableUploader resumableUploader = new ResumableUploader(config); HttpResult result = resumableUploader.UploadStream(stream, FileName, Token, putExtra); return(result); }
public override void Process() { string uploadFileName = null; var accessKey = "填写在你七牛后台找到相应的accesskey"; var secretKey = "填写在你七牛后台找到相应的secretkey"; HttpPostedFile file = Request.Files[UploadConfig.UploadFieldName]; Stream myStream = file.InputStream; uploadFileName = file.FileName; if (!CheckFileType(uploadFileName)) { Result.State = UploadState.TypeNotAllow; WriteResult(); return; } if (!CheckFileSize(file.ContentLength)) { Result.State = UploadState.SizeLimitExceed; WriteResult(); return; } Mac mac = new Mac("TYb8ZurNoN-xrUOhnNom_q3ZdPl1OLJqUsvoP0xB", "d7ajdjoitbUiJZQY7ANw5bSf3p6K3nQA8KkGxIDq"); // 上传文件名 string key = $"{Guid.NewGuid().ToString().Replace("-","")}{uploadFileName}"; // 本地文件路径 //string filePath = @"E:\MY\FastDevStu\Mvc\Models\微信图片_20190508193805.gif"; // 空间名 string Bucket = "cjj81pic"; // 设置上传策略,详见:https://developer.qiniu.com/kodo/manual/1206/put-policy PutPolicy putPolicy = new PutPolicy(); putPolicy.Scope = Bucket + ":" + key; putPolicy.SetExpires(3600); string token = Auth.CreateUploadToken(mac, putPolicy.ToJsonString()); Qiniu.Storage.Config config = new Qiniu.Storage.Config(); // 设置上传区域 config.Zone = Zone.ZONE_CN_South; // 设置 http 或者 https 上传 config.UseHttps = true; config.UseCdnDomains = true; config.ChunkSize = ChunkUnit.U512K; ResumableUploader target = new ResumableUploader(config); PutExtra extra = new PutExtra(); //设置断点续传进度记录文件,,报文件没又访问权限 //extra.ResumeRecordFile = ResumeHelper.GetDefaultRecordKey(uploadFileName, key); //Console.WriteLine("record file:" + extra.ResumeRecordFile); //extra.ResumeRecordFile = "test.progress"; try { HttpResult result = target.UploadStream(myStream, key, token, extra); if (result.Code == 200) { this.Result.State = UploadState.Success; // this.Result.Url = $"http://ptdzsd6xm.bkt.clouddn.com{Path.DirectorySeparatorChar}{key}"; this.Result.Url = key; } } catch (Exception ex) { throw ex; } finally { WriteResult(); } }