Ejemplo n.º 1
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
     {
         long fileSize = 0;
         using (IsolatedStorageFileStream s = new IsolatedStorageFileStream(filePath, FileMode.Open,
                                                                            IsolatedStorageFile.GetUserStoreForApplication()))
         {
             fileSize = s.Length;
         }
         //判断文件大小,选择上传方式
         if (fileSize <= Config.PUT_THRESHOLD)
         {
             new FormUploader().uploadFile(this.httpManager, filePath, key, token, uploadOptions, upCompletionHandler);
         }
         else
         {
             string recorderKey = null;
             if (this.keyGenerator != null)
             {
                 recorderKey = this.keyGenerator();
             }
             new ResumeUploader(this.httpManager, this.resumeRecorder, recorderKey, filePath, key, token, uploadOptions, upCompletionHandler).uploadFile();
         }
     }
     catch (Exception ex)
     {
         if (upCompletionHandler != null)
         {
             upCompletionHandler(key, ResponseInfo.fileError(ex), null);
         }
     }
 }
Ejemplo n.º 2
0
        public static void uploadFile()
        {
            Mac mac = new Mac(Settings.AccessKey, Settings.SecretKey);

            string bucket    = "BUCKET";
            string saveKey   = "SAVE_KEY";
            string localFile = "LOCAL_FILE";

            PutPolicy putPolicy = new PutPolicy();

            putPolicy.Scope = bucket;
            putPolicy.SetExpires(3600);
            putPolicy.DeleteAfterDays = 1;
            string token = Auth.createUploadToken(putPolicy, mac);

            UploadOptions uploadOptions = null;

            // 上传完毕事件处理
            UpCompletionHandler uploadCompleted = new UpCompletionHandler(OnUploadCompleted);

            // 方式1:使用UploadManager
            //默认设置 Qiniu.Common.Config.PUT_THRESHOLD = 512*1024;
            //可以适当修改,UploadManager会根据这个阈值自动选择是否使用分片(Resumable)上传
            UploadManager um = new UploadManager();

            um.uploadFile(localFile, saveKey, token, uploadOptions, uploadCompleted);

            // 方式2:使用FormManager
            //FormUploader fm = new FormUploader();
            //fm.uploadFile(localFile, saveKey, token, uploadOptions, uploadCompleted);
        }
Ejemplo n.º 3
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];
        }
        public void uploadData(byte[] data, int offset, int count, string key, string token,
                               UploadOptions uploadOptions, UpCompletionHandler upCompletionHandler)
        {
            HttpFormFile fFile = HttpFormFile.NewFileFromSlice(key, null, data, offset, count);

            upload(fFile, key, token, uploadOptions, upCompletionHandler);
        }
Ejemplo n.º 5
0
        public void UploadFile(QiNiuConfig qiNiuConfig)
        {
            // 上传策略
            PutPolicy putPolicy = new PutPolicy();

            // 设置要上传的目标空间
            putPolicy.Scope = qiNiuConfig.Bucket;
            // 上传策略的过期时间(单位:秒)
            putPolicy.SetExpires(3600);
            // 文件上传完毕后,在多少天后自动被删除
            //putPolicy.DeleteAfterDays = 1;
            // 请注意这里的Zone设置(如果不设置,就默认为华东机房)
            // var zoneId = Qiniu.Common.AutoZone.Query(AK,BUCKET);
            // Qiniu.Common.Config.ConfigZone(zoneId);
            Mac mac = new Mac(qiNiuConfig.AK, qiNiuConfig.SK); // Use AK & SK here
            // 生成上传凭证
            string        uploadToken   = Auth.createUploadToken(putPolicy, mac);
            UploadOptions uploadOptions = null;
            // 上传完毕事件处理
            UpCompletionHandler uploadCompleted = new UpCompletionHandler(OnUploadCompleted);
            // 方式1:使用UploadManager
            //默认设置 Qiniu.Common.Config.PUT_THRESHOLD = 512*1024;
            //可以适当修改,UploadManager会根据这个阈值自动选择是否使用分片(Resumable)上传
            UploadManager um = new UploadManager();

            um.uploadFile(qiNiuConfig.LocalFile, qiNiuConfig.SaveKey, uploadToken, uploadOptions, qiNiuConfig.UpHandler);
            // 方式2:使用FormManager
            //FormUploader fm = new FormUploader();
            //fm.uploadFile(localFile, saveKey, token, uploadOptions, uploadCompleted);
        }
        /// <summary>
        /// 上传文件
        /// </summary>
        /// <param name="httpManager">HttpManager对象</param>
        /// <param name="filePath">文件的完整路径</param>
        /// <param name="key">保存在七牛的文件名</param>
        /// <param name="token">上传凭证</param>
        /// <param name="uploadOptions">上传可选设置</param>
        /// <param name="upCompletionHandler">上传完成结果处理器</param>
        public void uploadFile(string filePath, string key,
                               string token, UploadOptions uploadOptions, UpCompletionHandler upCompletionHandler)
        {
            HttpFormFile fFile = HttpFormFile.NewFileFromPath(key, null, filePath);

            upload(fFile, key, token, uploadOptions, upCompletionHandler);
        }
Ejemplo n.º 7
0
        public static void uploadFile()
        {
            Mac mac = new Mac(Settings.AccessKey, Settings.SecretKey);

            string bucket = "BUCKET";
            string saveKey = "SAVE_KEY";
            string localFile = "LOCAL_FILE";

            PutPolicy putPolicy = new PutPolicy();
            putPolicy.Scope = bucket;
            putPolicy.SetExpires(3600);
            putPolicy.DeleteAfterDays = 1;
            string token = Auth.createUploadToken(putPolicy, mac);

            UploadOptions uploadOptions = null;

            // 上传完毕事件处理
            UpCompletionHandler uploadCompleted = new UpCompletionHandler(OnUploadCompleted);

            // 方式1:使用UploadManager
            //默认设置 Qiniu.Common.Config.PUT_THRESHOLD = 512*1024;
            //可以适当修改,UploadManager会根据这个阈值自动选择是否使用分片(Resumable)上传
            UploadManager um = new UploadManager();
            um.uploadFile(localFile, saveKey, token, uploadOptions, uploadCompleted);

            // 方式2:使用FormManager
            //FormUploader fm = new FormUploader();
            //fm.uploadFile(localFile, saveKey, token, uploadOptions, uploadCompleted);
        }
        /// <summary>
        /// 以表单方式上传数据流
        /// </summary>
        /// <param name="stream">文件数据流</param>
        /// <param name="key">保存在七牛的文件名</param>
        /// <param name="token">上传凭证</param>
        /// <param name="uploadOptions">上传可选设置</param>
        /// <param name="upCompletionHandler">上传完成结果处理器</param>
        public void uploadStream(Stream stream, string key, string token,
                                 UploadOptions uploadOptions, UpCompletionHandler upCompletionHandler)
        {
            HttpFormFile fFile = HttpFormFile.NewFileFromStream(key, null, stream);

            upload(fFile, key, token, uploadOptions, upCompletionHandler);
        }
Ejemplo n.º 9
0
        public void uploadStreamTest()
        {
            try
            {
                Mac mac = new Mac(Settings.AccessKey, Settings.SecretKey);

                UploadManager target = new UploadManager();

                string     key       = "s" + SmartHeatStationParaInfo.TimeHelper.GetUpLoadTime() + ".txt";
                string     filePath  = "stations.txt";
                FileStream fs        = new FileStream(filePath, FileMode.Open, FileAccess.Read);
                PutPolicy  putPolicy = new PutPolicy();
                putPolicy.Scope = Settings.Bucket + ":" + key;
                putPolicy.SetExpires(3600);
                putPolicy.DeleteAfterDays = 1;
                string        token         = Auth.createUploadToken(putPolicy, mac);
                UploadOptions uploadOptions = null;

                UpCompletionHandler upCompletionHandler = new UpCompletionHandler(delegate(string fileKey, ResponseInfo respInfo, string response)
                {
                });
                target.uploadStream(fs, key, token, uploadOptions, upCompletionHandler);
            }
            catch
            {
            }
        }
Ejemplo n.º 10
0
        public void uploadStreamTest(string _key, string _filePath)
        {
            Mac mac = new Mac(Settings.AccessKey, Settings.SecretKey);

            UploadManager target = new UploadManager();

            string key      = _key;
            string filePath = _filePath;

            FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);

            PutPolicy putPolicy = new PutPolicy();

            putPolicy.Scope = Settings.Bucket + ":" + key;
            putPolicy.SetExpires(3600);
            putPolicy.DeleteAfterDays = 1;
            string        token         = Auth.createUploadToken(putPolicy, mac);
            UploadOptions uploadOptions = null;

            UpCompletionHandler upCompletionHandler = new UpCompletionHandler(delegate(string fileKey, ResponseInfo respInfo, string response)
            {
            });

            target.uploadStream(fs, key, token, uploadOptions, upCompletionHandler);
        }
Ejemplo n.º 11
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();
            }
        }
Ejemplo n.º 12
0
        public void DoUpLoad()
        {
            //string AK = "ACCESS_KEY";
            //string SK = "SECRET_KEY";
            // 目标空间名
            //string bucket = "TARGET_BUCKET";
            // 目标文件名
            string saveKey = "stations" + TimeHelper.GetUpLoadTime() + ".txt";
            // 本地文件
            string localFile = Path.Combine(Application.StartupPath, "stations.txt");
            // 上传策略
            PutPolicy putPolicy = new PutPolicy();

            // 设置要上传的目标空间
            putPolicy.Scope = Bucket;
            // 上传策略的过期时间(单位:秒)
            putPolicy.SetExpires(3600);
            // 文件上传完毕后,在多少天后自动被删除
            putPolicy.DeleteAfterDays = 1;
            // 请注意这里的Zone设置(如果不设置,就默认为华东机房)
            // var zoneId = Qiniu.Common.AutoZone.Query(AK,BUCKET);
            // Qiniu.Common.Config.ConfigZone(zoneId);
            Mac mac = new Mac(AccessKey, SecretKey); // Use AK & SK here
            // 生成上传凭证
            string        token         = Auth.createUploadToken(putPolicy, mac);
            UploadOptions uploadOptions = null;
            // 上传完毕事件处理
            UpCompletionHandler uploadCompleted = new UpCompletionHandler(OnUploadCompleted);
            // 方式1:使用UploadManager
            //默认设置 Qiniu.Common.Config.PUT_THRESHOLD = 512*1024;
            //可以适当修改,UploadManager会根据这个阈值自动选择是否使用分片(Resumable)上传
            UploadManager um = new UploadManager();

            um.uploadFile(localFile, saveKey, token, uploadOptions, uploadCompleted);
        }
Ejemplo n.º 13
0
 /// <summary>
 /// 以表单方式上传字节数据
 /// </summary>
 /// <param name="data">字节数据</param>
 /// <param name="key">保存在七牛的文件名</param>
 /// <param name="token">上传凭证</param>
 /// <param name="uploadOptions">上传可选设置</param>
 /// <param name="upCompletionHandler">上传完成结果处理器</param>
 public void uploadData(byte[] data, string key,
     string token, UploadOptions uploadOptions, UpCompletionHandler upCompletionHandler)
 {
     HttpFormFile fFile = HttpFormFile.NewFileFromBytes(key, null, data);
     // 此处未设置FormFile.ContentType,稍后设置(在upload中已设置) @fengyh 2016-08-17 15:03
     upload(fFile, key, token, uploadOptions, upCompletionHandler);
 }
Ejemplo n.º 14
0
        public void uploadStreamTest()
        {
            //Settings.load();
            Settings.LoadFromFile();
            UploadManager target   = new UploadManager();
            Mac           mac      = new Mac(Settings.AccessKey, Settings.SecretKey);
            string        key      = "test_UploadManagerUploadStream.png";
            string        filePath = "F:\\test.png";

            FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);

            PutPolicy putPolicy = new PutPolicy();

            putPolicy.Scope = Settings.Bucket;
            putPolicy.SetExpires(3600);
            putPolicy.DeleteAfterDays = 1;
            string        token         = Auth.createUploadToken(putPolicy, mac);
            UploadOptions uploadOptions = null;

            UpCompletionHandler upCompletionHandler = new UpCompletionHandler(delegate(string fileKey, ResponseInfo respInfo, string response)
            {
                Assert.AreEqual(200, respInfo.StatusCode);
            });

            target.uploadStream(fs, key, token, uploadOptions, upCompletionHandler);
        }
Ejemplo n.º 15
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];
        }
Ejemplo n.º 16
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
            {
                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
                {
                    string recorderKey = null;
                    if (this.keyGenerator != null)
                    {
                        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);
                }
            }
        }
Ejemplo n.º 17
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);
        }
Ejemplo n.º 18
0
        /// <summary>
        /// 以表单方式上传字节数据
        /// </summary>
        /// <param name="data">字节数据</param>
        /// <param name="key">保存在七牛的文件名</param>
        /// <param name="token">上传凭证</param>
        /// <param name="uploadOptions">上传可选设置</param>
        /// <param name="upCompletionHandler">上传完成结果处理器</param>
        public void uploadData(byte[] data, string key,
                               string token, UploadOptions uploadOptions, UpCompletionHandler upCompletionHandler)
        {
            HttpFormFile fFile = HttpFormFile.NewFileFromBytes(key, null, data);

            // 此处未设置FormFile.ContentType,稍后设置(在upload中已设置) @fengyh 2016-08-17 15:03
            upload(fFile, key, token, uploadOptions, upCompletionHandler);
        }
Ejemplo n.º 19
0
 /// <summary>
 /// 上传沙盒文件
 /// </summary>
 /// <param name="httpManager">HttpManager对象</param>
 /// <param name="filePath">沙盒文件的完整路径</param>
 /// <param name="key">保存在七牛的文件名</param>
 /// <param name="token">上传凭证</param>
 /// <param name="uploadOptions">上传可选设置</param>
 /// <param name="upCompletionHandler">上传完成结果处理器</param>
 public void uploadFile(HttpManager httpManager, string filePath, string key,
     string token, UploadOptions uploadOptions, UpCompletionHandler upCompletionHandler)
 {
     PostArgs postArgs = new PostArgs();
     postArgs.File = filePath;
     postArgs.FileName = Path.GetFileName(filePath);
     httpManager.FileContentType = PostContentType.FILE;
     upload(httpManager, postArgs, key, token, uploadOptions, upCompletionHandler);
 }
Ejemplo n.º 20
0
        /// <summary>
        /// 上传沙盒文件
        /// </summary>
        /// <param name="httpManager">HttpManager对象</param>
        /// <param name="filePath">沙盒文件的完整路径</param>
        /// <param name="key">保存在七牛的文件名</param>
        /// <param name="token">上传凭证</param>
        /// <param name="uploadOptions">上传可选设置</param>
        /// <param name="upCompletionHandler">上传完成结果处理器</param>
        public void uploadFile(HttpManager httpManager, string filePath, string key,
                               string token, UploadOptions uploadOptions, UpCompletionHandler upCompletionHandler)
        {
            PostArgs postArgs = new PostArgs();

            postArgs.File               = filePath;
            postArgs.FileName           = Path.GetFileName(filePath);
            httpManager.FileContentType = PostContentType.FILE;
            upload(httpManager, postArgs, key, token, uploadOptions, upCompletionHandler);
        }
Ejemplo n.º 21
0
 /// <summary>
 /// 以表单方式上传数据流
 /// </summary>
 /// <param name="httpManager">HttpManager对象</param>
 /// <param name="stream">文件数据流</param>
 /// <param name="key">保存在七牛的文件名</param>
 /// <param name="token">上传凭证</param>
 /// <param name="uploadOptions">上传可选设置</param>
 /// <param name="upCompletionHandler">上传完成结果处理器</param>
 public void uploadStream(HttpManager httpManager, Stream stream, string key, string token,
     UploadOptions uploadOptions, UpCompletionHandler upCompletionHandler)
 {
     PostArgs postArgs = new PostArgs();
     postArgs.Stream = stream;
     if (key != null)
     {
         postArgs.FileName = key;
     }
     httpManager.FileContentType = PostContentType.STREAM;
     upload(httpManager, postArgs, key, token, uploadOptions, upCompletionHandler);
 }
Ejemplo n.º 22
0
 /// <summary>
 /// 以表单方式上传字节数据
 /// </summary>
 /// <param name="httpManager">HttpManager对象</param>
 /// <param name="data">字节数据</param>
 /// <param name="key">保存在七牛的文件名</param>
 /// <param name="token">上传凭证</param>
 /// <param name="uploadOptions">上传可选设置</param>
 /// <param name="upCompletionHandler">上传完成结果处理器</param>
 public void uploadData(HttpManager httpManager, byte[] data, string key,
     string token, UploadOptions uploadOptions, UpCompletionHandler upCompletionHandler)
 {
     PostArgs postArgs = new PostArgs();
     postArgs.Data = data;
     if (key != null)
     {
         postArgs.FileName = key;
     }
     httpManager.FileContentType = PostContentType.BYTES;
     upload(httpManager, postArgs, key, token, uploadOptions, upCompletionHandler);
 }
Ejemplo n.º 23
0
        /// <summary>
        /// 以表单方式上传字节数据
        /// </summary>
        /// <param name="httpManager">HttpManager对象</param>
        /// <param name="data">字节数据</param>
        /// <param name="key">保存在七牛的文件名</param>
        /// <param name="token">上传凭证</param>
        /// <param name="uploadOptions">上传可选设置</param>
        /// <param name="upCompletionHandler">上传完成结果处理器</param>
        public void uploadData(HttpManager httpManager, byte[] data, string key,
                               string token, UploadOptions uploadOptions, UpCompletionHandler upCompletionHandler)
        {
            PostArgs postArgs = new PostArgs();

            postArgs.Data = data;
            if (key != null)
            {
                postArgs.FileName = key;
            }
            httpManager.FileContentType = PostContentType.BYTES;
            upload(httpManager, postArgs, key, token, uploadOptions, upCompletionHandler);
        }
Ejemplo n.º 24
0
        /// <summary>
        /// 以表单方式上传数据流
        /// </summary>
        /// <param name="httpManager">HttpManager对象</param>
        /// <param name="stream">文件数据流</param>
        /// <param name="key">保存在七牛的文件名</param>
        /// <param name="token">上传凭证</param>
        /// <param name="uploadOptions">上传可选设置</param>
        /// <param name="upCompletionHandler">上传完成结果处理器</param>
        public void uploadStream(HttpManager httpManager, Stream stream, string key, string token,
                                 UploadOptions uploadOptions, UpCompletionHandler upCompletionHandler)
        {
            PostArgs postArgs = new PostArgs();

            postArgs.Stream = stream;
            if (key != null)
            {
                postArgs.FileName = key;
            }
            httpManager.FileContentType = PostContentType.STREAM;
            upload(httpManager, postArgs, key, token, uploadOptions, upCompletionHandler);
        }
Ejemplo n.º 25
0
        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);
                }
            }
        }
Ejemplo n.º 26
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);
                }
            }
        }
Ejemplo n.º 27
0
        private void button2_Click(object sender, EventArgs e)
        {
            string localFile = textBox1.Text.Trim();

            FileInfo fi = new FileInfo(localFile);

            string saveKey = fi.Name;
            string bucket  = comboBox1.Text;

            // 生成上传凭证
            Mac mac = this.GetMac();

            BucketManager bm = new BucketManager(mac);
            //BucketsResult rs = bm.buckets();

            DomainsResult dr = bm.domains(bucket);

            this.BucDomain = dr.Domains[0];
            //this.BucDomain

            // 上传策略
            PutPolicy putPolicy = new PutPolicy();

            // 设置要上传的目标空间
            putPolicy.Scope = bucket;
            // 上传策略的过期时间(单位:秒)
            putPolicy.SetExpires(3600);
            // 文件上传完毕后,在多少天后自动被删除
            putPolicy.DeleteAfterDays = 0;
            // 请注意这里的Zone设置(如果不设置,就默认为华东机房)
            // var zoneId = Qiniu.Common.AutoZone.Query(AK,BUCKET);
            // Qiniu.Common.Config.ConfigZone(zoneId);
            string uploadToken = Auth.createUploadToken(putPolicy, mac);

            UploadOptions uploadOptions = null;
            // 上传完毕事件处理
            UpCompletionHandler uploadCompleted = new UpCompletionHandler(OnUploadCompleted);
            // 方式1:使用UploadManager
            //默认设置 Qiniu.Common.Config.PUT_THRESHOLD = 512*1024;
            //可以适当修改,UploadManager会根据这个阈值自动选择是否使用分片(Resumable)上传
            UploadManager um = new UploadManager();

            um.uploadFile(localFile, saveKey, uploadToken, uploadOptions, uploadCompleted);
            // 方式2:使用FormManager
            //FormUploader fm = new FormUploader();
            //fm.uploadFile(localFile, saveKey, token, uploadOptions, uploadCompleted);
            //Console.ReadKey();
        }
Ejemplo n.º 28
0
        /// <summary>
        /// 七牛初始化
        /// </summary>
        private void QiniuInit()
        {
            var policy = new PutPolicy();     // 上传策略实例

            policy.Scope = PresetInfo.bucket; // 设置要上传的目标空间
            policy.SetExpires(86400);         // 上传策略的过期时间(单位:秒),当前设为24小时
            var zoneId = Qiniu.Common.AutoZone.Query(PresetInfo.QiniuAccessKey, PresetInfo.bucket);

            // 这里的Zone设置(如果不设置,就默认为华东机房)
            Qiniu.Common.Config.ConfigZone(zoneId);
            var mac = new Mac(PresetInfo.QiniuAccessKey, PresetInfo.QiniuSecretKey);

            _uploadToken = Auth.createUploadToken(policy, mac); // 生成上传凭证

            // 上传完毕事件处理
            _uploadCompleted = new UpCompletionHandler(OnQiniuUploadCompleted);
        }
Ejemplo n.º 29
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(this.httpManager, stream, key, token, uploadOptions, upCompletionHandler);
     }
     else
     {
         string recorderKey = null;
         if (this.keyGenerator != null)
         {
             recorderKey = this.keyGenerator();
         }
         new ResumeUploader(this.httpManager, this.resumeRecorder, recorderKey, stream, key, token, uploadOptions, upCompletionHandler).uploadStream();
     }
 }
Ejemplo n.º 30
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];
        }
Ejemplo n.º 31
0
        private void UploadFile(StockConfigModel model)
        {
            try
            {
                SaveFileFlag = SaveAsDefaultFile(Utils.FileNameAoto, model);
                // 本地文件
                string localFile = Utils.FileNameAoto;
                // 上传策略
                PutPolicy putPolicy = new PutPolicy();
                // 设置要上传的目标空间
                putPolicy.Scope = Utils.bucket;
                // 上传策略的过期时间(单位:秒)
                putPolicy.SetExpires(3600);
                // 文件上传完毕后,在多少天后自动被删除
                //putPolicy.DeleteAfterDays = 1;
                // 请注意这里的Zone设置(如果不设置,就默认为华东机房)
                var zoneId = Qiniu.Common.AutoZone.Query(AK, Utils.bucket);
                Qiniu.Common.Config.ConfigZone(zoneId);
                //Mac mac = new Mac(AK, SK); // Use AK & SK here
                // 生成上传凭证
                string        uploadToken   = Auth.createUploadToken(putPolicy, mac);
                UploadOptions uploadOptions = null;

                // 上传完毕事件处理
                UpCompletionHandler uploadCompleted = new UpCompletionHandler(OnUploadCompleted);
                // 方式1:使用UploadManager
                //默认设置 Qiniu.Common.Config.PUT_THRESHOLD = 512*1024;
                //可以适当修改,UploadManager会根据这个阈值自动选择是否使用分片(Resumable)上传
                UploadManager um = new UploadManager();

                DelFileFlag = DelFile(Utils.FileNameAoto);

                um.uploadFile(localFile, Utils.FileNameAoto, uploadToken, uploadOptions, uploadCompleted);
                // 方式2:使用FormManager
                //FormUploader fm = new FormUploader();
                //fm.uploadFile(localFile, saveKey, token, uploadOptions, uploadCompleted);
            }
            catch (Exception ex)
            {
                string text = "上传文件出现错误" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                log.ErrorFormat(text, ex.Message);
            }
        }
Ejemplo n.º 32
0
        /// <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];
        }
Ejemplo n.º 33
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
            {
                string recorderKey = null;
                if (this.keyGenerator != null)
                {
                    recorderKey = this.keyGenerator();
                }
                new ResumeUploader(this.resumeRecorder, recorderKey, stream, key, token, uploadOptions, upCompletionHandler).uploadStream();
            }
        }
Ejemplo n.º 34
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();
        }
Ejemplo n.º 35
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();
        }
Ejemplo n.º 36
0
        public void uploadDataTest()
        {
            //Settings.load();
            Settings.LoadFromFile("F:\\test.cfg");
            Mac mac = new Mac(Settings.AccessKey, Settings.SecretKey);
            FormUploader target = new FormUploader();
            byte[] data = Encoding.UTF8.GetBytes("hello world");
            string key = "test_FormUploaderUploadData.txt";

            PutPolicy putPolicy = new PutPolicy();
            putPolicy.Scope = Settings.Bucket;
            putPolicy.SetExpires(3600);
            putPolicy.DeleteAfterDays = 1;
            string token = Auth.createUploadToken(putPolicy, mac);
            UploadOptions uploadOptions = null;
            UpCompletionHandler upCompletionHandler = new UpCompletionHandler(delegate(string fileKey, ResponseInfo respInfo, string response)
            {
                Assert.AreEqual(200, respInfo.StatusCode);
            });
            target.uploadData(data, key, token, uploadOptions, upCompletionHandler);
        }
Ejemplo n.º 37
0
        public void uploadFileTest()
        {
            //Settings.load();
            Settings.LoadFromFile();
            UploadManager target = new UploadManager();
            Mac mac = new Mac(Settings.AccessKey, Settings.SecretKey);
            string key = "test_UploadManagerUploadFile.png";
            string filePath = "F:\\test.png";

            PutPolicy putPolicy = new PutPolicy();
            putPolicy.Scope = Settings.Bucket;
            putPolicy.SetExpires(3600);
            putPolicy.DeleteAfterDays = 1;
            string token = Auth.createUploadToken(putPolicy, mac);
            UploadOptions uploadOptions = null;

            UpCompletionHandler upCompletionHandler = new UpCompletionHandler(delegate (string fileKey, ResponseInfo respInfo, string response)
            {
                Assert.AreEqual(200, respInfo.StatusCode);
            });
            target.uploadFile(filePath, key, token, uploadOptions, upCompletionHandler);
        }
Ejemplo n.º 38
0
        public void UploadFile()
        {
            string AK = "DhR0aOgYjluVsdplOW_A7heVOfKW7PSIoR9aU08_";
            string SK = "T4W_D6urXqUKutAm3G0aHLE9NEF3RvGhepcXqfqJ";
            // 目标空间名
            string bucket = "myfreet";
            // 目标文件名
            string saveKey = "谷歌浏览器书签保存位置.txt";
            // 本地文件
            string localFile = @"C:\Users\z8489\Documents\GitHub\Bookmarks\谷歌浏览器书签保存位置.txt";
            // 上传策略
            PutPolicy putPolicy = new PutPolicy();

            // 设置要上传的目标空间
            putPolicy.Scope = bucket;
            // 上传策略的过期时间(单位:秒)
            putPolicy.SetExpires(3600);
            // 文件上传完毕后,在多少天后自动被删除
            putPolicy.DeleteAfterDays = 1;
            // 请注意这里的Zone设置(如果不设置,就默认为华东机房)
            // var zoneId = Qiniu.Common.AutoZone.Query(AK,BUCKET);
            // Qiniu.Common.Config.ConfigZone(zoneId);
            Mac mac = new Mac(AK, SK); // Use AK & SK here
            // 生成上传凭证
            string        uploadToken   = Auth.createUploadToken(putPolicy, mac);
            UploadOptions uploadOptions = null;
            // 上传完毕事件处理
            UpCompletionHandler uploadCompleted = new UpCompletionHandler(OnUploadCompleted);
            // 方式1:使用UploadManager
            //默认设置 Qiniu.Common.Config.PUT_THRESHOLD = 512*1024;
            //可以适当修改,UploadManager会根据这个阈值自动选择是否使用分片(Resumable)上传
            UploadManager um = new UploadManager();

            um.uploadFile(localFile, saveKey, uploadToken, uploadOptions, uploadCompleted);
            // 方式2:使用FormManager
            //FormUploader fm = new FormUploader();
            //fm.uploadFile(localFile, saveKey, token, uploadOptions, uploadCompleted);
        }
Ejemplo n.º 39
0
        public void uploadDataTest()
        {
            //Settings.load();
            Settings.LoadFromFile("F:\\test.cfg");
            Mac          mac    = new Mac(Settings.AccessKey, Settings.SecretKey);
            FormUploader target = new FormUploader();

            byte[] data = Encoding.UTF8.GetBytes("hello world");
            string key  = "test_FormUploaderUploadData.txt";

            PutPolicy putPolicy = new PutPolicy();

            putPolicy.Scope = Settings.Bucket;
            putPolicy.SetExpires(3600);
            putPolicy.DeleteAfterDays = 1;
            string              token               = Auth.createUploadToken(putPolicy, mac);
            UploadOptions       uploadOptions       = null;
            UpCompletionHandler upCompletionHandler = new UpCompletionHandler(delegate(string fileKey, ResponseInfo respInfo, string response)
            {
                Assert.AreEqual(200, respInfo.StatusCode);
            });

            target.uploadData(data, key, token, uploadOptions, upCompletionHandler);
        }
Ejemplo n.º 40
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
     {
         long fileSize = 0;
         using (FileStream s = new FileStream(filePath, FileMode.Open, FileAccess.Read))
         {
             fileSize = s.Length;
         }
         //判断文件大小,选择上传方式
         if (fileSize <= Config.PUT_THRESHOLD)
         {
             new FormUploader().uploadFile(this.httpManager, filePath, key, token, uploadOptions, upCompletionHandler);
         }
         else
         {
             string recorderKey = null;
             if (this.keyGenerator != null)
             {
                 recorderKey = this.keyGenerator();
             }
             new ResumeUploader(this.httpManager, this.resumeRecorder, recorderKey, filePath, key, token, uploadOptions, upCompletionHandler).uploadFile();
         }
     }
     catch (Exception ex)
     {
         if (upCompletionHandler != null)
         {
             upCompletionHandler(key, ResponseInfo.fileError(ex), null);
         }
     }
 }
Ejemplo n.º 41
0
        private void upload(HttpManager httpManager, PostArgs postArgs, string key, string token,
            UploadOptions uploadOptions, UpCompletionHandler upCompletionHandler)
        {
            if (uploadOptions == null)
            {
                uploadOptions = UploadOptions.defaultOptions();
            }
            postArgs.Params = new Dictionary<string, string>();
            //设置key
            if (!string.IsNullOrEmpty(key))
            {
                postArgs.Params.Add("key", key);
            }
            //设置token
            postArgs.Params.Add("token", token);
            //设置crc32校验
            if (uploadOptions.CheckCrc32)
            {
                switch (httpManager.FileContentType)
                {
                    case PostContentType.BYTES:
                        postArgs.Params.Add("crc32", string.Format("{0}", CRC32.CheckSumBytes(postArgs.Data, postArgs.Data.Length)));
                        break;
                    case PostContentType.STREAM:
                        long streamLength = postArgs.Stream.Length;
                        byte[] buffer = new byte[streamLength];
                        int cnt = postArgs.Stream.Read(buffer, 0, (int)streamLength);
                        postArgs.Params.Add("crc32", string.Format("{0}", CRC32.CheckSumBytes(buffer, cnt)));
                        postArgs.Stream.Seek(0, SeekOrigin.Begin);
                        break;
                    case PostContentType.FILE:
                        postArgs.Params.Add("crc32", string.Format("{0}", CRC32.CheckSumFile(postArgs.File)));
                        break;
                }
            }

            //设置MimeType
            postArgs.MimeType = uploadOptions.MimeType;
            //设置扩展参数
            foreach (KeyValuePair<string, string> kvp in uploadOptions.ExtraParams)
            {
                postArgs.Params.Add(kvp.Key, kvp.Value);
            }
            //设置进度处理和取消信号
            httpManager.ProgressHandler = new ProgressHandler(delegate(int bytesWritten, int totalBytes)
            {
                double percent = (double)bytesWritten / totalBytes;
                //这样做是为了等待回复
                if (percent > 0.95)
                {
                    percent = 0.95;
                }
                uploadOptions.ProgressHandler(key, percent);
            });

            httpManager.CancellationSignal = new CancellationSignal(delegate()
            {
                return uploadOptions.CancellationSignal();
            });
            httpManager.PostArgs = postArgs;
            //第一次失败后使用备用域名重试一次
            httpManager.CompletionHandler = new CompletionHandler(delegate(ResponseInfo respInfo, string response)
            {
                if (respInfo.needRetry())
                {
                    if (httpManager.PostArgs.Stream != null)
                    {
                        httpManager.PostArgs.Stream.Seek(0, SeekOrigin.Begin);
                    }
                    CompletionHandler retried = new CompletionHandler(delegate(ResponseInfo retryRespInfo, string retryResponse)
                    {
                        uploadOptions.ProgressHandler(key, 1.0);

                        if (httpManager.PostArgs.Stream != null)
                        {
                            httpManager.PostArgs.Stream.Close();
                        }

                        if (upCompletionHandler != null)
                        {
                            upCompletionHandler(key, retryRespInfo, retryResponse);
                        }
                    });
                    httpManager.CompletionHandler = retried;
                    httpManager.multipartPost(Config.UP_HOST);
                }
                else
                {
                    uploadOptions.ProgressHandler(key, 1.0);

                    if (httpManager.PostArgs.Stream != null)
                    {
                        httpManager.PostArgs.Stream.Close();
                    }

                    if (upCompletionHandler != null)
                    {
                        upCompletionHandler(key, respInfo, response);
                    }
                }
            });
            httpManager.multipartPost(Config.UPLOAD_HOST);
        }
Ejemplo n.º 42
0
 public void uploadData(byte[] data, int offset, int count, string key, string token,
     UploadOptions uploadOptions, UpCompletionHandler upCompletionHandler)
 {
     HttpFormFile fFile = HttpFormFile.NewFileFromSlice(key, null, data, offset, count);
     upload(fFile, key, token, uploadOptions, upCompletionHandler);
 }
Ejemplo n.º 43
0
 /// <summary>
 /// 上传文件
 /// </summary>
 /// <param name="httpManager">HttpManager对象</param>
 /// <param name="filePath">文件的完整路径</param>
 /// <param name="key">保存在七牛的文件名</param>
 /// <param name="token">上传凭证</param>
 /// <param name="uploadOptions">上传可选设置</param>
 /// <param name="upCompletionHandler">上传完成结果处理器</param>
 public void uploadFile(string filePath, string key,
     string token, UploadOptions uploadOptions, UpCompletionHandler upCompletionHandler)
 {
     HttpFormFile fFile = HttpFormFile.NewFileFromPath(key, null, filePath);
     upload(fFile, key, token, uploadOptions, upCompletionHandler);
 }
Ejemplo n.º 44
0
 /// <summary>
 /// 以表单方式上传数据流
 /// </summary>
 /// <param name="stream">文件数据流</param>
 /// <param name="key">保存在七牛的文件名</param>
 /// <param name="token">上传凭证</param>
 /// <param name="uploadOptions">上传可选设置</param>
 /// <param name="upCompletionHandler">上传完成结果处理器</param>
 public void uploadStream(Stream stream, string key, string token,
     UploadOptions uploadOptions, UpCompletionHandler upCompletionHandler)
 {
     HttpFormFile fFile = HttpFormFile.NewFileFromStream(key, null, stream);
     upload(fFile, key, token, uploadOptions, upCompletionHandler);
 }
Ejemplo n.º 45
0
        private void upload(HttpFormFile fFile, string key, string token,
            UploadOptions uploadOptions, UpCompletionHandler upCompletionHandler)
        {
            string uploadHost = "<UPLOAD_HOST>";
            string uploadHostRetry = "<UPLOAD_HOST_RETRY>";
            if(Config.UploadFromCDN)
            {
                uploadHost = Config.ZONE.UploadHost;
                uploadHostRetry = Config.ZONE.UpHost;
            }
            else
            {
                uploadHost = Config.ZONE.UpHost;
                uploadHostRetry = Config.ZONE.UploadHost;
            }

            if (uploadOptions == null)
            {
                uploadOptions = UploadOptions.defaultOptions();
            }
            Dictionary<string, string> vPostParams = new Dictionary<string, string>();
            //设置key
            if (!string.IsNullOrEmpty(key))
            {
                vPostParams.Add("key", key);
            }
            //设置token
            vPostParams.Add("token", token);
            //设置crc32校验
            if (uploadOptions.CheckCrc32)
            {
                switch (fFile.BodyType)
                {
                    case HttpFileType.DATA_SLICE:
                        vPostParams.Add("crc32", string.Format("{0}", CRC32.CheckSumSlice(fFile.BodyBytes, fFile.Offset, fFile.Count)));
                        break;
                    case HttpFileType.DATA_BYTES:
                        vPostParams.Add("crc32", string.Format("{0}", CRC32.CheckSumBytes(fFile.BodyBytes)));
                        break;
                    case HttpFileType.FILE_STREAM:
                        long streamLength = fFile.BodyStream.Length;
                        byte[] buffer = new byte[streamLength];
                        int cnt = fFile.BodyStream.Read(buffer, 0, (int)streamLength);
                        vPostParams.Add("crc32", string.Format("{0}", CRC32.CheckSumSlice(buffer, 0, cnt)));
                        fFile.BodyStream.Seek(0, SeekOrigin.Begin);
                        break;
                    case HttpFileType.FILE_PATH:
                        vPostParams.Add("crc32", string.Format("{0}", CRC32.CheckSumFile(fFile.BodyFile)));
                        break;
                }
            }

            //设置MimeType
            // FIX: (添加了下一行代码)
            // 修正上传文件MIME总为octect-stream(原因:未初始化FormFile.ContentType)的问题
            // @fengyh 2016-08-17 14:50
            fFile.ContentType = uploadOptions.MimeType;
            //设置扩展参数
            foreach (KeyValuePair<string, string> kvp in uploadOptions.ExtraParams)
            {
                vPostParams.Add(kvp.Key, kvp.Value);
            }
            //设置进度处理和取消信号
            ProgressHandler fUpProgressHandler = new ProgressHandler(delegate (long bytesWritten, long totalBytes)
             {
                 double percent = (double)bytesWritten / totalBytes;
                //这样做是为了等待回复
                if (percent > 0.95)
                 {
                     percent = 0.95;
                 }
                 uploadOptions.ProgressHandler(key, percent);
             });

            CancellationSignal fCancelSignal = new CancellationSignal(delegate ()
             {
                 return uploadOptions.CancellationSignal();
             });

            // 第一次失败后使用备用域名重试一次
            CompletionHandler fUpCompletionHandler = new CompletionHandler(delegate (ResponseInfo respInfo, string response)
            {
                Console.WriteLine("form upload result, {0}",respInfo.StatusCode);

                if (respInfo.needRetry()) // 需要重试
                {
                    Console.WriteLine(string.Format("form upload retry"));

                    if (Config.RetryWaitForNext)
                    {
                        Console.WriteLine(string.Format("wait for {0} milisecond(s)", Config.RETRY_INTERVAL_MILISEC));
                        System.Threading.Thread.Sleep(Config.RETRY_INTERVAL_MILISEC);
                    }

                    if (fFile.BodyStream != null)
                    {
                        fFile.BodyStream.Seek(0, SeekOrigin.Begin);
                    }

                    CompletionHandler retried = new CompletionHandler(delegate (ResponseInfo retryRespInfo, string retryResponse)
                    {
                        Console.WriteLine("form upload retry result, {0}",retryRespInfo.StatusCode);
                        if (respInfo.isOk())
                        {
                            uploadOptions.ProgressHandler(key, 1.0);
                        }

                        if (fFile.BodyStream != null)
                        {
                            fFile.BodyStream.Close();
                        }

                        if (upCompletionHandler != null)
                        {
                            try
                            {
                                upCompletionHandler(key, retryRespInfo, retryResponse);
                            }
                            catch (Exception ex)
                            {
                                Console.WriteLine("form upload retry completion error, {0}", ex.Message);
                            }
                        }
                    });

                    // 使用UPLOAD_HOST_RETRY重试
                    this.mHttpManager.postMultipartDataForm(uploadHostRetry, null, vPostParams, fFile, fUpProgressHandler, retried);
                }
                else // 不需要重试
                {
                    if (respInfo.isOk())
                    {
                        uploadOptions.ProgressHandler(key, 1.0);
                    }

                    if (fFile.BodyStream != null)
                    {
                        fFile.BodyStream.Close();
                    }

                    if (upCompletionHandler != null)
                    {
                        try
                        {
                            upCompletionHandler(key, respInfo, response);
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine("form upload completion error, {0}", ex.Message);
                        }
                    }
                }
            });

            // 使用UPLOAD_HOST上传
            this.mHttpManager.postMultipartDataForm(uploadHost, null, vPostParams, fFile, fUpProgressHandler, fUpCompletionHandler);
        }
Ejemplo n.º 46
0
 /// <summary>
 /// 上传字节数据
 /// </summary>
 /// <param name="data">二进制数据</param>
 /// <param name="key">保存在七牛的文件名</param>
 /// <param name="token">上传凭证</param>
 /// <param name="uploadOptions">上传可选设置</param>
 /// <param name="upCompletionHandler">上传结果处理器</param>
 #region   字节数据
 public void uploadData(byte[] data, string key,
                        string token, UploadOptions uploadOptions, UpCompletionHandler upCompletionHandler)
 {
     new FormUploader().uploadData(data, key, token, uploadOptions, upCompletionHandler);
 }
Ejemplo n.º 47
0
 private CommodityUploadInfo(string uploadToken, UploadOptions uploadOptions, UpCompletionHandler uploadCompleted)
 {
     UploadToken     = uploadToken;
     UploadOptions   = uploadOptions;
     UploadCompleted = uploadCompleted;
 }
Ejemplo n.º 48
0
        private void upload(HttpFormFile fFile, string key, string token,
                            UploadOptions uploadOptions, UpCompletionHandler upCompletionHandler)
        {
            if (uploadOptions == null)
            {
                uploadOptions = UploadOptions.defaultOptions();
            }
            Dictionary <string, string> vPostParams = new Dictionary <string, string>();

            //设置key
            if (!string.IsNullOrEmpty(key))
            {
                vPostParams.Add("key", key);
            }
            //设置token
            vPostParams.Add("token", token);
            //设置crc32校验
            if (uploadOptions.CheckCrc32)
            {
                switch (fFile.BodyType)
                {
                case HttpFileType.DATA_SLICE:
                    vPostParams.Add("crc32", string.Format("{0}", CRC32.CheckSumSlice(fFile.BodyBytes, fFile.Offset, fFile.Count)));
                    break;

                case HttpFileType.DATA_BYTES:
                    vPostParams.Add("crc32", string.Format("{0}", CRC32.CheckSumBytes(fFile.BodyBytes)));
                    break;

                case HttpFileType.FILE_STREAM:
                    long   streamLength = fFile.BodyStream.Length;
                    byte[] buffer       = new byte[streamLength];
                    int    cnt          = fFile.BodyStream.Read(buffer, 0, (int)streamLength);
                    vPostParams.Add("crc32", string.Format("{0}", CRC32.CheckSumSlice(buffer, 0, cnt)));
                    fFile.BodyStream.Seek(0, SeekOrigin.Begin);
                    break;

                case HttpFileType.FILE_PATH:
                    vPostParams.Add("crc32", string.Format("{0}", CRC32.CheckSumFile(fFile.BodyFile)));
                    break;
                }
            }

            //设置MimeType
            // FIX: (添加了下一行代码)
            // 修正上传文件MIME总为octect-stream(原因:未初始化FormFile.ContentType)的问题
            // @fengyh 2016-08-17 14:50
            fFile.ContentType = uploadOptions.MimeType;
            //设置扩展参数
            foreach (KeyValuePair <string, string> kvp in uploadOptions.ExtraParams)
            {
                vPostParams.Add(kvp.Key, kvp.Value);
            }
            //设置进度处理和取消信号
            ProgressHandler fUpProgressHandler = new ProgressHandler(delegate(long bytesWritten, long totalBytes)
            {
                double percent = (double)bytesWritten / totalBytes;
                //这样做是为了等待回复
                if (percent > 0.95)
                {
                    percent = 0.95;
                }
                uploadOptions.ProgressHandler(key, percent);
            });

            CancellationSignal fCancelSignal = new CancellationSignal(delegate()
            {
                return(uploadOptions.CancellationSignal());
            });


            //第一次失败后使用备用域名重试一次
            CompletionHandler fUpCompletionHandler = new CompletionHandler(delegate(ResponseInfo respInfo, string response)
            {
                Console.WriteLine("form upload result, {0}", respInfo.StatusCode);
                if (respInfo.needRetry())
                {
                    if (fFile.BodyStream != null)
                    {
                        fFile.BodyStream.Seek(0, SeekOrigin.Begin);
                    }

                    CompletionHandler retried = new CompletionHandler(delegate(ResponseInfo retryRespInfo, string retryResponse)
                    {
                        Console.WriteLine("form upload retry result, {0}", retryRespInfo.StatusCode);
                        if (respInfo.isOk())
                        {
                            uploadOptions.ProgressHandler(key, 1.0);
                        }

                        if (fFile.BodyStream != null)
                        {
                            fFile.BodyStream.Close();
                        }

                        if (upCompletionHandler != null)
                        {
                            try
                            {
                                upCompletionHandler(key, retryRespInfo, retryResponse);
                            }
                            catch (Exception ex)
                            {
                                Console.WriteLine("form upload retry completion error, {0}", ex.Message);
                            }
                        }
                    });


                    this.mHttpManager.postMultipartDataForm(Config.ZONE.UploadHost, null, vPostParams, fFile, fUpProgressHandler, retried);
                }
                else
                {
                    if (respInfo.isOk())
                    {
                        uploadOptions.ProgressHandler(key, 1.0);
                    }

                    if (fFile.BodyStream != null)
                    {
                        fFile.BodyStream.Close();
                    }

                    if (upCompletionHandler != null)
                    {
                        try
                        {
                            upCompletionHandler(key, respInfo, response);
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine("form upload completion error, {0}", ex.Message);
                        }
                    }
                }
            });

            this.mHttpManager.postMultipartDataForm(Config.ZONE.UploadHost, null, vPostParams, fFile, fUpProgressHandler, fUpCompletionHandler);
        }
Ejemplo n.º 49
0
        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();
            }
        }
Ejemplo n.º 50
0
 public void uploadData(byte[] data, string key,
     string token, UploadOptions uploadOptions, UpCompletionHandler upCompletionHandler)
 {
     new FormUploader().uploadData(data, key, token, uploadOptions, upCompletionHandler);
 }