• 腾讯云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:UsersAdministratorDesktop新建文本文档.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();
     }
  • 相关阅读:
    3n+1问题
    判断x的m次方和y的m次方末尾三位数是否相等
    OpenJudge 计算概论1007:点评赛车
    整数划分问题【转】
    证明:平面内有5个整点,必有两个点连线的中点为整点【本资源整理自网络】
    欧几里德算法的证明
    导出本地和远程SVN项目, Export remote SVN repository
    Centos7的firewalld配置
    ESXi5.5下的Centos7虚机配置静态IP
    Dubbo消费端错误: ClassNotFoundException: org.apache.zookeeper.proto.WatcherEvent
  • 原文地址:https://www.cnblogs.com/zhaogaojian/p/9370867.html
Copyright © 2020-2023  润新知