七牛关于C# SDK的各种Demo

具体可以参考七牛PHP SDK的源码以及官网使用文档:
https://github.com/qiniu/csharp-sdk
http://developer.qiniu.com/docs/v6/sdk/csharp-sdk.html

简单上传

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Qiniu.Auth;
using Qiniu.IO;
using Qiniu.IO.Resumable;
using Qiniu.RS;

namespace ConsoleDemo {
    class UploadDemo {

        private void upload()
        {
            //设置账号的AK和SK
            Qiniu.Conf.Config.ACCESS_KEY = "Access_Key";
            Qiniu.Conf.Config.SECRET_KEY = "Secret_Key";
            IOClient target = new IOClient();
            PutExtra extra = new PutExtra();
            //设置上传的空间
            String bucket = "bucket_name";
            //设置上传的文件的key值
            String key = "yourdefinekey";

            //普通上传,只需要设置上传的空间名就可以了,第二个参数可以设定token过期时间
            PutPolicy put = new PutPolicy(bucket, 3600);

            //调用Token()方法生成上传的Token
            string upToken = put.Token();
            //上传文件的路径
            String filePath = "/.../...";

            //调用PutFile()方法上传
            PutRet ret = target.PutFile(upToken, key, filePath, extra);
            //打印出相应的信息
            Console.WriteLine(ret.Response.ToString());
            Console.WriteLine(ret.key);
            Console.ReadLine();
        }

        static void Main(string[] args)
        {
            //实例化UploadDemo对象并调用设置的upload方法
            UploadDemo Upload = new UploadDemo();
            Upload.upload();
        }

    }
}

覆盖上传

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Qiniu.Auth;
using Qiniu.IO;
using Qiniu.IO.Resumable;
using Qiniu.RS;

namespace ConsoleDemo {
    class UploadDemo {

        private void upload()
        {
            //设置账号的AK和SK
            Qiniu.Conf.Config.ACCESS_KEY = "Access_Key";
            Qiniu.Conf.Config.SECRET_KEY = "Secret_Key";
            IOClient target = new IOClient();
            PutExtra extra = new PutExtra();
            //设置上传的空间
            String bucket = "bucket_name";
            //设置上传的文件的key值
            String key = "yourdefinekey";

            //覆盖上传,<bucket>:<key>,表示只允许用户上传指定key的文件。在这种格式下文件默认允许“修改”,已存在同名资源则会被本次覆盖。
            PutPolicy put = new PutPolicy(bucket+":"+key, 3600);

            //设置callbackUrl以及callbackBody,七牛将文件名和文件大小回调给业务服务器
            put.CallBackUrl = "http://your.domain.com/callback";
            put.CallBackBody = "filename=$(fname)&filesize=$(fsize)";

            //调用Token()方法生成上传的Token
            string upToken = put.Token();
            //上传文件的路径
            String filePath = "/.../...";

            //调用PutFile()方法上传
            PutRet ret = target.PutFile(upToken, key, filePath, extra);
            //打印出相应的信息
            Console.WriteLine(ret.Response.ToString());
            Console.WriteLine(ret.key);
            Console.ReadLine();
        }

        static void Main(string[] args)
        {
            //实例化UploadDemo对象并调用设置的upload方法
            UploadDemo Upload = new UploadDemo();
            Upload.upload();
        }

    }
}

上传&回调

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Qiniu.Auth;
using Qiniu.IO;
using Qiniu.IO.Resumable;
using Qiniu.RS;

namespace ConsoleDemo {
    class UploadDemo {

        private void upload()
        {
            //设置账号的AK和SK
            Qiniu.Conf.Config.ACCESS_KEY = "Access_Key";
            Qiniu.Conf.Config.SECRET_KEY = "Secret_Key";
            IOClient target = new IOClient();
            PutExtra extra = new PutExtra();
            //设置上传的空间
            String bucket = "bucket_name";
            //设置上传的文件的key值
            String key = "yourdefinekey";

            //普通上传,只需要设置上传的空间名就可以了,第二个参数可以设定token过期时间
            PutPolicy put = new PutPolicy(bucket, 3600);

            //设置callbackUrl以及callbackBody,七牛将文件名和文件大小回调给业务服务器
            put.CallBackUrl = "http://your.domain.com/callback";
            put.CallBackBody = "filename=$(fname)&filesize=$(fsize)";

            //调用Token()方法生成上传的Token
            string upToken = put.Token();
            //上传文件的路径
            String filePath = "/.../...";

            PutRet ret = target.PutFile(upToken, key, filePath, extra);
            //打印出相应的信息
            Console.WriteLine(ret.Response.ToString());
            Console.WriteLine(ret.key);
            Console.ReadLine();
        }

        static void Main(string[] args)
        {
            //实例化UploadDemo对象并调用设置的upload方法
            UploadDemo Upload = new UploadDemo();
            Upload.upload();
        }

    }
}

上传&预转持续化(以视频转码为例)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Qiniu.Auth;
using Qiniu.IO;
using Qiniu.IO.Resumable;
using Qiniu.RS;

namespace ConsoleDemo
{
    class UploadDemo
    {

        private void upload()
        {
            //设置账号的AK和SK
            Qiniu.Conf.Config.ACCESS_KEY = "Access_Key";
            Qiniu.Conf.Config.SECRET_KEY = "Secret_Key";
            IOClient target = new IOClient();
            PutExtra extra = new PutExtra();
            //设置上传的空间
            String bucket = "bucket_name";
            //设置上传的文件的key值
            String key = "yourdefinekey";

            //设置转码操作参数
            String fops = "avthumb/mp4/s/640x360b/1.25m";
            //设置转码的队列
            String pipeline = "yourpipelinename";

            //可以对转码后的文件进行使用saveas参数自定义命名,当然也可以不指定文件会默认命名并保存在当前空间。
            String urlbase64 = Qiniu.Util.Base64URLSafe.Encode("目标Bucket_Name:自定义文件key");
            String pfops = fops + "|saveas/" + urlbase64;

            //普通上传,只需要设置上传的空间名就可以了,第二个参数可以设定token过期时间
            PutPolicy put = new PutPolicy(bucket, 3600);

            //设置PersistentOps以及PersistentPipeline
            put.PersistentOps = fops;
            put.PersistentPipeline = pipeline;

            //调用Token()方法生成上传的Token
            string upToken = put.Token();
            //上传文件的路径
            String filePath = "/.../...";

            PutRet ret = target.PutFile(upToken, key, filePath, extra);
            //打印出相应的信息
            Console.WriteLine(ret.Response.ToString());
            Console.WriteLine(ret.key);
            Console.ReadLine();
        }

        static void Main(string[] args)
        {
            //实例化UploadDemo对象并调用设置的upload方法
            UploadDemo Upload = new UploadDemo();
            Upload.upload();
        }

    }
}



注:上面的Demo只是针对视频转码,如果需要别的功能比如音视频切片、视频截图、视频拼接只需要修改下上面的fops后面的参数就可以了,
eg: fops = "vframe/jpg/offset/1/w/480/h/360/rotate/90"就表示视频截图了。
下面给出一些常见的数据处理功能,可以根据需要进行选择:
//------------------图片缩放-------------------
fops ="imageView/2/w/200/h/200";

//------------------视频转码-------------------
// fops ="avthumb/flv/vb/229k/vcodec/libx264/noDomain/1";

//------------------图片水印-------------------
String pictureurl = Qiniu.Util.Base64URLSafe.Encode("http://developer.qiniu.com/resource/logo-2.jpg");
fops = "watermark/1/image/" + pictureurl;

//------------------视频切片-------------------
fops = "avthumb/m3u8";
//切片与加密参数
fops = "avthumb/m3u8/vb/640k/hlsKey/MDEyMzQ1Njc4OTEyMzQ1Ng==/hlsKeyUrl/aHR0cDovLzd4bGVrYi5jb20yLnowLmdsYi5xaW5pdWNkbi5jb20vcWluaXV0ZXN0LmtleQ==";

//------------------文档转换-------------------
fops = "yifangyun_preview";

//------------------视频截图-------------------
fops = "vframe/jpg/offset/1/w/480/h/360/rotate/90";

//------------------视频拼接-------------------
//拼接视频片段时要保证所有源的画面长宽值一致
//除去作为数据处理对象的源文件以外,还可以指定最多5个源文件(即总计6个片段)
//所有源文件必须属于同一存储空间
//格式:avconcat/<Mode>/format/<Format>/<encodedUrl0>/<encodedUrl1>/<encodedUrl2>/...
String encodedUrl1 = Qiniu.Util.Base64URLSafe.Encode("http://7xl4c9.com1.z0.glb.clouddn.com/pingjie2.flv");
String encodedUrl2 = Qiniu.Util.Base64URLSafe.Encode("http://7xl4c9.com1.z0.glb.clouddn.com/pingjie3.avi");
fops = "avconcat/2/format/mp4/"+encodedUrl1+encodedUrl2;

//------------------多文件压缩-------------------
//可将若干七牛空间中的资源文件,在七牛服务端压缩后存储
//格式:mkzip/<mode>/url/<Base64EncodedURL>/alias/<Base64EncodedAlias>/url/<Base64EncodedURL>
String encodedfile1 = Qiniu.Util.Base64URLSafe.Encode("http://7xl4c9.com1.z0.glb.clouddn.com/photo1.jpg");
String encodedfile2 = Qiniu.Util.Base64URLSafe.Encode("http://7xl4c9.com1.z0.glb.clouddn.com/vedio1.mp4");
String encodedfile3 = Qiniu.Util.Base64URLSafe.Encode("http://7xl4c9.com1.z0.glb.clouddn.com/audio1.mp3");
fops = "mkzip/2/url/"+encodedfile1+"url/"+encodedfile2+"url/"+encodedfile3;

文件下载

生成下载链接

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Qiniu.RS;

namespace ConsoleDemo {
    class Download {
        public static void download()
        {
            //设置需要操作的账号的AK和SK
            Qiniu.Conf.Config.ACCESS_KEY = "AK";
            Qiniu.Conf.Config.SECRET_KEY = "SK";
            //构造私有空间的需要生成的下载的链接
            string baseUrl = "http://bucketdomain/key";
            //调用MakeRequest方法生成私有下载链接
            string private_url = GetPolicy.MakeRequest(baseUrl);
            Console.WriteLine(private_url);
            Console.ReadLine();
        }

        static void Main(string[] args)
        {
            Download.download();
        }
    }
}

空间资源的管理

获取文件的信息

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Qiniu.RS;
using Qiniu.RPC;

namespace ConsoleDemo {
    class BucketManager {

        public static void Stat(string bucket, string key)
        {   

            //实例化一个RSClient对象,用于操作BucketManager里面的方法
            RSClient client = new RSClient();
            //调用Stat方法获取文件的信息
            Entry entry = client.Stat(new EntryPath(bucket, key));
            if (entry.OK)
            {   
                //打印文件的hash、fsize等信息
                Console.WriteLine("Hash: " + entry.Hash);
                Console.WriteLine("Fsize: " + entry.Fsize);
                Console.WriteLine("PutTime: " + entry.PutTime);
                Console.WriteLine("MimeType: " + entry.MimeType);
                Console.ReadLine();
            }
            else
            {   
                Console.WriteLine("Failed to Stat");
            }
        }

        static void Main(string[] args)
        {
            //初始化AK,SK
            Qiniu.Conf.Config.ACCESS_KEY = "cbe0G6jcr4AeDIdXbpqyZJJKlnHMXDJQpbyJGDIO";
            Qiniu.Conf.Config.SECRET_KEY = "0SR--h8_dpDhfOCbbkWNu_glCnlWCD87NBlhtpIm";
            //要测试的空间和key,并且这个key在你空间中存在
            String bucket = "Bucket_Name";
            String key = "Bucket_key";
            //调用Stat方法
            BucketManager.Stat(bucket,key);

        }
    }
}

移动单个文件

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Qiniu.RS;
using Qiniu.RPC;

namespace ConsoleDemo {
    class BucketManager {

        public static void Move(string bucketSrc, string keySrc, string bucketDest, string keyDest)
        {   

            //实例化一个RSClient对象,用于操作BucketManager里面的方法
            RSClient client = new RSClient();
            new EntryPathPair(bucketSrc, keySrc, bucketDest, keyDest);
            CallRet ret = client.Move(new EntryPathPair(bucketSrc, keySrc, bucketDest, keyDest));
            if (ret.OK)
            {
                Console.WriteLine("Move OK");
            }
            else
            {
                Console.WriteLine("Failed to Move");
            }
        }

        static void Main(string[] args)
        {
            //初始化AK,SK
            Qiniu.Conf.Config.ACCESS_KEY = "cbe0G6jcr4AeDIdXbpqyZJJKlnHMXDJQpbyJGDIO";
            Qiniu.Conf.Config.SECRET_KEY = "0SR--h8_dpDhfOCbbkWNu_glCnlWCD87NBlhtpIm";
            //要测试的空间和key,并且这个key在你空间中存在
            String bucket = "Bucket_Name";
            String key = "Bucket_key";

            //将文件从文件key移动到文件key2, 可以在不同bucket移动,同空间移动相当于重命名
            String key2 = "yourjavakey";
            //调用Move方法
            BucketManager.Move(bucket,key, bucket,key2);

        }
    }
}

复制单个文件

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Qiniu.RS;
using Qiniu.RPC;

namespace ConsoleDemo {
    class BucketManager {

        public static void Copy(string bucketSrc, string keySrc, string bucketDest, string keyDest)
        {
            //实例化一个RSClient对象,用于操作BucketManager里面的方法
            RSClient client = new RSClient();
            CallRet ret = client.Copy(new EntryPathPair(bucketSrc, keySrc, bucketDest, keyDest));
            if (ret.OK)
            {
                Console.WriteLine("Copy OK");
            }
            else
            {
                Console.WriteLine("Failed to Copy");
            }
        }

        static void Main(string[] args)
        {
            //初始化AK,SK
            Qiniu.Conf.Config.ACCESS_KEY = "cbe0G6jcr4AeDIdXbpqyZJJKlnHMXDJQpbyJGDIO";
            Qiniu.Conf.Config.SECRET_KEY = "0SR--h8_dpDhfOCbbkWNu_glCnlWCD87NBlhtpIm";
            //要测试的空间和key,并且这个key在你空间中存在
            String bucket = "Bucket_Name";
            String key = "Bucket_key";

            //将文件从文件key复制到文件key2, 可以在不同bucket移动
            String key2 = "yourjavakey";
            //调用Move方法
            BucketManager.Stat(bucket,key,bucket,key2);

        }
    }
}

删除单个文件

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Qiniu.RS;
using Qiniu.RPC;

namespace ConsoleDemo {
    class BucketManager {


        public static void Delete(string bucket, string key)
        {
            //实例化一个RSClient对象,用于操作BucketManager里面的方法
            RSClient client = new RSClient();
            CallRet ret = client.Delete(new EntryPath(bucket, key));
            if (ret.OK)
            {
                Console.WriteLine("Delete OK");
            }
            else
            {
                Console.WriteLine("Failed to delete");
            }
        }

        static void Main(string[] args)
        {
            //初始化AK,SK
            Qiniu.Conf.Config.ACCESS_KEY = "cbe0G6jcr4AeDIdXbpqyZJJKlnHMXDJQpbyJGDIO";
            Qiniu.Conf.Config.SECRET_KEY = "0SR--h8_dpDhfOCbbkWNu_glCnlWCD87NBlhtpIm";
            //要测试的空间和key,并且这个key在你空间中存在
            String bucket = "Bucket_Name";
            String key = "Bucket_key";
            //调用Delete方法
            BucketManager.Delete(bucket,key);

        }
    }
}

触发持久化操作(以视频转码为例)

视频转码

using Qiniu.RS;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleDemo {
    class Pfop {  

        static void Main(string[] args)
        {
            //初始化AK,SK
            Qiniu.Conf.Config.ACCESS_KEY = "AK";
            Qiniu.Conf.Config.SECRET_KEY = "SK";

            //设置要转码的空间和key,并且这个key在你空间中存在
            String bucket = "Bucket_Name";
            String key = "Bucket_key";

            //实例化一个entry对象
            EntryPath entry = new EntryPath(bucket, key);

            //设置转码操作参数
            String fops = "avthumb/mp4/s/640x360/vb/1.25m";
            //设置转码的队列
            String pipeline = "yourpipelinename";

            //可以对转码后的文件进行使用saveas参数自定义命名,当然也可以不指定文件会默认命名并保存在当前空间。
            String urlbase64 = Qiniu.Util.Base64URLSafe.Encode("保存的空间:保存的key";);
            String pfops = fops + "|saveas/"+urlbase64;

            //实例化一个fop对象主要进行后续转码操作
            Qiniu.RS.Pfop fop = new Qiniu.RS.Pfop();

            string[] fops = new string[] { fo };
            Uri uri = null;

            string s = fop.Do(entry, pfops, uri, pipeline, 1);
            Console.WriteLine(s);
            Console.ReadLine();
        }


    }
}



注:上面的Demo只是针对视频转码,如果需要别的功能比如音视频切片、视频截图、视频拼接只需要修改下上面的fops后面的参数就可以了,
eg: fops = "vframe/jpg/offset/1/w/480/h/360/rotate/90"就表示视频截图了。
下面给出一些常见的数据处理功能,可以根据需要进行选择:
//------------------图片缩放-------------------
fops ="imageView/2/w/200/h/200";

//------------------视频转码-------------------
// fops ="avthumb/flv/vb/229k/vcodec/libx264/noDomain/1";

//------------------图片水印-------------------
String pictureurl = Qiniu.Util.Base64URLSafe.Encode("http://developer.qiniu.com/resource/logo-2.jpg");
fops = "watermark/1/image/" + pictureurl;

//------------------视频切片-------------------
fops = "avthumb/m3u8";
//切片与加密参数
fops = "avthumb/m3u8/vb/640k/hlsKey/MDEyMzQ1Njc4OTEyMzQ1Ng==/hlsKeyUrl/aHR0cDovLzd4bGVrYi5jb20yLnowLmdsYi5xaW5pdWNkbi5jb20vcWluaXV0ZXN0LmtleQ==";

//------------------文档转换-------------------
fops = "yifangyun_preview";

//------------------视频截图-------------------
fops = "vframe/jpg/offset/1/w/480/h/360/rotate/90";

//------------------视频拼接-------------------
//拼接视频片段时要保证所有源的画面长宽值一致
//除去作为数据处理对象的源文件以外,还可以指定最多5个源文件(即总计6个片段)
//所有源文件必须属于同一存储空间
//格式:avconcat/<Mode>/format/<Format>/<encodedUrl0>/<encodedUrl1>/<encodedUrl2>/...
String encodedUrl1 = Qiniu.Util.Base64URLSafe.Encode("http://7xl4c9.com1.z0.glb.clouddn.com/pingjie2.flv");
String encodedUrl2 = Qiniu.Util.Base64URLSafe.Encode("http://7xl4c9.com1.z0.glb.clouddn.com/pingjie3.avi");
fops = "avconcat/2/format/mp4/"+encodedUrl1+encodedUrl2;

//------------------多文件压缩-------------------
//可将若干七牛空间中的资源文件,在七牛服务端压缩后存储
//格式:mkzip/<mode>/url/<Base64EncodedURL>/alias/<Base64EncodedAlias>/url/<Base64EncodedURL>
String encodedfile1 = Qiniu.Util.Base64URLSafe.Encode("http://7xl4c9.com1.z0.glb.clouddn.com/photo1.jpg");
String encodedfile2 = Qiniu.Util.Base64URLSafe.Encode("http://7xl4c9.com1.z0.glb.clouddn.com/vedio1.mp4");
String encodedfile3 = Qiniu.Util.Base64URLSafe.Encode("http://7xl4c9.com1.z0.glb.clouddn.com/audio1.mp3");
fops = "mkzip/2/url/"+encodedfile1+"url/"+encodedfile2+"url/"+encodedfile3;

你可能感兴趣的:(七牛)