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() == "")//上传成功
{
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();
}

腾讯云cos封装的更多相关文章

  1. 微信小程序/网站 上传图片到腾讯云COS

    COS简介: 腾讯云提供的一种对象存储服务,供开发者存储海量文件的分布式存储服务.可以将自己开发的应用的存储部分全部接入COS的存储桶中,有效减少应用服务器的带宽,请求等.个人也可以通过腾讯云账号免费 ...

  2. 腾讯云COS体验

    其实这篇文章本来是推荐COS的,写了一半发现COS的免费额度取消了,2019年之后的开通的用户免费6个月,老用户不受影响,这还让我怎么推荐啊?!写都写了,删掉岂不是白浪费时间? 都怪你!腾讯云! 起因 ...

  3. 利用腾讯云COS云对象存储定时远程备份网站

    版权声明:本文由张戈 原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/942851001487125915 来源:腾云阁 h ...

  4. Nginx反向代理腾讯云COS的一个坑

    版权声明:本文由黄希彤   原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/668639001484812620 来源:腾云 ...

  5. 直压到亚马逊AWS平台,阿里云OSS平台或者腾讯云COS平台

    GTX Compressor (直压上云技术预览版) Powered by GTXLab of Genetalks. 技术预览版本下载地址: https://github.com/Genetalks/ ...

  6. 搭建markdown图床-腾讯云COS

    背景介绍 书写markdown笔记时,如何处理图片,实在是有些棘手的问题.每一张图都保存在当前文件夹? 每张图都自己重命名?每次上传到cnblogs博客都需要一张一张拖动?markdown已经非常成功 ...

  7. PHP 腾讯云cos使用之我见

    因为某些人的原因,本文从新改名发布一遍. 原名称:tp5 -- 腾讯云cos简单使用 原文链接:https://www.cnblogs.com/YFYQ/p/10840050.html 因项目需要,本 ...

  8. tp5 -- 腾讯云cos简单使用

    因项目需要,本来是需要对接阿里云oss,但因客户错误将云存储买成腾讯云cos,因此简单做了个对象上传使用 首先下载cos的sdk: 三种方式在文档上面都有介绍 SDK 安装有三种方式:Composer ...

  9. 腾讯云COS对象存储的简单使用

    叮当哥之前买了一年的腾讯云服务器,昨日偶然发现腾讯云送了叮当哥半年的cos对象存储服务器,于是就撸起袖子传了几张珍藏的高清大图上去,现将其上传的简单使用步骤总结一波(其它操作参加官方SDK文档API) ...

随机推荐

  1. 【BZOJ5197】Gambling Guide (最短路,期望)

    [BZOJ5197]Gambling Guide (最短路,期望) 题面 BZOJ权限题 洛谷 题解 假设我们求出了每个点的期望,那么对于一个点,只有向期望更小的点移动的时候才会更新答案. 即转移是: ...

  2. NOIp2018 复习笔记

    其实也没什么用啦,只是来占个坑 OI知识 3367 [模板]并查集 就这么做啊 没什么其他的 就是可以做tarjan LCA和Kruskal的操作 //关键函数 int getfa(int t) { ...

  3. [NOI2005]聪聪与可可(期望dp)

    题意:给一张无向图,有一只猫和一只老鼠,猫每秒会向老鼠的方向移动两个单位,若它们的距离为一,那么只会移动一个单位,老鼠会等概率向周围移动一步或不动,求猫抓到老鼠的期望时间. Solutionluogu ...

  4. string的基本用法

    #include <iostream> #include<string> #include<vector> #include<algorithm> us ...

  5. Ubuntu下安装Flask虚拟环境及使用

    一.关于Flask介绍 诞生时间:Flask诞生于2010年,是Armin ronacher(人名)用 Python 语言基于 Werkzeug工具箱编写的轻量级Web开发框架. Flask框架包含两 ...

  6. Python中的实例方法、classmethod和staticmethod的区别

    class NewsPaper(object): # 类属性 __print_times = 0 # 下划线表示私有属性 # 实例方法 def __init__(self, title, conten ...

  7. request.getRequestDispatcher 页面跳转,样式丢失。

    在页面中引用样式和其它资源的时候,尽量不要用相对路径,因为"当前路径"这个概念在J2EE中是不稳定的. 所以最好都是绝对路径,类似于: <% String cp = requ ...

  8. ubuntu 百度云

    下载链接:https://pan.baidu.com/s/1HBu5T3PZ8JsS93PgOKnUYw提取密码:6J1A 在终端中输入命令: sudo dpkg -i bcloud_3..1_all ...

  9. 第二十五篇-Android 应用资源

    这里介绍android的一些资源文件. 以一个登录界面为例. layout.xml <?xml version="1.0" encoding="utf-8" ...

  10. R语言实现两文件对应行列字符替换(解决正负链统一的问题)

    假设存在文件file1.xlsx,其内容如下: 存在文件file2.xlsx,其内容如下: 现在我想从第七列开始,将file2所有的字符替换成file1一样的,即第七.八.九.十列不需要改变,因为fi ...