腾讯云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) ...
随机推荐
- JS基本类型-引用类型-深浅拷贝
在JavaScript中变量包含两种类型的值:一种是基本类型,一种是引用类型. 基本类型包括:数值.字符串.null.undefined.布尔值引用类型包括:对象.数组.函数.正则… 补充: null ...
- 【bfs】仙岛求药
[题目描述] 少年李逍遥的婶婶病了,王小虎介绍他去一趟仙灵岛,向仙女姐姐要仙丹救婶婶.叛逆但孝顺的李逍遥闯进了仙灵岛,克服了千险万难来到岛的中心,发现仙药摆在了迷阵的深处.迷阵由M×N个方格组成,有的 ...
- Billiard CFR484 div2 (数论)
就是一个点从开始的点在一个矩形内往某个方向一直运动,如果碰到了矩形的边,那么就反弹,我们可以把这个矩形拓展开,那么就是问题变成了我有没有一个点,这个点的坐标(Tx, Ty)满足n|Tx,m|Ty 那么 ...
- BZOJ3133[Baltic2013]ballmachine
题目描述 https://www.lydsy.com/JudgeOnline/problem.php?id=3133 题解 还是分两个操作来说吧. 先看第一个操作,放球,可以发现,对于祖先节点和后代节 ...
- 阶乘函数(factorial)——结果在整型范围内的阶乘计算
定义: 在数学中,正整数的阶乘(英语:factorial)是所有小于及等于该数的正整数的积,计为n!,例如5的阶乘计为5!,其值为120: \[ 5!=5\times 4\times 3\times ...
- 基于配置文件的redis的主从复制
redis中主从复制有很多种配置方法: 1. 使用配置文件即为redis.conf来配置 在随从redis中配置 # slaveof {masterHost} {MastePort} slaveof ...
- angular2的lazyload初体验
angular2自带了lazyload,就是路由的loadChild,要优化ng2项目必不可少.代码已更新到ng-demo ->https://github.com/chenby/ng2-dem ...
- linux server 产生大量 Too many open files CLOSE_WAIT激增
情景描述:系统产生大量“Too many open files” 原因分析:在服务器与客户端通信过程中,因服务器发生了socket未关导致的closed_wait发生,致使监听port打开的句柄数到了 ...
- A1106. Lowest Price in Supply Chain
A supply chain is a network of retailers(零售商), distributors(经销商), and suppliers(供应商)-- everyone invo ...
- 删除文件夹下面的文件的shell命令
首先看我的文件所在目录 我想删除位于desktop下面的helloBox中的react-hello-dimple中的package.json文件,我们注意一下时间是4月18号 shell命令如下 $ ...