public ResumeUploader(HttpManager httpManager, ResumeRecorder recorder, string recordKey, Stream stream, string key, string token, UploadOptions uploadOptions, UpCompletionHandler upCompletionHandler) { this.httpManager = httpManager; this.resumeRecorder = recorder; this.recordKey = recordKey; this.fileStream = stream; this.key = key; this.storage = IsolatedStorageFile.GetUserStoreForApplication(); this.uploadOptions = (uploadOptions == null) ? UploadOptions.defaultOptions() : uploadOptions; this.upCompletionHandler = new UpCompletionHandler(delegate(string fileKey, ResponseInfo respInfo, string response) { uploadOptions.ProgressHandler(key, 1.0); if (this.fileStream != null) { try { this.fileStream.Close(); } catch (Exception) { } } if (upCompletionHandler != null) { upCompletionHandler(key, respInfo, response); } }); this.httpManager.setAuthHeader("UpToken " + token); this.chunkBuffer = new byte[Config.CHUNK_SIZE]; }
public void resumeUploadTest() { //Settings.load(); Settings.LoadFromFile(); Mac mac = new Mac(Settings.AccessKey, Settings.SecretKey); ResumeRecorder recorder = new ResumeRecorder("dir"); string token = "<token>"; ResumeUploader target = new ResumeUploader(recorder, "big.record", "F:\\big.dat", "big_ResumeUpload.dat", token, null, null); target.uploadFile(); }
public void uploadFile(string filePath, string key, string token, UploadOptions uploadOptions, UpCompletionHandler upCompletionHandler) { try { if(upCompletionHandler==null) { upCompletionHandler = DefaultUpCompletionHandler; } long fileSize = 0; FileInfo s = new FileInfo(filePath); fileSize = s.Length; //判断文件大小,选择上传方式 if (fileSize <= Config.PUT_THRESHOLD) { new FormUploader().uploadFile(filePath, key, token, uploadOptions, upCompletionHandler); } else { if(this.resumeRecorder==null) { string home = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile); this.resumeRecorder = new ResumeRecorder(home); } string recorderKey = null; if (this.keyGenerator == null) { recorderKey = string.Format("qiniu_{0}.resume", Util.StringUtils.md5Hash(filePath + key)); } else { recorderKey = this.keyGenerator(); } new ResumeUploader(this.resumeRecorder, recorderKey, filePath, key, token, uploadOptions, upCompletionHandler).uploadFile(); } } catch (Exception ex) { if (upCompletionHandler != null) { upCompletionHandler(key, ResponseInfo.fileError(ex), null); } } }
/// <summary> /// 构建分片上传对象 /// </summary> /// <param name="recorder">分片上传进度记录器</param> /// <param name="recordKey">分片上传进度记录文件名</param> /// <param name="filePath">上传的文件全路径</param> /// <param name="key">保存在七牛的文件名</param> /// <param name="token">上传凭证</param> /// <param name="uploadOptions">上传可选设置</param> /// <param name="upCompletionHandler">上传完成结果处理器</param> public ResumeUploader(ResumeRecorder recorder, string recordKey, string filePath, string key, string token, UploadOptions uploadOptions, UpCompletionHandler upCompletionHandler) { this.mHttpManager = new HttpManager(); this.resumeRecorder = recorder; this.recordKey = recordKey; this.filePath = filePath; this.key = key; this.uploadOptions = (uploadOptions == null) ? UploadOptions.defaultOptions() : uploadOptions; this.upCompletionHandler = new UpCompletionHandler(delegate(string fileKey, ResponseInfo respInfo, string response) { if (respInfo.isOk()) { this.uploadOptions.ProgressHandler(key, 1.0); } if (this.fileStream != null) { try { this.fileStream.Close(); this.fileStream = null; } catch (Exception) { } } try { if (upCompletionHandler != null) { upCompletionHandler(key, respInfo, response); } } catch (Exception ex) { Console.WriteLine("resumable upload completion error, {0}", ex.Message); } }); string upTokenHeader = string.Format("UpToken {0}", token); this.upHeaders = new Dictionary<string, string>(); this.upHeaders.Add("Authorization", upTokenHeader); this.chunkBuffer = new byte[Config.CHUNK_SIZE]; }
public static void uploadBigFile() { Mac mac = new Mac(Settings.AccessKey, Settings.SecretKey); string bucket = "BUCKET"; string saveKey = "SAVE_KEY"; string localFile = "LOCAL_FILE"; string recordPath = "RECORD_PATH"; string recordFile = "RECORD_FILE"; PutPolicy putPolicy = new PutPolicy(); putPolicy.Scope = bucket; putPolicy.SetExpires(3600); putPolicy.DeleteAfterDays = 1; string token = Auth.createUploadToken(putPolicy, mac); ResumeRecorder rr = new ResumeRecorder(recordPath); UploadOptions uploadOptions = new UploadOptions( null, // ExtraParams null, // MimeType false, // CheckCrc32 new UpProgressHandler(OnUploadProgressChanged), // 上传进度 null // CancelSignal ); UpCompletionHandler uploadCompleted = new UpCompletionHandler(OnUploadCompleted); // 上传完毕 ResumeUploader ru = new ResumeUploader( rr, // 续传记录 recordFile, // 续传记录文件 localFile, // 待上传的本地文件 saveKey, // 要保存的文件名 token, // 上传凭证 uploadOptions, // 上传选项(其中包含进度处理),可为null uploadCompleted // 上传完毕事件处理 ); ru.uploadFile(); }
/// <summary> /// 以指定的分片上传进度记录器和分片上传记录文件名构建上传管理器 /// /// 可以指定这两个参数来使分片上传支持断点续传功能 /// </summary> /// <param name="recorder">分片上传进度记录器</param> /// <param name="generator">分片上传进度记录文件名</param> public UploadManager(ResumeRecorder recorder, KeyGenerator generator) { this.resumeRecorder = recorder; this.keyGenerator = generator; }
/// <summary> /// 默认上传管理器 /// </summary> public UploadManager() { this.resumeRecorder = null; this.keyGenerator = null; }
public void uploadStream(Stream stream, string key, string token, UploadOptions uploadOptions, UpCompletionHandler upCompletionHandler) { long fileSize = stream.Length; if (fileSize <= Config.PUT_THRESHOLD) { new FormUploader().uploadStream(stream, key, token, uploadOptions, upCompletionHandler); } else { if (this.resumeRecorder == null) { string home = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile); this.resumeRecorder = new ResumeRecorder(home); } string recorderKey = null; if (this.keyGenerator == null) { recorderKey = string.Format("qiniu_{0}.resume", Util.StringUtils.md5Hash(key)); } else { recorderKey = this.keyGenerator(); } new ResumeUploader(this.resumeRecorder, recorderKey, stream, key, token, uploadOptions, upCompletionHandler).uploadStream(); } }