{"id":18445,"date":"2025-07-08T12:07:24","date_gmt":"2025-07-08T04:07:24","guid":{"rendered":"https:\/\/blog.ywdevops.cn\/?p=18445"},"modified":"2025-09-19T14:32:11","modified_gmt":"2025-09-19T06:32:11","slug":"golang-4","status":"publish","type":"post","link":"https:\/\/blog.ywdevops.cn\/index.php\/2025\/07\/08\/golang-4\/","title":{"rendered":"golang\u6279\u91cf\u5220\u9664fastdfs\u6587\u4ef6"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">1\u3001\u6839\u636e\u6587\u4ef6\u5b8c\u6574\u94fe\u63a5\u6279\u91cf\u5220\u9664fastdfs\u6587\u4ef6\uff0cgolang\u5185\u5bb9\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport (\n\t\"bufio\"\n\t\"fmt\"\n\t\"log\"\n\t\"os\"\n\t\"os\/exec\"\n\t\"strings\"\n)\n\/\/\u5220\u9664\u6587\u4ef6\nfunc del(fileid string) {\n\tcmd := exec.Command(\"fdfs_delete_file\", \"\/etc\/fdfs\/client.conf\", strings.TrimSpace(fileid))\n\tout, err := cmd.CombinedOutput()\n\tif err != nil {\n\t\tfmt.Println(\"fileid:\", fileid)\n\t\tfmt.Println(\"output:\", string(out))\n\t\tlog.Fatal(\"\u5220\u9664\u6587\u4ef6\u62a5\u9519:\", err)\n\t}\n\tfmt.Println(\"\u5df2\u5220\u9664\u6587\u4ef6:\", fileid)\n}\n\n\/\/ \u68c0\u67e5\u6587\u4ef6\u662f\u5426\u5b58\u5728\nfunc check_file(filename string) {\n\tcmd := exec.Command(\"ls\", filename)\n\tout, _ := cmd.CombinedOutput()\n\tif strings.TrimSpace(string(out)) == filename {\n\t\tfile_id := strings.Replace(string(out), \"\/data\/fastdfs\/storage\/data\", \"group1\/M00\", -1)\n\t\tdel(file_id)\n\t}\n}\nfunc main() {\n\tf, err := os.Open(\"fdfs.txt\")\n\tif err != nil {\n\t\tlog.Fatal(\"\u6253\u5f00\u6587\u4ef6\u62a5\u9519:\", err)\n\t}\n\tdefer f.Close()\n\tscan := bufio.NewScanner(f)\n\tfor scan.Scan() {\n\t\tline := scan.Text()\n\t\tfilename := strings.Replace(line, \"http:\/\/test.com\/group1\/M00\", \"\/data\/fastdfs\/storage\/data\", -1)\n\t\tcheck_file(filename)\n\t}\n}\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">fdfs.txt\u6587\u4ef6\u662f\u8981\u63d0\u524d\u51c6\u5907\u597d\u7684\uff0c\u91cc\u9762\u90fd\u662f\u5b8c\u6574\u7684\u6587\u4ef6\u94fe\u63a5\uff0c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>http:&#47;&#47;test.com\/group1\/M00\/00\/00\/wKgx4GhrKIGALIv1AAXuTr3P82s869.pdf\nhttp:\/\/test.com\/group1\/M00\/00\/00\/wKgx4GhrKIGALIv1AAXuTr3P82s865.pdf\nhttp:\/\/test.com\/group1\/M00\/00\/00\/wKgxumhrL26AEhK8AAXuTr3P82s774.pdf<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">2\u3001\u53bbfastdfs\u91cc\u5bfb\u627e\u6307\u5b9a\u65e5\u671f\u4e4b\u524d\u7684\u6587\u4ef6\u5e76\u5220\u9664\uff0c\u6bd4\u5982\u8981\u5220\u9664100\u5929\u4e4b\u524d\u7684pdf\u6587\u4ef6\uff0c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport (\n\t\"bufio\"\n\t\"fmt\"\n\t\"log\"\n\t\"os\"\n\t\"os\/exec\"\n\t\"strings\"\n)\n\n\/*\n@date: 20250918\n@author: gongguan\n@function: \u5220\u9664fastdfs\u4e2d\u6587\u4ef6\n*\/\nfunc findFdfs(filepath, filetype, filetime string) {\n\tcmd := exec.Command(\"find\", fmt.Sprintf(\"%s\", filepath), \"-type\", \"f\", \"-name\", fmt.Sprintf(\"*%s\", filetype), \"-mtime\", fmt.Sprintf(\"+%s\", filetime))\n\tout, err := cmd.CombinedOutput()\n\tif err != nil {\n\t\tlog.Fatal(\"\u67e5\u627e\u6587\u4ef6\u9519\u8bef:\", err)\n\t}\n\tf, err := os.OpenFile(\"linshi.txt\", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0655)\n\tif err != nil {\n\t\tlog.Fatal(\"\u6253\u5f00linshi.txt\u6587\u4ef6\u9519\u8bef:\", err)\n\t}\n\tdefer f.Close()\n\t_, err = f.Write(out)\n\tif err != nil {\n\t\tlog.Fatal(err)\n\t}\n}\nfunc del(id string) {\n\tcmd := exec.Command(\"fdfs_delete_file\", \"\/etc\/fdfs\/client.conf\", id)\n\t_, err := cmd.CombinedOutput()\n\tif err != nil {\n\t\tfmt.Println(\"\u6587\u4ef6\u540d\u79f0:\", id)\n\t\tlog.Fatal(\"\u5220\u9664\u6587\u4ef6\u62a5\u9519:\", err)\n\t}\n}\nfunc main() {\n    \/\/\u8fd9\u91cc\u63a5\u6536\u4e09\u4e2a\u53c2\u6570\n    \/\/1\u3001\u8981\u5220\u9664\u7684\u6587\u4ef6\u6240\u5728\u76ee\u5f55\n    \/\/2\u3001\u8981\u5220\u9664\u7684\u6587\u4ef6\u540e\u7f00\u540d\n     \/\/3\u3001\u8981\u5220\u9664\u7684\u6587\u4ef6\u65e5\u671f\uff0c\u5982\u679c\u519910\u90a3\u5c31\u662f10\u5929\u524d\n\tfileparam := os.Args&#91;1:]\n\t\/\/\u9996\u5148\u4ece\u5b58\u5904\u7406\u62ff\u5230\u6307\u5b9a\u7684\u540e\u7f00\u540d\u7684\u6587\u4ef6\uff0c\u591a\u5c11\u5929\u4e4b\u524d\u7684\u6587\u4ef6,\u5e76\u4fdd\u5b58\u5230linshi.txt\u4e2d\n\tfindFdfs(fileparam&#91;0], fileparam&#91;1], fileparam&#91;2])\n\t\/\/\u6253\u5f00\u8fd9\u4e2a\u4e34\u65f6\u7684linshi.txt\u6587\u4ef6(\u76ee\u7684\u662f\u8981\u5904\u7406\u5176\u4e2d\u91cd\u590d\u7684\u6587\u4ef6\u8bb0\u5f55)\n\ttempFile, err := os.Open(\".\/linshi.txt\")\n\tif err != nil {\n\t\tlog.Fatal(err)\n\t}\n\tdefer tempFile.Close()\n\tm := make(map&#91;string]int)\n\tscan := bufio.NewScanner(tempFile)\n\tfor scan.Scan() {\n\t\tline := scan.Text()\n\t\tm&#91;line] = 1 \/\/\u5c06\u6587\u4ef6\u5185\u5bb9\u5faa\u73af\u5199\u5165\u5230map\u4e2d(map\u4f1a\u8fc7\u6ee4\u91cd\u590d\u5185\u5bb9)\n\t}\n        \/\/\u521b\u5efa\u4e00\u4e2a\u65b0\u6587\u4ef6fdfs.txt\n\tnewFile, err := os.Create(\"fdfs.txt\")\n\tif err != nil {\n\t\tlog.Fatal(err)\n\t}\n\tdefer newFile.Close()\n        \/\/\u5faa\u73af\u904d\u5386map\uff0c\u5c06\u952e\u5199\u5165\u5230\u6587\u4ef6\u4e2d\uff0c\u952e\u5c31\u662f\u6587\u4ef6\u5185\u5bb9\uff0c\u6b64\u65f6\u5df2\u7ecf\u53bb\u91cd\n\tfor k, _ := range m {\n\t\t_, err := newFile.WriteString(fmt.Sprintf(\"%s\\n\", k))\n\t\tif err != nil {\n\t\t\tlog.Fatal(err)\n\t\t}\n\t}\n\t\/\/\u6253\u5f00fdfs.txt\u6587\u4ef6\n\tfile, err := os.Open(\"fdfs.txt\")\n\tif err != nil {\n\t\tlog.Fatal(\"\u6253\u5f00\u6587\u4ef6\u62a5\u9519:\", err)\n\t}\n\tdefer file.Close()\n\tscan1 := bufio.NewScanner(file)\n\tres := \"\"\n\tfor scan1.Scan() {\n\t\tline := scan1.Text()\n\t\tif strings.HasSuffix(fileparam&#91;0], \"\/\") {\n\t\t\tres = strings.Replace(line, fmt.Sprintf(\"%s\", fileparam&#91;0]), \"group1\/M00\/\", 1)\n\t\t} else {\n\t\t\tres = strings.Replace(line, fmt.Sprintf(\"%s\", fileparam&#91;0]), \"group1\/M00\", 1)\n\t\t}\n\t\tdel(res)\n\t}\n}\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u6700\u540e\u9762\u7684if \u6d41\u7a0b\u63a7\u5236\u7684\u610f\u601d\u5c31\u662f\u5224\u65ad\u4f60\u5728\u8f93\u5165\u7684\u6587\u4ef6\u8def\u5f84\u53c2\u6570\u7684\u65f6\u5019\u540e\u9762\u6709\u6ca1\u6709\u52a0&#8221;\/&#8221;\uff0c\u5982\u679c\u52a0\u4e86\uff0c\u90a3\u4e48\u5728\u66ff\u6362\u7684\u65f6\u5019\u5c31\u8981\u5199\u4e3agroup1\/M00\/\uff0c\u5426\u5219\u5c31\u4f1a\u53d8\u6210group1\/M0000\/06\/&#8230;.\u8fd9\u79cd\uff0c\u5982\u679c\u6ca1\u52a0\uff0c\u5c31\u66ff\u6362\u4e3agroup1\/M00\uff0c\u811a\u672c\u7684\u6267\u884c\u65b9\u5f0f\u53ef\u4ee5\u53c2\u8003\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#\u6bd4\u5982\u5df2\u7ecf\u5c06delfdfs.go\u7f16\u8bd1\u4e3a\u4e8c\u8fdb\u5236\u6587\u4ef6delfdfs\n.\/delfdfs \/data\/fdfs\/storage0\/data pdf 10 #\u8fd9\u91cc\u7684\u610f\u601d\u5c31\u662f\u8bf4\u5220\u9664\/data\/fdfs\/storage0\/data\u8fd9\u4e2a\u8def\u5f84\u4e0b10\u5929\u4ee5\u524d\u7684pdf\u6587\u4ef6 <\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u76f4\u63a5\u5c06\u4e0a\u8ff0\u7f16\u8bd1\u540e\u7684\u4e8c\u8fdb\u5236\u6587\u4ef6\u653e\u5230fastdfs\u73af\u5883\u4e2d\u6267\u884c\u5373\u53ef\uff0c\u5982\u679c\u662f\u901a\u8fc7docker\u8fd0\u884c\u7684fastdfs\uff0c\u90a3\u4e48\u5c31\u9700\u8981copy\u5230\u5bb9\u5668\u4e2d\uff0c\u7136\u540e\u8fd0\u884c<\/p>\n","protected":false},"excerpt":{"rendered":"<p>1\u3001\u6839\u636e\u6587\u4ef6\u5b8c\u6574\u94fe\u63a5\u6279\u91cf\u5220\u9664fastdfs\u6587\u4ef6\uff0cgolang\u5185\u5bb9\u5982\u4e0b\uff1a fdfs.txt\u6587\u4ef6\u662f\u8981\u63d0\u524d\u51c6\u5907\u597d\u7684\uff0c [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":18447,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[34],"tags":[],"class_list":["post-18445","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-linux"],"blocksy_meta":{"styles_descriptor":{"styles":{"desktop":"","tablet":"","mobile":""},"google_fonts":[],"version":8}},"_links":{"self":[{"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/posts\/18445","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/comments?post=18445"}],"version-history":[{"count":6,"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/posts\/18445\/revisions"}],"predecessor-version":[{"id":19100,"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/posts\/18445\/revisions\/19100"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/media\/18447"}],"wp:attachment":[{"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/media?parent=18445"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/categories?post=18445"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/tags?post=18445"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}