public void ResumablePutFileTest() { Settings putSetting = new Settings(); // TODO: 初始化为适当的值 string key=NewKey; ResumablePutExtra extra = new ResumablePutExtra(); NameValueCollection nc = new NameValueCollection (); nc.Add("x:username","qiniu"); extra.CallbackParams = nc; extra.Notify += new EventHandler<PutNotifyEvent>(extra_Notify); extra.NotifyErr += new EventHandler<PutNotifyErrorEvent>(extra_NotifyErr); ResumablePut target = new ResumablePut(putSetting, extra); // TODO: 初始化为适当的值 Console.WriteLine ("extra.Bucket:"+Bucket); string upToken = new PutPolicy(Bucket).Token(new Qiniu.Auth.digest.Mac()); TmpFIle file=new TmpFIle(1024*1024*4); target.PutFinished += new EventHandler<CallRet> ((o,e) => { file.Del (); if (e.OK) { RSHelper.RSDel (Bucket, key); } }); CallRet ret =target.PutFile (upToken, file.FileName, key); //Action a = new Action (() => //{ // target.PutFile (upToken, BigFile, NewKey); //}); //a.BeginInvoke (null,null); }
public void ResumablePutFileTest() { Settings putSetting = new Settings(); // TODO: 初始化为适当的值 string key=NewKey; ResumablePutExtra extra = new ResumablePutExtra(); extra.Notify += new EventHandler<PutNotifyEvent>(extra_Notify); extra.NotifyErr += new EventHandler<PutNotifyErrorEvent>(extra_NotifyErr); extra.Bucket = Bucket; ResumablePut target = new ResumablePut(putSetting, extra); // TODO: 初始化为适当的值 Console.WriteLine ("extra.Bucket:"+extra.Bucket); string upToken = new PutPolicy(extra.Bucket).Token(new Qiniu.Auth.digest.Mac()); target.Progress += new Action<float>(target_Progress); TmpFIle file=new TmpFIle(1024*1024*4); target.PutFinished += new EventHandler<CallRet> ((o,e) => { file.Del (); if (e.OK) { RSHelper.RSDel (Bucket, key); } }); target.PutFile (upToken, file.FileName, key); //Action a = new Action (() => //{ // target.PutFile (upToken, BigFile, NewKey); //}); //a.BeginInvoke (null,null); }
public void ResumablePutFile(string bucket, string key, System.IO.Stream stream) { Qiniu.Conf.Config.ACCESS_KEY = "_DN2l8Cb2ZjfajZWFiJ7uLsRgasrqDDlGkkq5bBS"; Qiniu.Conf.Config.SECRET_KEY = "34Wig1rOV0wLnGGQ7E7aUDrogXdT8lDLKDsifA_l"; Qiniu.RS.PutPolicy policy = new Qiniu.RS.PutPolicy(bucket, 3600); string uptoken = policy.Token(); Qiniu.IO.Resumable.Settings setting = new Settings(); ResumablePutExtra extra = new ResumablePutExtra(); ResumablePut client = new ResumablePut(setting, extra); client.PutFile(uptoken, Guid.NewGuid().ToString("N"), stream); }
/// <summary> /// 断点续传 /// </summary> /// <param name="bucket"></param> /// <param name="key"></param> /// <param name="fname"></param> public void ResumablePutFile(string bucket, string key, string fname) { Console.WriteLine("\n===> ResumablePutFile {0}:{1} fname:{2}", bucket, key, fname); PutPolicy policy = new PutPolicy(bucket); string upToken = policy.Token(); Qiniu.IO.Resumable.Settings setting = new Qiniu.IO.Resumable.Settings(); ResumablePutExtra extra = new ResumablePutExtra(); extra.Notify += new EventHandler <PutNotifyEvent>(extra_Notify); //+= PutNotifyEvent(int blkIdx, int blkSize, BlkputRet ret);//上传进度通知事件 ResumablePut client = new ResumablePut(setting, extra); client.PutFile(upToken, fname, key); }
/// <summary> /// </summary> /// <param name="fname"></param> /// <param name="bucket"></param> public string ResumablePutFile(string fname, string bucket = "") { if (bucket == "") { bucket = BUCKET; } //Console.WriteLine("\n===> ResumablePutFile {0}:{1} fname:{2}", bucket, key, fname); var policy = new PutPolicy(bucket, 3600); var upToken = policy.Token(); var setting = new Settings(); var extra = new ResumablePutExtra(); var client = new ResumablePut(setting, extra); var ret = client.PutFile(upToken, fname, Guid.NewGuid().ToString()); if (ret != null) { return ret.Response; } return ""; }
/// <summary> /// 断点续传类 /// </summary> /// <param name="putSetting"></param> /// <param name="extra"></param> public ResumablePut(Settings putSetting, ResumablePutExtra extra) { extra.chunkSize = putSetting.ChunkSize; this.putSetting = putSetting; this.extra = extra; }
/// <summary> /// 断点续上传本地文件 /// </summary> /// <param name="bucket"></param> /// <param name="key"></param> /// <param name="fname"></param> public static void ResumablePutFile(string bucket, string key, string fname) { Console.WriteLine("\n===> ResumablePutFile {0}:{1} fname:{2}", bucket, key, fname); PutPolicy policy = new PutPolicy(bucket, 3600); string upToken = policy.Token(); Settings setting = new Settings(); ResumablePutExtra extra = new ResumablePutExtra(); ResumablePut client = new ResumablePut(setting, extra); client.PutFile(upToken, fname, Guid.NewGuid().ToString()); }
/// <summary> /// 上传图片 /// </summary> /// <param name="fileData">包含图片信息的对象</param> /// <param name="imgKind">照片类型</param> /// <returns></returns> public async static Task<CommonRespMsg> UploadImageAsnyc(ButterFileToUpload fileData, UploadImageKind imgKind,CancellationToken ctoken) { var kindstr = "1:1"; if (fileData.ScaleKind == ScaleKind.Scale_3x4) kindstr = "3:4"; else if (fileData.ScaleKind == ScaleKind.Scale_4x3) kindstr = "4:3"; CommonRespMsg result = new CommonRespMsg(); try { using (var client = new HttpClient()) { Dictionary<string, string> dic = new Dictionary<string, string>(); dic.Add("uid", LocalSettingHelper.GetValue("uid")); dic.Add("access_token", LocalSettingHelper.GetValue("access_token")); dic.Add("appkey", UrlHelper.APP_KEY); dic.Add("img_info", "{\"scale\":\"" + kindstr + "\"}"); dic.Add("ps", fileData.PsData); dic.Add("is_private", fileData.IsPrivate ? "1" : "0"); if (fileData.ActivityID != null) dic.Add("activity_id", fileData.ActivityID); if (fileData.OriginalImageID != -1) dic.Add("ding_imgid", fileData.OriginalImageID.ToString()); var resp = await client.PostAsync(new Uri(imgKind==UploadImageKind.Photo?UrlHelper.UploadImageUrl:UrlHelper.UploadAvatarUrl), new HttpFormUrlEncodedContent(dic)); var json = JsonObject.Parse(await resp.Content.ReadAsStringAsync()); var token = JsonParser.GetStringFromJsonObj(json, "token"); var key = JsonParser.GetStringFromJsonObj(json, "key"); Settings setting = new Settings(); ResumablePutExtra extra = new ResumablePutExtra(); ResumablePut rclient = new ResumablePut(setting, extra); extra.Notify += ((sendern, en) => { }); ctoken.ThrowIfCancellationRequested(); var ioresult = await Task.Run(async () => { ctoken.ThrowIfCancellationRequested(); return await rclient.PutFile(token, fileData.File.Path, key,ctoken); }); if (!ioresult.OK) throw new APIException(int.Parse(ioresult.StatusCode.ToString()), ioresult.Exception.Message); else { return result; } } } catch (APIException) { result.IsSuccessful = false; return result; } catch(OperationCanceledException) { throw; } catch (Exception ee) { var task = ExceptionHelper.WriteRecord(ee, nameof(CloudServices), nameof(UploadImageAsnyc)); var resultMsg = new CommonRespMsg() { IsSuccessful = false, ErrorMsg = ee.Message }; return resultMsg; } }
/// <summary> /// 断点续传类 /// </summary> /// <param name="putSetting"></param> /// <param name="extra"></param> public ResumablePut(Settings putSetting, ResumablePutExtra extra) { this.putSetting = putSetting; this.extra = extra; }