简单对七牛.Net API管理文件进行简单封装的类

//封装类代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Qiniu.Conf;
using Qiniu.RS;
using Qiniu.RPC;
using Qiniu.RSF;
using Qiniu.IO;
using Qiniu.IO.Resumable;
using System.IO;
using System.Web;
using System.Drawing.Imaging;
using Qiniu.FileOp;
using System.Net;
//要先引用qiniu的dll文件

namespace ypsuit.common.QiniuHelper
{
    public class QiniuHelper
    {
        /// 
        /// cdnPath为网站服务器本地路径,本地备份一份,再上传到七牛空间一份
        /// 
        private static readonly string cdnPath = DataHelper.GetAbsolutePath(CommonFun.GetConfigData(DataConfigKey.Qiniu.ToString(), DataConfigAttr.CDN_PATH.ToString()));
        /// 
        /// 空间名
        /// 
        public static readonly string bucket = CommonFun.GetConfigData(DataConfigKey.Qiniu.ToString(), DataConfigAttr.BUCKET.ToString());
        /// 
        /// 七牛外域名
        /// 
        public static readonly string Domain = CommonFun.GetConfigData(DataConfigKey.Qiniu.ToString(), DataConfigAttr.DOMAIN.ToString());
        /// 
        /// 在网站/程序初始化时调用一次
        /// 
        public static void Init()
        {
            //设置权限key
            Qiniu.Conf.Config.ACCESS_KEY = CommonFun.GetConfigData(DataConfigKey.Qiniu.ToString(), DataConfigAttr.ACCESS_KEY.ToString());
            //设置密匙key
            Qiniu.Conf.Config.SECRET_KEY = CommonFun.GetConfigData(DataConfigKey.Qiniu.ToString(), DataConfigAttr.SECRET_KEY.ToString());
        }

        #region 查看单个文件属性信息
        /// 
        /// 查看单个文件属性信息
        /// 
        /// 七牛云存储空间名
        /// 文件key
        public static void Stat(string bucket, string key)
        {
            RSClient client = new RSClient();
            Entry entry = client.Stat(new EntryPath(bucket, key));
            if (entry.OK)
            {
                Console.WriteLine("Hash: " + entry.Hash);
                Console.WriteLine("Fsize: " + entry.Fsize);
                Console.WriteLine("PutTime: " + entry.PutTime);
                Console.WriteLine("MimeType: " + entry.MimeType);
                Console.WriteLine("Customer: " + entry.Customer);
            }
            else
            {
                Console.WriteLine("Failed to Stat");
            }
        }
        #endregion

        /// 
        /// 复制单个文件
        /// 
        /// 需要复制的文件所在的空间名
        /// 需要复制的文件key
        /// 目标文件所在的空间名
        /// 标文件key
        public static void Copy(string bucketSrc, string keySrc, string bucketDest, string keyDest)
        {
            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");
            }
        }

        /// 
        /// 移动单个文件
        /// 
        /// 需要移动的文件所在的空间名
        /// 需要移动的文件
        /// 目标文件所在的空间名
        /// 目标文件key
        public static void Move(string bucketSrc, string keySrc, string bucketDest, string keyDest)
        {
            Console.WriteLine("\n===> Move {0}:{1} To {2}:{3}",
            bucketSrc, keySrc, bucketDest, keyDest);
            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");
            }
        }

        /// 
        /// 删除单个文件
        /// 
        /// 文件所在的空间名
        /// 文件key
        public static void Delete(string bucket, string key)
        {
            //Console.WriteLine("\n===> Delete {0}:{1}", bucket, key);
            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");
            //}
        }
        /// 
        /// 批量获取文件信息
        /// 
        /// 
        /// 
        public static void BatchStat(string bucket, string[] keys)
        {
            RSClient client = new RSClient();
            List EntryPaths = new List();
            foreach (string key in keys)
            {
                Console.WriteLine("\n===> Stat {0}:{1}", bucket, key);
                EntryPaths.Add(new EntryPath(bucket, key));
            }
            client.BatchStat(EntryPaths.ToArray());
        }
        /// 
        /// 批量复制
        /// 
        /// 
        /// 
        public static void BatchCopy(string bucket, string[] keys)
        {
            List pairs = new List();
            foreach (string key in keys)
            {
                EntryPathPair entry = new EntryPathPair(bucket, key, Guid.NewGuid().ToString());
                pairs.Add(entry);
            }
            RSClient client = new RSClient();
            client.BatchCopy(pairs.ToArray());
        }

        /// 
        /// 批量移动
        /// 
        /// 
        /// 
        public static void BatchMove(string bucket, string[] keys)
        {
            List pairs = new List();
            foreach (string key in keys)
            {
                EntryPathPair entry = new EntryPathPair(bucket, key, Guid.NewGuid().ToString());
                pairs.Add(entry);
            }
            RSClient client = new RSClient();
            client.BatchMove(pairs.ToArray());
        }
        /// 
        /// 批量删除
        /// 
        /// 
        /// 
        public static void BatchDelete(string bucket, string[] keys)
        {
            RSClient client = new RSClient();
            List EntryPaths = new List();
            foreach (string key in keys)
            {
                //Console.WriteLine("\n===> Stat {0}:{1}", bucket, key);
                EntryPaths.Add(new EntryPath(bucket, key));
            }
            client.BatchDelete(EntryPaths.ToArray());
        }
        /// 
        /// 列出所有文件信息
        /// 
        /// 
        public static List List(int limit = 100, string prefix = "")
        {
            RSFClient rsf = new RSFClient(bucket);
            rsf.Prefix = prefix;
            rsf.Limit = limit;
            List list = new List();
            List items;
            while ((items = rsf.Next()) != null)
            {
                list.AddRange(items);
                //todo
            }
            return list;
        }


        /// 
        /// 普通上传文件
        /// key必须采用utf8编码,如使用非utf8编码访问七牛云存储将反馈错误
        /// 
        /// 
        /// 上传后的路径
        /// 要上传的文件路径(文件必须存在)
        public static PutRet PutFile(string key, string fname)
        {
            if (key.IndexOf('.') == -1) key += Path.GetExtension(fname);
            var policy = new PutPolicy(bucket, 3600);
            string upToken = policy.Token();
            PutExtra extra = new PutExtra();
            IOClient client = new IOClient();
            //client.PutFinished += (o, retTemp) =>
            //{
            //    if (retTemp.StatusCode == HttpStatusCode.OK)
            //    {
            //    }
            //    else if (retTemp.StatusCode == HttpStatusCode.BadGateway ||
            //        retTemp.StatusCode == HttpStatusCode.BadRequest ||
            //        retTemp.StatusCode == HttpStatusCode.GatewayTimeout ||
            //        retTemp.StatusCode == HttpStatusCode.GatewayTimeout ||
            //        retTemp.StatusCode == HttpStatusCode.InternalServerError)
            //    {
            //    }
            //};
            PutRet ret = client.PutFile(upToken, key, fname, extra);

            return ret;
        }
        /// 
        /// 普通上传图片
        /// key必须采用utf8编码,如使用非utf8编码访问七牛云存储将反馈错误
        /// 
        /// 
        /// 上传后的路径
        /// 要上传的文件路径(文件必须存在)
        public static PutRet PutImage(string key, string fname, string mimeType = "image/jpg")
        {
            if (key.IndexOf('.') == -1) key += Path.GetExtension(fname);
            if (mimeType == "") mimeType = GetMimeTypeByExtension(Path.GetExtension(fname));
            var policy = new PutPolicy(bucket, 3600);
            string upToken = policy.Token();
            PutExtra extra = new PutExtra
            {
                MimeType = mimeType,
                Crc32 = 123,
                CheckCrc = CheckCrcType.CHECK,
                Params = new Dictionary()
            };
            IOClient client = new IOClient();
            PutRet ret = client.PutFile(upToken, key, fname, extra);
            return ret;
        }

        /// 
        /// 上传文件到网站服务器,再上传到七牛服务器
        /// 
        /// 页面/swf传过来的文件数据
        /// 预留
        /// 文件加逻辑路径,如:images/ 
        /// 是否保留文件原名
        /// 不保留原名时,文件名的前缀部分
        /// 
        public static PutRet UploadFile(HttpPostedFileBase fileData, string fileType = "", string path = "", string isOriginal = "",string prefix="")
        {
            PutRet ret = null;
            if (fileData != null)
            {

                var fileExt = Path.GetExtension(fileData.FileName);
                var fileName = "";
                if (isOriginal != "1")
                {
                    fileName = DataHelper.CreateRandomName(fileExt);
                    if (prefix != "") fileName = prefix + fileName;
                }
                else fileName = Path.GetFileName(fileData.FileName).Replace(" ", "");

                var fileFullPath = "";
                bool isImage = false;
                string mimeType = GetMimeTypeByExtension(fileExt);
                if (mimeType == "")
                {
                    if (path.Len() == 0) path = "files/";
                    fileFullPath = Path.Combine(path, fileType);
                }
                else if (mimeType.StartsWith("image"))
                {
                    if (path.Len() == 0) path = "images/";
                    fileFullPath = Path.Combine(path, fileType);
                    isImage = true;
                }
                else
                {
                    if (path.Len() == 0) path = "other/";
                    fileFullPath = Path.Combine(path, fileType);
                }
                if (!Directory.Exists(cdnPath + fileFullPath))
                {
                    Directory.CreateDirectory(cdnPath + fileFullPath);
                }
                var fileFullName = cdnPath + fileFullPath + "/" + fileName;
                fileData.SaveAs(fileFullName);

                string key = string.Empty;
                //上传图片到七牛
                if (System.IO.File.Exists(fileFullName))
                {
                    // long len = fileData.ContentLength / 1024;
                    key = fileFullName.Replace(cdnPath, "").Replace("\\", "/").Replace("//", "/");
                    if (isImage) ret = PutImage(key, fileFullName, mimeType);
                    else ret = PutFile(key, fileFullName);
                }
            }
            return ret;
        }

        /// 
        /// 通过后缀获取mime类型
        /// 
        /// 
        /// 
        private static string GetMimeTypeByExtension(string extension)
        {
            string mimeType = "";
            extension = extension.TrimStart('.').ToLowerInvariant();
            switch (extension)
            {
                case "gif":
                    mimeType = "image/gif";
                    break;
                case "png":
                    mimeType = "image/png";
                    break;
                case "bmp":
                    mimeType = "image/bmp";
                    break;
                case "jpeg":
                case "jpg":
                    mimeType = "image/jpg";
                    break;

            }
            return mimeType;
        }

        /// 
        /// 断点续传
        /// 
        /// 
        /// 
        /// 
        public static void ResumablePutFile(string bucket, string key, string fname)
        {
            Console.WriteLine("\n===> ResumablePutFile {0}:{1} fname:{2}", bucket, key, fname);
            PutPolicy policy = new PutPolicy(bucket, 3600);
            string upToken = policy.Token();
            Settings setting = new Settings();
            ResumablePutExtra extra = new ResumablePutExtra();
            // extra.Notify += PutNotifyEvent;//(int blkIdx, int blkSize, BlkputRet ret);//上传进度通知事件
            ResumablePut client = new ResumablePut(setting, extra);
            client.PutFile(upToken, fname, Guid.NewGuid().ToString());
        }

        /// 
        /// 得到文件的外链地址 
        /// 
        /// 
        /// 
        /// 
        public static string GetUrl(string key, bool isPublic = true)
        {
            string baseUrl = GetPolicy.MakeBaseUrl(Domain, key);
            if (isPublic) return baseUrl;
            string private_url = GetPolicy.MakeRequest(baseUrl);
            return private_url;
        }

        /// 
        /// 获取图片信息
        /// 
        /// 
        /// 
        /// 
        public static ImageInfoRet GetImageInfo(string key, bool isPublic = true)
        {
            string url = GetPolicy.MakeBaseUrl(Domain, key);
            //生成fop_url
            string imageInfoURL = ImageInfo.MakeRequest(url);
            //对其签名,生成private_url。如果是公有bucket此步可以省略
            if (!isPublic) imageInfoURL = GetPolicy.MakeRequest(imageInfoURL);
            ImageInfoRet infoRet = ImageInfo.Call(imageInfoURL);
            if (infoRet.OK)
            {
                Console.WriteLine("Format: " + infoRet.Format);
                Console.WriteLine("Width: " + infoRet.Width);
                Console.WriteLine("Heigth: " + infoRet.Height);
                Console.WriteLine("ColorModel: " + infoRet.ColorModel);
            }
            else
            {
                Console.WriteLine("Failed to ImageInfo");
            }
            return infoRet;
        }

        public static string GetImagePreviewUrl(string key, bool isPublic = true)
        {
            string url = GetPolicy.MakeBaseUrl(Domain, key);

            ImageView imageView = new ImageView { Mode = 0, Width = 200, Height = 200, Quality = 90, Format = "jpg" };
            string viewUrl = imageView.MakeRequest(url);
            if (!isPublic) viewUrl = GetPolicy.MakeRequest(viewUrl);//私链
            return viewUrl;
        }

        /// 
        /// 获取水印图片
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        public static string GetWaterMarker(string key, WaterType type = WaterType.No, string waterString = "", bool isPublic = true, string waterColor = "red")
        {
            string url = GetUrl(key);
            string markerUrl = string.Empty;
            switch (type)
            {
                case WaterType.Text:
                    {
                        //文字水印
                        WaterMarker marker = new TextWaterMarker(waterString, "", waterColor);
                        markerUrl = marker.MakeRequest(url);
                    }
                    break;
                case WaterType.Image:
                    {
                        //图片水印
                        WaterMarker marker = new ImageWaterMarker(waterString);
                        markerUrl = marker.MakeRequest(url);
                    }
                    break;
            }
            if (!isPublic && !string.IsNullOrEmpty(markerUrl)) markerUrl = GetPolicy.MakeRequest(markerUrl);
            return markerUrl;


        }
        /// 
        /// 水印类型枚举
        /// 
        public enum WaterType
        {
            /// 
            /// 无水印
            /// 
            No,
            /// 
            /// 文字水印
            /// 
            Text,
            /// 
            /// 图片水印
            /// 
            Image
        }




    }
}


.Net MVC控制器调用上传:

        /// 
        /// 上传文件到七牛服务器
        /// 
        /// 
        /// 目录
        /// 是否保留原名(1:保留,其他不保留)
        /// 
        public ActionResult UploadFile(HttpPostedFileBase fileData, string uploadDir = "", string isOriginal = "")
        {
            Qiniu.IO.PutRet ret = QiniuHelper.UploadFile(fileData, "", uploadDir, isOriginal);
            if (ret != null && ret.StatusCode == HttpStatusCode.OK)
            {
                QiniuFile newFile = new QiniuFile()
                {
                    FileKey = ret.key,
                    Hash = ret.Hash,
                    FSize = fileData.ContentLength,
                    Mime = "application/file",
                    PutTime = DateTime.Now,
                    CreatedById = LoginId
                };
                Contract.Add(newFile);
            }

            return Json(ret);
        }


.NET MVC页面使用 uploadify进行文件上传(uploadify插件我就不详细说了,这里是简单说下使用方法,说太多了就没研究价值了 ):

@model ypsuit.model.QiniuFile
@using System.Web.Mvc
@using System.Web.Mvc.Html
@using ypsuit.common
@{
    string QiniuUrl = CommonFun.GetConfigData(DataConfigKey.QiniuUrl.ToString());
    Layout = null;

}

@using (Ajax.BeginForm("UploadFile", "QiniuFile", new AjaxOptions { HttpMethod = "POST", OnSuccess = "editSuccess" }, new { @class = "form-horizontal", id = "form-create" }))
{
    
}





最后出一张页面图:

简单对七牛.Net API管理文件进行简单封装的类_第1张图片



附:

七牛开发文档中心:http://developer.qiniu.com/docs/v6/index.html

如果大家觉得不错,注册一个,我能得到点好处。

推荐注册地址: https://portal.qiniu.com/signup?code=3lqg1obz73y36


认证成为标准用户后,有很大好处,个人网站一般用不完:

简单对七牛.Net API管理文件进行简单封装的类_第2张图片


简单对七牛.Net API管理文件进行简单封装的类_第3张图片

你可能感兴趣的:(C#/.NET/VS/Linq)