7NiuYun雲存儲UploadPicture

1.七牛云云存儲圖片,上傳、下載。源代碼地址:ssh (git@gitee.com:516877626/QiNiuYunUploadpicture.git) 、https(https://gitee.com/516877626/QiNiuYunUploadpicture.git)

七牛雲sdk地址:https://developer.qiniu.com/kodo/sdk/1237/csharpgit

nuget中安裝七牛雲sdk, Install-Package Qiniujson

 /// <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());
        }
相關文章
相關標籤/搜索