static void Main(string[] args) { try { var result = ""; var bucketName = "r_test"; var cos = new CosCloud(APP_ID, SECRET_ID, SECRET_KEY); var start = DateTime.Now.ToUnixTime(); //result = cos.GetFolderList(bucketName, "/", 20, "", 0, FolderPattern.Both); //result = cos.CreateFolder(bucketName, "/sdk/"); //result = cos.UploadFile(bucketName, "/sdk/xx.txt", @"D:\aa.txt"); //result = cos.UpdateFile(bucketName, "/sdk/xx.txt", "test file"); //result = cos.GetFileStat(bucketName, "/sdk/xx.txt"); //result = cos.UpdateFolder(bucketName, "/sdk/", "test folder"); //result = cos.GetFolderStat(bucketName, "/sdk/"); //result = cos.DeleteFile(bucketName, "/sdk/xx.txt"); //result = cos.DeleteFolder(bucketName, "/sdk/"); result = cos.SliceUploadFile(bucketName, "/红警II共和国之辉(简体中文版).rar", "F:\\红警II共和国之辉(简体中文版).rar", 512 * 1024); var end = DateTime.Now.ToUnixTime(); Console.WriteLine(result); Console.WriteLine("总用时:" + (end - start) + "毫秒"); Console.ReadKey(); } catch (Exception e) { Console.WriteLine(e.Message); } Console.ReadKey(); }
public Storage() { var config = Configuration.GetConfiguration(); bucketName = config.BucketName; _client = new CosCloud(config.AppID, config.SecretId, config.SecretKey); }
private void Awake() { Instance = this; //创建cos对象 cos = new CosCloud(APP_ID, SECRET_ID, SECRET_KEY); cos.SetRegion("bj"); }
static void Main(string[] args) { var config = Configuration.GetConfiguration(); bucketName = config.BucketName; _client = new CosCloud(config.AppID, config.SecretId, config.SecretKey); baseDirectoryInfo = new DirectoryInfo(AppDomain.CurrentDomain.BaseDirectory + "Upload\\"); UploadFiles(baseDirectoryInfo); Console.ReadKey(); }
/** * * @param filepath 图片路径 */ public void uploadImage(string filepath) { loadConfig(); try { var result = ""; //var bucketName = "bucketName"; var cos = new CosCloud(APP_ID, SECRET_ID, SECRET_KEY); var start = DateTime.Now.ToUniversalTime(); string remotePath = Path.GetFileName(filepath); //上传文件(不论文件是否分片,均使用本接口) var uploadParasDic = new Dictionary <string, string>(); uploadParasDic.Add(CosParameters.PARA_BIZ_ATTR, ""); uploadParasDic.Add(CosParameters.PARA_INSERT_ONLY, "0"); //uploadParasDic.Add(CosParameters.PARA_SLICE_SIZE,SLICE_SIZE.SLIZE_SIZE_3M.ToString()); result = cos.UploadFile(BUCKET_NAME, remotePath, filepath, uploadParasDic); Console.WriteLine("上传文件:" + result); var end = DateTime.Now.ToUniversalTime(); Console.WriteLine(result); var obj = (JObject)JsonConvert.DeserializeObject(result); var code = (int)obj["code"]; if (code == 0) { var data = obj["data"]; var downloadUrl = data["source_url"].ToString(); Clipboard.SetDataObject(URL_REGEX.Replace("url", downloadUrl).Replace("http:", "https:")); Console.WriteLine("总用时:" + (end - start) + "毫秒"); toolStripStatusLabel1.Text = "总用时:" + (end - start) + "毫秒"; toolStripStatusLabel1.Text = "文件已经上传成功"; if (keepNotification) { notifyIcon1.ShowBalloonTip(2000, "图床工具提示", "图片上传成功", ToolTipIcon.Info); } } else { MetroMessageBox.Show(this, "发现一点小问题,图像上传失败了。。。。"); toolStripStatusLabel1.Text = "文件上传失败"; } } catch (Exception ex) { MetroMessageBox.Show(this, "发现一点小问题,图像上传失败了。。。。"); toolStripStatusLabel1.Text = "文件上传失败"; } }
/// <summary> /// 备份数据库&上传腾讯云对象存储 /// </summary> public static ActionResultVM BackupDataBase() { var vm = new ActionResultVM(); try { var listMsg = new List <object>(); var kp = "Work:BackupDataBase:SQLServer:"; //执行命令 var cmd = GlobalTo.GetValue(kp + "cmd"); var rt = Core.CmdTo.Shell(cmd); listMsg.Add(rt.ToJObject()); //上传配置 string bucketName = GlobalTo.GetValue(kp + "upload:bucketName"); string remotePath = string.Format(GlobalTo.GetValue(kp + "upload:remotePath"), DateTime.Now.ToString("yyyyMMdd_HHmmss")); string localPath = GlobalTo.GetValue(kp + "upload:localPath"); int APPID = Convert.ToInt32(GlobalTo.GetValue("ApiKey:AccessCOS:APPID")); string SecretId = GlobalTo.GetValue("ApiKey:AccessCOS:SecretId"); string SecretKey = GlobalTo.GetValue("ApiKey:AccessCOS:SecretKey"); //上传 var cos = new CosCloud(APPID, SecretId, SecretKey, UrlType.HB); var cu = cos.UploadFile(bucketName, remotePath, localPath); listMsg.Add(cu.ToJObject()); vm.Set(ARTag.success); vm.data = listMsg; } catch (Exception ex) { vm.Set(ex); } return(vm); }
static void Main(string[] args) { try { var result = ""; const string bucketName = "kitmansh"; const string localPath = @"C:\\testdata\a"; const string remotePath = "/sdktest/a"; const string folder = "/sdktest/"; //创建cos对象 var cos = new CosCloud(APP_ID, SECRET_ID, SECRET_KEY); cos.SetRegion("sh"); //创建文件夹 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); //获取文件属性 result = cos.GetFileStat(bucketName, remotePath); Console.WriteLine("获取文件属性:" + result); //获取文件列表 var foldListParasDic = new Dictionary <string, string>(); foldListParasDic.Add(CosParameters.PARA_NUM, "100"); result = cos.GetFolderList(bucketName, folder, foldListParasDic); Console.WriteLine("获取文件列表:" + result); //设置可选参数 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); //删除文件 result = cos.DeleteFile(bucketName, remotePath); Console.WriteLine("删除文件:" + result); //目录列表 var folderlistParasDic = new Dictionary <string, string>(); folderlistParasDic.Add(CosParameters.PARA_NUM, "100"); folderlistParasDic.Add(CosParameters.PARA_ORDER, "0"); folderlistParasDic.Add(CosParameters.PARA_PATTERN, FolderPattern.PATTERN_BOTH); result = cos.GetFolderList(bucketName, folder, folderlistParasDic); Console.WriteLine("查询目录列表:" + result); //删除文件夹 result = cos.DeleteFolder(bucketName, folder); Console.WriteLine("删除文件夹:" + result); } catch (Exception e) { Console.WriteLine(e.Message); } Console.ReadKey(); }
public void CosTest() { var cos = new CosCloud(AppId, SecretId, SecretKey); //var rs1 = cos.UploadFile(CosBucketName, "/test/ha.png", @"C:\Users\Vic\Desktop\logo.png"); //var rs2 = cos.UploadStream(CosBucketName, "/test/ha.png", "ha.png", @"C:\Users\Vic\Desktop\logo.png".GetFileInfo().ReadToStream()); }
private void SetUpCos() { this.CosLoginInfo = GlobalCache.CosLoginInfo; //创建cos对象 this.CosCloud = new CosCloud(CosLoginInfo.AppID, CosLoginInfo.SecretId, CosLoginInfo.SecretKey); }
public string QQAPI(string bucket, string path) { var cos = new CosCloud(AccessCOS.APPID, AccessCOS.SecretId, AccessCOS.SecretKey, UrlType.HB); string cmd = RouteData.Values["id"]?.ToString().ToLower(); string result = "fail"; switch (cmd) { #region 列表 case "list": try { //关键字搜索 string keywords = Request.Form["keywords"].ToString(); var folderlistParasDic = new Dictionary <string, string> { { CosParameters.PARA_NUM, "1000" }, { CosParameters.PARA_ORDER, "0" }, { CosParameters.PARA_PATTERN, FolderPattern.PATTERN_BOTH }, { CosParameters.PARA_PREFIX, keywords } }; //桶、路径下 result = cos.GetFolderList(bucket, path, folderlistParasDic); result = System.Net.WebUtility.UrlDecode(result); } catch (Exception) { result = "{}"; } break; #endregion #region 存在 case "exists": try { result = "0"; //文件名搜索 string keywords = Request.Form["key"].ToString(); var folderlistParasDic = new Dictionary <string, string> { { CosParameters.PARA_NUM, "1000" }, { CosParameters.PARA_ORDER, "0" }, { CosParameters.PARA_PATTERN, FolderPattern.PATTERN_BOTH }, { CosParameters.PARA_PREFIX, keywords } }; //桶、路径下 result = cos.GetFolderList(bucket, path, folderlistParasDic); JObject jo = JObject.Parse(result); if (jo["code"].ToStringOrEmpty() == "0") { var infos = jo["data"]["infos"]; foreach (var item in infos) { foreach (JProperty jp in item) { if (jp.Name == "name" && jp.Value.ToStringOrEmpty().Equals(keywords)) { result = "1"; goto eachflag; } } } } eachflag : if (result == "1") { result = "1"; } else { result = "0"; } } catch (Exception) { result = "0"; } break; #endregion #region 新建文件夹 case "newfolder": try { path = path.EndsWith("/") ? path : path + "/"; string folder = Request.Form["folder"].ToString(); result = cos.CreateFolder(bucket, path + folder); } catch (Exception) { result = "{}"; } break; #endregion #region 除 case "del": { path = path.EndsWith("/") ? path : path + "/"; //是否都删除成功 bool b1 = false; //是否有部分失败 bool b2 = false; List <string> files = Request.Form["files"].ToString().Split(',').ToList(); List <string> folder = Request.Form["folder"].ToString().Split(',').ToList(); foreach (var item in files) { try { if (!string.IsNullOrWhiteSpace(item)) { JObject jo = jo = JObject.Parse(cos.DeleteFile(bucket, path + item)); if (jo["code"].ToStringOrEmpty() == "0") { b1 = true; } else { b2 = true; } } } catch (Exception) { } } foreach (var item in folder) { try { if (!string.IsNullOrWhiteSpace(item)) { JObject jo = JObject.Parse(cos.DeleteFolder(bucket, path + System.Net.WebUtility.UrlEncode(item))); if (jo["code"].ToStringOrEmpty() == "0") { b1 = true; } else { b2 = true; } } } catch (Exception) { } } if (b2) { result = "partialfail"; } else { if (b1) { result = "success"; } else { result = "fail"; } } } break; #endregion } return(result); }
public QCloudHelper() { _cloud = new CosCloud(QCloudConfig.AppId.ToInt32(), QCloudConfig.SecretId, QCloudConfig.SecretKey); _bucketName = QCloudConfig.BucketName; }
static void Main(string[] args) { try { var result = ""; const string bucketName = "jonnxu5"; const string localPath = "test.txt"; const string remotePath = "/sdktest/test.txt"; const string folder = "/sdktest"; //创建cos对象 var cos = new CosCloud(APP_ID, SECRET_ID, SECRET_KEY); //创建文件夹 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); //上传文件(不论文件是否分片,均使用本接口) var uploadParasDic = new Dictionary <string, string>(); uploadParasDic.Add(CosParameters.PARA_BIZ_ATTR, ""); uploadParasDic.Add(CosParameters.PARA_INSERT_ONLY, "0"); uploadParasDic.Add(CosParameters.PARA_SLICE_SIZE, SLICE_SIZE.SLIZE_SIZE_3M.ToString()); result = cos.UploadFile(bucketName, remotePath, localPath, uploadParasDic); Console.WriteLine(result); //获取文件属性 result = cos.GetFileStat(bucketName, remotePath); Console.WriteLine(result); //获取文件列表 var foldListParasDic = new Dictionary <string, string>(); foldListParasDic.Add(CosParameters.PARA_NUM, "100"); result = cos.GetFolderList(bucketName, folder, foldListParasDic); Console.WriteLine(result); //设置可选参数 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); //目录列表 var folderlistParasDic = new Dictionary <string, string>(); folderlistParasDic.Add(CosParameters.PARA_NUM, "100"); folderlistParasDic.Add(CosParameters.PARA_ORDER, "0"); folderlistParasDic.Add(CosParameters.PARA_PATTERN, FolderPattern.PATTERN_BOTH); result = cos.GetFolderList(bucketName, folder, folderlistParasDic); Console.WriteLine(result); //删除文件 result = cos.DeleteFile(bucketName, remotePath); Console.WriteLine(result); //删除文件夹 result = cos.DeleteFolder(bucketName, folder); Console.WriteLine(result); } catch (Exception e) { Console.WriteLine(e.Message); } Console.ReadKey(); }
public object DownCos([FromUri] string userid, [FromUri] string url) { if (string.IsNullOrWhiteSpace(userid)) { return(Json(new { res = -1 })); } if (string.IsNullOrWhiteSpace(url)) { return(Json(new { res = -1 })); } if (!Utils.IsUrl(url)) { return(Json(new { res = -1 })); } try { WebClient wc = new WebClient(); byte[] ab = wc.DownloadData(url); using (MemoryStream ms = new MemoryStream(ab, 0, ab.Length)) { using (Image returnImage = Image.FromStream(ms)) { string Path = ""; #if DEBUG || P17 Path = @"2"; #endif #if Test Path = @"1"; #endif #if Release Path = @"0"; #endif string tempPath = string.Format(@"{0}\{1}", Utils.ServerPath(@"\img\tmp"), Utils.GenerateNonceStr()); returnImage.Save(tempPath); var tmpName = Utils.SHA1File(tempPath); File.Delete(tempPath); string Sha1Value = string.Empty; string fileName = string.Format(@"{0}.jpg", tmpName); string filePath = string.Format(@"{0}\{1}", Utils.ServerPath(@"\img\" + Path), fileName); string remotePath = string.Format(@"/{0}/{1}", Path, fileName); var result = ""; var bucketName = "logo"; var cos = new CosCloud(); if (File.Exists(filePath)) { Sha1Value = Utils.SHA1File(filePath); } var flag = tmpName == Sha1Value; if (!flag) { //var user = RoleBLL.GetModelByIDList(userid).FirstOrDefault(); //if (user != null) //{ // string delfileName = user.FigureUrl; // log.Info(" DownCos delfileName:" + delfileName); // string delfilePath = string.Format(@"{0}\{1}", Utils.ServerPath(@"\img" + Path), delfileName); // if (File.Exists(delfilePath)) // { // File.Delete(delfilePath); // } // result = cos.GetFileStat(bucketName, delfileName); // log.Info(" DownCos GetFileStat:" + result); // result = cos.DeleteFile(bucketName, delfileName); // log.Info(" DownCos DeleteFile:" + result); //} if (File.Exists(filePath)) { File.Delete(filePath); } //result = cos.GetFileStat(bucketName, remotePath); //if((int)((JObject)JsonConvert.DeserializeObject(result))["code"] == 0) //{ result = cos.DeleteFile(bucketName, remotePath); //} returnImage.Save(filePath); returnImage.Dispose(); result = cos.UploadFile(bucketName, remotePath, filePath); var obj = (JObject)JsonConvert.DeserializeObject(result); var code = (int)obj["code"]; if (code == 0) { //var data = obj["data"]; //var fileId = data["fileid"].ToString(); //var downloadUrl = data["download_url"].ToString(); //result = pic.Query(bucketName, fileId); //result = pic.Copy(bucketName, fileId); //result = pic.Delete(bucketName, fileId); //result = pic.Detection(bucketName, downloadUrl); //Console.WriteLine(result); #if DEBUG return(Json(new { res = 1, fileId = obj })); #endif return(Json(new { res = 1, fileId = remotePath })); } return(Json(new { res = code, fileId = remotePath, error = obj["message"].ToString() })); } returnImage.Dispose(); return(Json(new { res = -18861, fileId = remotePath, error = "图片fileid已经存在" })); } } } catch (Exception ex) { return(Json(new { res = -1, error = ex.Message })); } }