腾讯云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() == "")//上传成功
{
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封装的更多相关文章
- 微信小程序/网站 上传图片到腾讯云COS
COS简介: 腾讯云提供的一种对象存储服务,供开发者存储海量文件的分布式存储服务.可以将自己开发的应用的存储部分全部接入COS的存储桶中,有效减少应用服务器的带宽,请求等.个人也可以通过腾讯云账号免费 ...
- 腾讯云COS体验
其实这篇文章本来是推荐COS的,写了一半发现COS的免费额度取消了,2019年之后的开通的用户免费6个月,老用户不受影响,这还让我怎么推荐啊?!写都写了,删掉岂不是白浪费时间? 都怪你!腾讯云! 起因 ...
- 利用腾讯云COS云对象存储定时远程备份网站
版权声明:本文由张戈 原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/942851001487125915 来源:腾云阁 h ...
- Nginx反向代理腾讯云COS的一个坑
版权声明:本文由黄希彤 原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/668639001484812620 来源:腾云 ...
- 直压到亚马逊AWS平台,阿里云OSS平台或者腾讯云COS平台
GTX Compressor (直压上云技术预览版) Powered by GTXLab of Genetalks. 技术预览版本下载地址: https://github.com/Genetalks/ ...
- 搭建markdown图床-腾讯云COS
背景介绍 书写markdown笔记时,如何处理图片,实在是有些棘手的问题.每一张图都保存在当前文件夹? 每张图都自己重命名?每次上传到cnblogs博客都需要一张一张拖动?markdown已经非常成功 ...
- PHP 腾讯云cos使用之我见
因为某些人的原因,本文从新改名发布一遍. 原名称:tp5 -- 腾讯云cos简单使用 原文链接:https://www.cnblogs.com/YFYQ/p/10840050.html 因项目需要,本 ...
- tp5 -- 腾讯云cos简单使用
因项目需要,本来是需要对接阿里云oss,但因客户错误将云存储买成腾讯云cos,因此简单做了个对象上传使用 首先下载cos的sdk: 三种方式在文档上面都有介绍 SDK 安装有三种方式:Composer ...
- 腾讯云COS对象存储的简单使用
叮当哥之前买了一年的腾讯云服务器,昨日偶然发现腾讯云送了叮当哥半年的cos对象存储服务器,于是就撸起袖子传了几张珍藏的高清大图上去,现将其上传的简单使用步骤总结一波(其它操作参加官方SDK文档API) ...
随机推荐
- 【WC2018】通道(边分治,虚树,动态规划)
[WC2018]通道(边分治,虚树,动态规划) 题面 UOJ 洛谷 题解 既然是三棵树,那么显然就是找点什么东西来套个三层. 一棵树怎么做?入门dp. 两棵树?假设在第一棵树中的深度为\(dep\). ...
- HDU1285确定比赛名次
确定比赛名次 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/ ...
- 「SCOI2016」萌萌哒 解题报告
「SCOI2016」萌萌哒 这思路厉害啊.. 容易发现有个暴力是并查集 然后我想了半天线段树优化无果 然后正解是倍增优化并查集 有这个思路就简单了,就是开一个并查集代表每个开头\(i\)每个长\(2^ ...
- pandas to_excel
报错:IllegalCharacterError 其原因是字段中包含了unicode字符. 解决方案: # 首先,安装python包xlsxwriter pip install xlsxwriter ...
- Nginx反向代理后端多节点下故障节点的排除思路
仔细想来,其实是个非常简单的问题:开发和运维觉得两个后端节点跑起来压力太大了,就扩充了两个新的后端节点上去,这一加就出问题了,访问时页面间歇性丢失,这尼玛什么情况...想了半天没思路,查了Nginx的 ...
- 跟angular2学一键开启项目--关于上个react-redux项目的一键调试
一键调试类似于webpack的hot-loader,但是这个hot-loader并不怎么好用,想省事的同学可以配置一下就完了. 今天介绍browser-sync,用它来一键开启项目.它可以监听任意文件 ...
- CodeFroces-- Feel Good
题目大意:给出一段无序数组找出任意 一段区间和*这段区间的最小值 使这个值最大 栈的经典问题 用栈预处理出当前ai 为这块区间最小值的时候 的区间范围(L 和R) #include<bits/s ...
- Learn to securely share files on the blockchain with IPFS!
https://medium.com/@mycoralhealth/learn-to-securely-share-files-on-the-blockchain-with-ipfs-219ee47d ...
- iview 模态框点击确定按钮不消失
<div slot="footer"> <Button type="text" size="large" @click=& ...
- LOJ#2541 猎人杀
解:step1:猎人死了之后不下台,而是继续开枪,这样分母不变...... 然后容斥,枚举猎人集合s,钦定他们在1之后死.定义打到1的时候结束,枚举游戏在i轮时结束. 发现式子是一个1 + x + x ...