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

 /// <summary>
        /// 文件分片上传(断点续传)
        /// </summary>
        /// <param name="args"></param>
        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());
        }
        /// <summary>
        /// Form 表单上传
        /// </summary>
        /// <param name="args"></param>
        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();
        }
        /// <summary>
        /// Form 表单上传 自定义返回体
        /// </summary>
        /// <param name="args"></param>
        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();
        }
        /// <summary>
        /// 下载
        /// </summary>
        /// <param name="args"></param>
        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");
        }


        /// <summary>
        /// 批量删除图片
        /// </summary>
        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<string> ops = new List<string>();
            //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());
        }

 

posted on 2017-12-21 09:56  Hai_yi  阅读(711)  评论(0编辑  收藏  举报