コード例 #1
0
        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];
        }
コード例 #2
0
        /// <summary>
        /// 上传文件流
        /// </summary>
        /// <param name="stream">文件流对象</param>
        /// <param name="key">保存在七牛的文件名</param>
        /// <param name="token">上传凭证</param>
        /// <param name="uploadOptions">上传可选设置</param>
        /// <param name="upCompletionHandler">上传结果处理器</param>
        #region   文件流
        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();
            }
        }
コード例 #3
0
        private void upload(string filePath, string fileName, string qetag, string uploadToken)
        {
            //set button state
            toggleButtonState(UploadButton, false);
            toggleButtonState(HaltButton, true);

            string         recordKey     = StringUtils.urlSafeBase64Encode(qetag + ":" + fileName);
            ResumeRecorder recorder      = new ResumeRecorder("record");
            UploadManager  uploadManager = new UploadManager(recorder, new KeyGenerator(delegate() { return(recordKey); }));
            UploadOptions  uploadOptions = UploadOptions.defaultOptions();

            uploadOptions.ProgressHandler = new UpProgressHandler(delegate(string key, double percent)
            {
                updateProgress(percent);
            });
            uploadOptions.CancellationSignal = new UpCancellationSignal(delegate() { return(this.cancelSignal); });
            UpCompletionHandler upCompletionHandler = new UpCompletionHandler(delegate(string key, ResponseInfo respInfo, string response)
            {
                if (respInfo.isOk())
                {
                    appendLog("文件上传成功!");
                }
                else
                {
                    appendLog("文件上传失败,原因如下:");
                    appendLog(respInfo.ToString());
                }
                toggleButtonState(UploadButton, true);
                toggleButtonState(HaltButton, false);
            });

            appendLog("正在上传...");
            uploadManager.uploadFile(filePath, fileName, uploadToken, uploadOptions, upCompletionHandler);
        }
コード例 #4
0
        public void resumeUploadTest()
        {
            Mac mac = new Mac(Settings.AccessKey, Settings.SecretKey);

            ResumeRecorder recorder = new ResumeRecorder("dir");
            string         token    = "<token>";
            ResumeUploader target   = new ResumeUploader(recorder, "test.txt", "F:\\test.txt", "test.txt", token, null, null);

            target.uploadFile();
        }
コード例 #5
0
        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();
        }
コード例 #6
0
        /// <summary>
        /// 上传沙盒文件
        /// </summary>
        /// <param name="filePath">沙盒文件全路径</param>
        /// <param name="key">保存在七牛的文件名</param>
        /// <param name="token">上传凭证</param>
        /// <param name="uploadOptions">上传可选设置</param>
        /// <param name="upCompletionHandler">上传结果处理器</param>
        #region   文件
        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);
                }
            }
        }
コード例 #7
0
        public ResumeUploader(ResumeRecorder recorder, string recordKey, Stream stream,
                              string key, string token, UploadOptions uploadOptions, UpCompletionHandler upCompletionHandler)
        {
            this.mHttpManager        = new HttpManager();
            this.resumeRecorder      = recorder;
            this.recordKey           = recordKey;
            this.fileStream          = stream;
            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];
        }
コード例 #8
0
        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();
        }
コード例 #9
0
        public static void uploadBigFile()
        {
            Mac mac = new Mac(Settings.AccessKey, Settings.SecretKey);

            string bucket     = "TARGET_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);
            ResumeUploader ru = new ResumeUploader(rr, recordFile, localFile, saveKey, token, null, null);

            ru.uploadFile();
        }
コード例 #10
0
 /// <summary>
 /// 以指定的分片上传进度记录器和分片上传记录文件名构建上传管理器
 ///
 /// 可以指定这两个参数来使分片上传支持断点续传功能
 /// </summary>
 /// <param name="recorder">分片上传进度记录器</param>
 /// <param name="generator">分片上传进度记录文件名</param>
 public UploadManager(ResumeRecorder recorder, KeyGenerator generator)
 {
     this.resumeRecorder = recorder;
     this.keyGenerator   = generator;
 }
コード例 #11
0
 /// <summary>
 /// 默认上传管理器
 /// </summary>
 public UploadManager()
 {
     this.resumeRecorder = null;
     this.keyGenerator   = null;
 }
コード例 #12
0
ファイル: UploadManager.cs プロジェクト: qiniu/wp-sdk
 /// <summary>
 /// 以指定HttpManager构建上传管理器
 /// </summary>
 /// <param name="httpManager">HttpManager对象</param>
 public UploadManager(HttpManager httpManager)
 {
     this.httpManager    = httpManager;
     this.resumeRecorder = null;
     this.keyGenerator   = null;
 }