博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
7NiuYun云存储UploadPicture
阅读量:5060 次
发布时间:2019-06-12

本文共 8921 字,大约阅读时间需要 29 分钟。

1.七牛云云存储图片,上传、下载。源代码地址:ssh (git@gitee.com:516877626/QiNiuYunUploadpicture.git) 、https(https://gitee.com/516877626/QiNiuYunUploadpicture.git)

七牛云sdk地址:https://developer.qiniu.com/kodo/sdk/1237/csharp

nuget中安装七牛云sdk, Install-Package Qiniu

///         /// 文件分片上传(断点续传)        ///         ///         static void Main1(string[] args)        {            Mac mac = new Mac("6cFdejkPY4zWPHMlPMdKNE7SIaLX_Q9sMoAG1ahy", "BG5JU-fLMLE1DJtcgD5-hC8Dy4WNhoDjc5cWYi21");//(AccessKey, SecretKey);            // 上传文件名            string key = "2";            // 本地文件路径            string filePath = "D:\\2.png";            // 存储空间名            string Bucket = "zszhousepicturebucket";            // 设置上传策略,详见:https://developer.qiniu.com/kodo/manual/1206/put-policy            PutPolicy putPolicy = new PutPolicy();            putPolicy.Scope = Bucket + ":" + key;            putPolicy.SetExpires(3600);            string token = Auth.CreateUploadToken(mac, putPolicy.ToJsonString());            Config config = new Config();            // 设置上传区域            config.Zone = Zone.ZONE_CN_South;            // 设置 http 或者 https 上传            config.UseHttps = true;            config.UseCdnDomains = true;            config.ChunkSize = ChunkUnit.U512K;            ResumableUploader target = new ResumableUploader(config);            PutExtra extra = new PutExtra();            //设置断点续传进度记录文件            extra.ResumeRecordFile = ResumeHelper.GetDefaultRecordKey(filePath, key);            Console.WriteLine("record file:" + extra.ResumeRecordFile);            extra.ResumeRecordFile = "test.progress";            HttpResult result = target.UploadFile(filePath, key, token, extra);            Console.WriteLine("resume upload: " + result.ToString());        }        ///         /// Form 表单上传        ///         ///         static void Main2(string[] args)        {            Mac mac = new Mac("6cFdejkPY4zWPHMlPMdKNE7SIaLX_Q9sMoAG1ahy", "BG5JU-fLMLE1DJtcgD5-hC8Dy4WNhoDjc5cWYi21");//(AccessKey, SecretKey);            // 上传文件名            string key = "2";            // 本地文件路径            string filePath = "D:\\2.png";            // 存储空间名            string Bucket = "zszhousepicturebucket";            // 设置上传策略,详见:https://developer.qiniu.com/kodo/manual/1206/put-policy            PutPolicy putPolicy = new PutPolicy();            putPolicy.Scope = Bucket;            putPolicy.SetExpires(3600);//自定义凭证有效期(示例2小时,expires单位为秒,为上传凭证的有效时间)            putPolicy.DeleteAfterDays = 1;            string token = Auth.CreateUploadToken(mac, putPolicy.ToJsonString());            Config config = new Config            {                // 设置上传区域                Zone = Zone.ZONE_CN_South,                // 设置 http 或者 https 上传                UseHttps = true,                UseCdnDomains = true,                ChunkSize = ChunkUnit.U512K            };            // 表单上传            FormUploader target = new FormUploader(config);            HttpResult result = target.UploadFile(filePath, key, token, null);            Console.WriteLine("form upload result: " + result.ToString());            Console.Read();        }        ///         /// Form 表单上传 自定义返回体        ///         ///         static void Main5(string[] args)        {            Mac mac = new Mac("6cFdejkPY4zWPHMlPMdKNE7SIaLX_Q9sMoAG1ahy", "BG5JU-fLMLE1DJtcgD5-hC8Dy4WNhoDjc5cWYi21");//(AccessKey, SecretKey);            // 上传文件名            string key = "2";            // 本地文件路径            string filePath = "D:\\2.png";            // 存储空间名            string Bucket = "zszhousepicturebucket";            // 设置上传策略,详见:https://developer.qiniu.com/kodo/manual/1206/put-policy            PutPolicy putPolicy = new PutPolicy();            putPolicy.SetExpires(3600);//自定义凭证有效期(示例2小时,expires单位为秒,为上传凭证的有效时间)            putPolicy.Scope = Bucket;          //  putPolicy.CallbackUrl = "http://api.example.com/qiniu/upload/callback";            putPolicy.CallbackBody = "{\"key\":\"$(key)\",\"hash\":\"$(etag)\",\"fsiz\":$(fsize),\"bucket\":\"$(bucket)\",\"name\":\"$(x:name)\"}";            putPolicy.CallbackBodyType = "application/json";            var upToken = Auth.CreateUploadToken(mac, putPolicy.ToJsonString());            Console.WriteLine(upToken);            //则文件上传到七牛之后,收到的回复内容如下:{"key":"qiniu.jpg","hash":"Ftgm-CkWePC9fzMBTRNmPMhGBcSV","bucket":"if-bc","fsize":39335,"name":"qiniu"}            Config config = new Config            {                // 设置上传区域                Zone = Zone.ZONE_CN_South,                // 设置 http 或者 https 上传                UseHttps = true,                UseCdnDomains = true,                ChunkSize = ChunkUnit.U512K            };            // 表单上传            FormUploader target = new FormUploader(config);            HttpResult result = target.UploadFile(filePath, key, upToken, null);            Console.WriteLine("form upload result: " + result.ToString());            Console.Read();        }        ///         /// 下载        ///         ///         static void Main6(string[] args)        {            string domain = "http://p18x1qwno.bkt.clouddn.com";            string key = "2";            string publicUrl = DownloadManager.CreatePublishUrl(domain, key);             Qiniu.Http.HttpResult res =  DownloadManager.Download(publicUrl, "D:\\TestPic\\2.png");            Console.WriteLine(publicUrl);            Console.WriteLine("oko");        }        ///         /// 批量删除图片        ///         static void Main(string[] args) {            // 设置存储区域            Config config = new Config            {                Zone = Zone.ZONE_CN_South            };            Mac mac = new Mac("6cFdejkPY4zWPHMlPMdKNE7SIaLX_Q9sMoAG1ahy", "BG5JU-fLMLE1DJtcgD5-hC8Dy4WNhoDjc5cWYi21");//(AccessKey, SecretKey);            BucketManager bucketManager = new BucketManager(mac, config);            //单个的删除            // 空间名            string Bucket = "zszhousepicturebucket";            // 文件名            string Key = "2";//不用加后缀            HttpResult deleteRet = bucketManager.Delete(Bucket, Key);            Console.WriteLine("delete error: " + deleteRet.ToString());            if (deleteRet.Code != (int)HttpCode.OK)            {                Console.WriteLine("delete error: " + deleteRet.ToString());            }            //批量的删除            ////每个operations的数量不可以超过1000个,如果总数量超过1000,需要分批发送            //// 设置空间            //string Bucket = "zszhousepicturebucket";            //string[] keys = { "2" };//不用加后缀            //List
ops = new List
(); //foreach (string key in keys) //{ // string op = bucketManager.DeleteOp(Bucket, key); // ops.Add(op); //} //BatchResult ret = bucketManager.Batch(ops); //if (ret.Code / 100 != 2) //{ // Console.WriteLine("batch error: " + ret.ToString()); //} //foreach (BatchInfo info in ret.Result) //{ // if (info.Code == (int)HttpCode.OK) // { // Console.WriteLine("delete success"); // } // else // { // Console.WriteLine(info.Data.Error); // } //} Console.Read(); }

asp.net mvc中使用:

public ActionResult Index()        {            Mac mac = new Mac("6cFdejkPY4zWPHMlPMdKNE7SIaLX_Q9sMoAG1ahy", "BG5JU-fLMLE1DJtcgD5-hC8Dy4WNhoDjc5cWYi21");//(AccessKey, SecretKey);            // 上传文件名            string key = "2";            // 本地文件路径            string filePath = "D:\\2.png";            // 存储空间名            string Bucket = "zszhousepicturebucket";            // 设置上传策略,详见:https://developer.qiniu.com/kodo/manual/1206/put-policy            PutPolicy putPolicy = new PutPolicy();            putPolicy.SetExpires(3600);//自定义凭证有效期(示例2小时,expires单位为秒,为上传凭证的有效时间)            putPolicy.Scope = Bucket;            putPolicy.CallbackUrl = "";            putPolicy.CallbackBody = "{\"key\":\"$(key)\",\"hash\":\"$(etag)\",\"fsiz\":$(fsize),\"bucket\":\"$(bucket)\",\"name\":\"$(x:name)\"}";            putPolicy.CallbackBodyType = "application/json";            var upToken = Auth.CreateUploadToken(mac, putPolicy.ToJsonString());            //则文件上传到七牛之后,收到的回复内容如下:{"key":"qiniu.jpg","hash":"Ftgm-CkWePC9fzMBTRNmPMhGBcSV","bucket":"if-bc","fsize":39335,"name":"qiniu"}            Config config = new Config            {                // 设置上传区域                Zone = Zone.ZONE_CN_South,                // 设置 http 或者 https 上传                UseHttps = true,                UseCdnDomains = true,                ChunkSize = ChunkUnit.U512K            };            // 表单上传            FormUploader target = new FormUploader(config);            Qiniu.Http.HttpResult result = target.UploadFile(filePath, key, upToken, null);            Console.WriteLine(result.ToString());            return Content("form upload result: " + result.ToString());        }

 

转载于:https://www.cnblogs.com/Tom-yi/p/8078714.html

你可能感兴趣的文章
[重要更新][Quartus II][14.1正式版]
查看>>
kubeadm安装Kubernetes13.1集群-三
查看>>
整数数组中子数组的最大值
查看>>
通过其轴标签沿 X 轴对齐不同系列中的数据点
查看>>
2019/1/9 6系列所有装置编号与SIM卡信息抓取
查看>>
Git的远程仓库
查看>>
621. Task Scheduler && 358. Rearrange String k Distance Apart
查看>>
数据加密:Stunnel
查看>>
加密解密-C#
查看>>
数据库ACID和mvcc
查看>>
c++11 std::map 通过值查找键
查看>>
Oracle Silent Install 静默安装
查看>>
java实现图像灰度化
查看>>
CI分页类使用入门
查看>>
CS 231n 学习笔记 02——课程2.2 Linear Classification
查看>>
Lucene4.6至 Lucene6.6的每个迭代对API的改动
查看>>
2015 CALLED THE INTERFACE OF 2014
查看>>
flex布局学习笔记
查看>>
Linux FreeTDS的安装与配置
查看>>
使用postgre数据库实现树形结构表的子-父级迭代查询,通过级联菜单简单举例...
查看>>