Beispiel #1
0
        /// <summary>
        /// [异步async]上传数据流,根据流长度以及设置的阈值(用户初始化UploadManager时可指定该值)自动选择表单或者分片上传
        /// </summary>
        /// <param name="stream">待上传的数据流,要求:流长度(Stream.Length)是确定的</param>
        /// <param name="saveKey">要保存的文件名称</param>
        /// <param name="token">上传凭证</param>
        /// <returns>上传数据后的返回结果</returns>
        public async Task <HttpResult> UploadStreamAsync(Stream stream, string saveKey, string token)
        {
            HttpResult result = new HttpResult();

            if (stream.Length > PUT_THRESHOLD)
            {
                ResumableUploader ru = new ResumableUploader(UPLOAD_FROM_CDN);
                result = await ru.UploadStreamAsync(stream, saveKey, token, sph);
            }
            else
            {
                FormUploader fu = new FormUploader(UPLOAD_FROM_CDN);
                result = await fu.UploadStreamAsync(stream, saveKey, token);
            }

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

            Stream fs = await localFile.OpenStreamForReadAsync();

            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.UploadStreamAsync(fs, key, token, null);

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