七牛云存储c# SDK

此SDK完成了七牛云存储的中心部份,即文件上传,目标是简化文件上传并供应越发便利的编程接口,从更高层面进行了笼统,而非官方SDK那样,仅是对API的一套封装。

项目地点: https://github.com/icattlecoder/qiniu-csharp-sdk

初始化

初始化事情包含对七牛的API Keys的赋值,如:

qiniu.Config.ACCESS_KEY = "IT9iP3J9wdXXYsT1p8ns0gWD-CQOdLvIQuyE0FOK";
qiniu.Config.SECRET_KEY = "zUCzekBtEqTZ4-WJPCGlBrr2PeyYxsYn98LPaivM";

上传文件

QiniuFile qfile = new QiniuFile ("<input your bucket name>", "<input qiniu file key>", "<local disk file path>");
qfile.Upload();

一个QiniuFile对象示意一个七牛云空间的文件,初始化QiniuFile供应以下三个参数:
bucketName,七牛云空间称号
key,七牛文件key
localfile,当地文件。该参数为可选,假如要上传当地的文件至七牛云空间,则须要指定此参数的值。

注重上传为异步操纵,上传的效果由事宜关照。

上传事宜

共包含五个事宜

事宜称号申明
UploadCompleted;上传完成
UploadFailed;上传失利
UploadProgressChanged;上传进度
UploadBlockCompleted;上传块完成
UploadBlockFailed;上传块失利

前三个事宜比较轻易明白,后两个事宜是依据七牛的大文件上传机制衍生出来的,合理应用这两个事宜能够完成大文件分块上传效果耐久化,从而完成续传。

上传效果

胜利上传一个文件后,效果经由过程事宜uploadCompleted猎取获得,包含文件的HashKey以及从七牛云存储返回的原始字符串(重要考虑到上传凭据中指定了自定义的returnBody)。

续传

QiniuResumbleUploadEx可用于续传,见示例。

文件操纵

简朴的完成了文件的基本信息猎取及删除操纵,分别为StatDelete

完全示例

using System;
using System.Collections;
using System.Collections.Generic;
using qiniu;
using System.Threading;

namespace demo
{
    class MainClass
    {
        public static void Main (string[] args)
        {
            // 初始化qiniu设置,重如果API Keys
            qiniu.Config.ACCESS_KEY = "IT9iP3J9wdXXYsT1p8ns0gWD-CQOdLvIQuyE0FOi";
            qiniu.Config.SECRET_KEY = "zUCzekBtEqTZ4-WJPCGlBrr2PeyYxsYn98LPaivM";

            /**********************************************************************
            能够用下面的要领从设置文件中初始化
            qiniu.Config.InitFromAppConfig ();
            **********************************************************************/

            string localfile = "/Users/icattlecoder/Movies/tzd.rmvb";
            string bucket = "icattlecoder";
            string qiniukey = "tzd.rmvb";

            //======================================================================
            {
                QiniuFile qfile = new QiniuFile (bucket, qiniukey, localfile);

                ResumbleUploadEx puttedCtx = new ResumbleUploadEx (localfile); //续传

                ManualResetEvent done = new ManualResetEvent (false);
                qfile.UploadCompleted += (sender, e) => {
                    Console.WriteLine (e.key);
                    Console.WriteLine (e.Hash);
                    done.Set ();
                };
                qfile.UploadFailed += (sender, e) => {
                    Console.WriteLine (e.Error.ToString ());
                    puttedCtx.Save();
                    done.Set ();
                };
                qfile.UploadProgressChanged += (sender, e) => {
                    int percentage = (int)(100 * e.BytesSent / e.TotalBytes);
                    Console.Write (percentage);
                };
                qfile.UploadBlockCompleted += (sender, e) => {
                    //上传效果耐久化
                    puttedCtx.Add(e.Index,e.Ctx);
                    puttedCtx.Save();
                };
                qfile.UploadBlockFailed += (sender, e) => {
                    //
                };

                //上传为异步操纵
                //上传当地文件到七牛云存储
                qfile.Upload ();

                //假如要续传,挪用下面的要领
                //qfile.Upload (puttedCtx.PuttedCtx);

                done.WaitOne ();
            }

            //======================================================================
            {

                try {
                    QiniuFile qfile = new QiniuFile (bucket, qiniukey);
                    QiniuFileInfo finfo = qfile.Stat ();
                    if (finfo != null) {
                        qfile.Move("cloudcomment","movetest");
                        //删除七牛云空间的文件
                        //qfile.Delete ();
                    }
                } catch (QiniuWebException e) {
                    Console.WriteLine (e.Error.HttpCode);
                    Console.WriteLine (e.Error.ToString ());
                }
            }
        }   
    }   
}

题目反应

https://github.com/icattlecoder/qiniu-csharp-sdk/issues/new

    原文作者:icattlecoder
    原文地址: https://segmentfault.com/a/1190000000458246
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞