public string CreateFolder(string folder) { var result = _client.CreateFolder(bucketName, RelativePath(folder)); if (result.IsNotNullAndWhiteSpace() && result.Contains("resource_path")) { JToken token = JObject.Parse(result); var data = token.SelectToken("data"); return((string)data.SelectToken("resource_path")); } return(null); }
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 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); }
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(); }