腾讯云cos封装

public class CosUtil
    {
         int _appId = xxxxx;
         string _secretId = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
         string _secretKey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
         string _bucketName = "test";
        public CosUtil()
        {
            string cosAppId = System.Configuration.ConfigurationSettings.AppSettings.Get("COS_APP_ID");
            if (!string.IsNullOrEmpty(cosAppId) )
            {
                int.TryParse(cosAppId,out _appId) ;
            }
            string cosSecretId = System.Configuration.ConfigurationSettings.AppSettings.Get("COS_SECRET_ID");
            if (!string.IsNullOrEmpty(cosSecretId) )
            {
                _secretId = cosSecretId;
            }
            string cosSecretKey = System.Configuration.ConfigurationSettings.AppSettings.Get("COS_SECRET_KEY");
            if (!string.IsNullOrEmpty(cosSecretKey) )
            {
                _secretKey = cosSecretKey;
            }
            string cosBucketName = System.Configuration.ConfigurationSettings.AppSettings.Get("COS_BUCKET_NAME");
            if (!string.IsNullOrEmpty(cosBucketName))
            {
                _bucketName = cosBucketName;
            }
        }

        public JObject UpLoadFile(string AppName, string filePath)
        {
            var result = "";

            string aFirstName = filePath.Substring(filePath.LastIndexOf("\\") + 1, (filePath.LastIndexOf(".") - filePath.LastIndexOf("\\") - 1)); //文件名
            string aLastName = filePath.Substring(filePath.LastIndexOf(".") + 1, (filePath.Length - filePath.LastIndexOf(".") - 1)); //扩展名
            string newFileName = Guid.NewGuid().ToString();
            string datePath = DateTime.Now.ToString("yyyy-MM-dd");
            string localPath = filePath;
            string remotePath = "/" + AppName + "/" + datePath +"/"+ newFileName +"."+aLastName;
            string folder = "/" + AppName + "/";

            //创建cos对象
            var cos = new CosCloud(_appId, _secretId, _secretKey);

            cos.SetRegion("bj");

            //cos.GetFileStat.GetFolderList("test","\\",)
            //创建文件夹
            result = cos.CreateFolder(_bucketName, folder);
            Console.WriteLine("创建文件目录:" + result);

            //目录更新
            var updateParasDic = new Dictionary<string, string>();
            updateParasDic.Add(CosParameters.PARA_BIZ_ATTR, "new attribute");
            result = cos.UpdateFolder(_bucketName, folder, updateParasDic);
            Console.WriteLine("目录更新:" + result);


            //获取文件夹属性
            result = cos.GetFolderStat(_bucketName, folder);
            Console.WriteLine("查询文件夹属性:" + result);


            //上传文件(不论文件是否分片,均使用本接口)
            Stopwatch sw = new Stopwatch();
            sw.Start();
            var uploadParasDic = new Dictionary<string, string>();
            uploadParasDic.Add(CosParameters.PARA_BIZ_ATTR, "");
            uploadParasDic.Add(CosParameters.PARA_INSERT_ONLY, "0");

            result = cos.UploadFile(_bucketName, remotePath, localPath, uploadParasDic, true, 20);
            sw.Stop();
            Console.WriteLine("上传文件:" + result);
            Console.WriteLine(sw.Elapsed.TotalMilliseconds);

            //设置可选参数
            var optionParasDic = new Dictionary<string, string>();
            optionParasDic.Add(CosParameters.PARA_BIZ_ATTR, "new attribute");
            optionParasDic.Add(CosParameters.PARA_AUTHORITY, AUTHORITY.AUTHORITY_PRIVATEPUBLIC);
            //optionParasDic.Add(CosParameters.PARA_CACHE_CONTROL, "no");
            //optionParasDic.Add(CosParameters.PARA_CONTENT_TYPE, "application/text");
            //optionParasDic.Add(CosParameters.PARA_CONTENT_DISPOSITION, "inline filename=\"QC-7677.pdf\"");
            //optionParasDic.Add(CosParameters.PARA_CONTENT_LANGUAGE, "en");
            //optionParasDic.Add("x-cos-meta-test", "test");

            //更新文件
            result = cos.UpdateFile(_bucketName, remotePath, optionParasDic);
            //Console.WriteLine("更新文件属性" + result);

            //获取文件属性
            result = cos.GetFileStat(_bucketName, remotePath);
            //Console.WriteLine("获取文件属性:" + result);




            JObject jo = (JObject)JsonConvert.DeserializeObject(result);

            return jo;
        }
    }

需要

using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

调用方法

 

JObject  v=new CosUtil().UpLoadFile("GH", @"C:\Users\Administrator\Desktop\新建文本文档.txt");
if (v["code"].ToString() == "0")//上传成功
 {
     string sourceUrl = v["data"]["source_url"].ToString();
     string accessUrl = v["data"]["access_url"].ToString();
     string len = v["data"]["filelen"].ToString();

 }
 else//上传失败
 {
     string message = v["message"].ToString();
 }

 

posted @ 2018-07-26 11:41  zhaogaojian  阅读(440)  评论(0编辑  收藏  举报