NET7下传图片到七牛云上
NET7下传图片到七牛云上
2023年11月10日记录一下,在自己的商品库项目里用到的,之前的代码没有关闭文件流,导致想在上传到七牛云后再删除本地图片就报错了,删除不了,改了一下,关闭了文件流,然后就可以删除了,记得引用nuget: Qiniu.SDK
//using Qiniu.Util; //using Qiniu.Storage; string AccessKey = "WB-23oGbtMRULmI1n5345320zbdk2O"; string SecretKey = "cJRT2FAUG9nsG25325326nu3x27nz3D"; string Bucket = "niunan-net"; Mac mac = new Mac(AccessKey, SecretKey); PutPolicy putPolicy = new PutPolicy(); putPolicy.Scope = Bucket; putPolicy.SetExpires(3600); string jstr = putPolicy.ToJsonString(); string token = Auth.CreateUploadToken(mac, jstr); Config config = new Config(); config.Zone = Zone.ZoneCnSouth; config.UseHttps = false; config.UseCdnDomains = true; FormUploader fu = new FormUploader(config); Stream s = new System.IO.FileInfo(tpath).OpenRead(); var result = await fu.UploadStream(s, filename_upload, token, null); s.Dispose(); s.Close(); log.Info($"上传图片到七牛云,文件名【{filename_upload}】,上传结果\r\n{result.Text}\r\n\r\n\r\n");
撸码:复制、粘贴,拿起键盘就是“干”!!!