コード例 #1
0
        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();
        }
コード例 #2
0
        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);
        }
コード例 #3
0
ファイル: Demo.cs プロジェクト: jackjet870/cos-dotnet-sdk
        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();
        }
コード例 #4
0
 public string DeleteFile(string file)
 {
     return(_client.DeleteFile(bucketName, RelativePath(file)));
 }
コード例 #5
0
        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 }));
            }
        }