示例#1
0
        /// <summary>
        /// 上传图片到COS
        /// </summary>
        /// <param name="remoteDir">服务器目录</param>
        /// <param name="fileName">服务器文件名,不包括后缀</param>
        /// <param name="localStream">本地文件路径</param>
        /// <returns></returns>
        public UploadFileResponseModel UploadFile(string remoteDir, string fileName, Stream localStream)
        {
            localStream.Seek(0L, SeekOrigin.Begin);
            var result = _cloud.UploadFile(_bucketName, remoteDir + fileName, localStream);

            return(result);
        }
示例#2
0
        public string SaveFile(string file)
        {
            string result = _client.UploadFile(bucketName, RelativePath(file), file);

            if (result.IsNotNullAndWhiteSpace() && result.Contains("access_url"))
            {
                JToken token = JObject.Parse(result);
                var    data  = token.SelectToken("data");
                return((string)data.SelectToken("access_url"));
            }
            return(null);
        }
示例#3
0
        /**
         *
         * @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 = "文件上传失败";
            }
        }
示例#4
0
    public void UploadObj(string remotePath, string localPath)
    {
        var 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();
        print("上传文件:" + result);
    }
示例#5
0
 static void UploadFiles(DirectoryInfo dir)
 {
     foreach (var childDir in dir.GetDirectories())
     {
         UploadFiles(childDir);
     }
     foreach (var file in dir.GetFiles())
     {
         Console.WriteLine(file.FullName);
         string result = _client.UploadFile(bucketName, RelativePath(file.FullName), file.FullName);
         JToken token  = JObject.Parse(result);
         var    data   = token.SelectToken("data");
         Console.WriteLine(data.SelectToken("access_url"));
         Console.WriteLine(result);
         Console.ReadKey();
     }
 }
示例#6
0
文件: TaskAid.cs 项目: ajimidan/blog
        /// <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);
        }
示例#7
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();
        }
示例#8
0
        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();
        }
示例#9
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 }));
            }
        }