Beispiel #1
0
        /// <summary>
        /// [异步async]上传文件
        /// </summary>
        /// <param name="localFile">本地待上传的文件名</param>
        /// <param name="saveKey">要保存的文件名称</param>
        /// <param name="token">上传凭证</param>
        /// <returns>上传文件后的返回结果</returns>
        public async Task <HttpResult> UploadFileAsync(StorageFile localFile, string saveKey, string token)
        {
            HttpResult result = new HttpResult();

            var fi = await localFile.GetBasicPropertiesAsync();

            if (fi.Size > (ulong)PUT_THRESHOLD)
            {
                if (recordFile == null)
                {
                    string recordKey = ResumeHelper.GetDefaultRecordKey(localFile.Path, saveKey);
                    recordFile = await(await UserEnv.GetHomeFolderAsync()).CreateFileAsync(recordKey, CreationCollisionOption.OpenIfExists);
                }
                if (upph == null)
                {
                    upph = new UploadProgressHandler(ResumableUploader.DefaultUploadProgressHandler);
                }

                if (upctl == null)
                {
                    upctl = new UploadController(ResumableUploader.DefaultUploadController);
                }

                ResumableUploader ru = new ResumableUploader(UPLOAD_FROM_CDN, CHUNK_UNIT);
                result = await ru.UploadFileAsync(localFile, saveKey, token, recordFile, upph, upctl);
            }
            else
            {
                FormUploader fu = new FormUploader(UPLOAD_FROM_CDN);
                result = await fu.UploadFileAsync(localFile, saveKey, token);
            }

            return(result);
        }
Beispiel #2
0
        /// <summary>
        /// [异步async]上传文件
        /// </summary>
        /// <param name="localFile">本地待上传的文件名</param>
        /// <param name="saveKey">要保存的文件名称</param>
        /// <param name="token">上传凭证</param>
        /// <returns>上传文件后的返回结果</returns>
        public async Task <HttpResult> UploadFileAsync(string localFile, string saveKey, string token)
        {
            HttpResult result = new HttpResult();

            var fi = new FileInfo(localFile);

            if (fi.Length > PUT_THRESHOLD)
            {
                if (recordFile == null)
                {
                    string recordKey = ResumeHelper.GetDefaultRecordKey(localFile, saveKey);
                    recordFile = Path.Combine(UserEnv.GetHomeFolder(), recordKey);
                }
                if (upph == null)
                {
                    upph = new UploadProgressHandler(ResumableUploader.DefaultUploadProgressHandler);
                }

                if (upctl == null)
                {
                    upctl = new UploadController(ResumableUploader.DefaultUploadController);
                }

                ResumableUploader ru = new ResumableUploader(UPLOAD_FROM_CDN, CHUNK_UNIT);
                result = await ru.UploadFileAsync(localFile, saveKey, token, recordFile, upph, upctl);
            }
            else
            {
                FormUploader fu = new FormUploader(UPLOAD_FROM_CDN);
                result = await fu.UploadFileAsync(localFile, saveKey, token);
            }

            return(result);
        }
        public async Task UploadFileTest()
        {
            Mac         mac       = new Mac(AccessKey, SecretKey);
            string      key       = FileKey2;
            StorageFile localFile = await StorageFile.GetFileFromPathAsync(LocalFile2);

            PutPolicy putPolicy = new PutPolicy();

            putPolicy.Scope = Bucket1 + ":" + key;
            putPolicy.SetExpires(3600);
            putPolicy.DeleteAfterDays = 1;
            string token = Auth.CreateUploadToken(mac, putPolicy.ToJsonString());

            ResumableUploader target = new ResumableUploader();
            HttpResult        result = await target.UploadFileAsync(localFile, key, token);

            Assert.AreEqual((int)HttpCode.OK, result.Code);
        }