{"id":10020834,"date":"2025-06-18T18:25:51","date_gmt":"2025-06-18T10:25:51","guid":{"rendered":"https:\/\/itmax.cn\/?p=10020834"},"modified":"2025-06-18T18:25:51","modified_gmt":"2025-06-18T10:25:51","slug":"%e9%9d%a2%e8%af%95%e5%ae%98%ef%bc%9a%e4%bd%a0%e6%98%af%e5%89%8d%e7%ab%af%e4%bd%a0%e4%ba%86%e8%a7%a3oss%e5%90%97%ef%bc%9f%e6%88%91%e5%8f%8d%e6%89%8b%e5%86%99%e4%ba%86%e4%b8%80%e4%b8%aareactexpressmin","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10020834","title":{"rendered":"\u9762\u8bd5\u5b98\uff1a\u4f60\u662f\u524d\u7aef\u4f60\u4e86\u89e3oss\u5417\uff1f\u6211\u53cd\u624b\u5199\u4e86\u4e00\u4e2areact+express+minio\u5b9e\u73b0oss\u6587\u4ef6\u5b58\u50a8\u529f\u80fd"},"content":{"rendered":"<blockquote><p>\u6211\u6709\u4e00\u4e2a\u670b\u53cb\uff0c\u524d\u7aef\uff0c\u6700\u8fd1\u5728\u627e\u5de5\u4f5c\uff0c\u9762\u8bd5\u5b98\u5c31\u95ee\u4e86\u4ed6\uff0c\u5bf9oss\u4e86\u89e3\u5417\uff0c\u4ed6\u6ca1\u56de\u7b54\u4e0a\u6765\uff0c\u4e8e\u662f\u5c31\u6709\u4e86\u8fd9\u7bc7\u6587\u7ae0&#8230;<\/p><\/blockquote>\n<h2>\u4ecb\u7ecd<\/h2>\n<h3>\u672c\u6587\u7b80\u4ecb<\/h3>\n<ul>\n<li>\u672c\u6587\u4f7f\u7528react\u5b9e\u73b0\u524d\u7aef\uff0cnode\u7684express\u6846\u67b6\u5b9e\u73b0\u540e\u7aef\uff0c\u642d\u914d\u5f00\u6e90\u7684minio<\/li>\n<li>\u5b9e\u73b0\u4e00\u4e2aoss\u6587\u4ef6\u5b58\u50a8\u670d\u52a1\u529f\u80fd<\/li>\n<li>\u6709\u52a9\u4e8e\u524d\u7aef\u66f4\u597d\u5730\u7406\u89e3\u6587\u4ef6\u5b58\u50a8\u7684\u8fc7\u7a0b<\/li>\n<li>\u5b8c\u6574\u9879\u76ee\u4ee3\u7801\uff1a<a href=\"https:\/\/link.segmentfault.com\/?enc=NZWqARHD5CqCsuuTiSpgRw%3D%3D.mMX16dSqLbtJuEjl%2FUEXlnHKffI0Bw6GpWNOf03i62zi%2FlmTu%2BxkN9Qe2hAlG7iQDiPBHSdWcSRHrAAaxUh4Ug%3D%3D\" rel=\"nofollow noopener noreferrer\" target=\"_blank\">https:\/\/github.com\/shuirongshuifu\/react-node-minio<\/a><\/li>\n<\/ul>\n<h3>\u6548\u679c\u56fe<\/h3>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"723\" height=\"385\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVdjWll\" alt=\"\" title=\"\"><\/p>\n<p>\u529f\u80fd\u6709\uff1a<code>\u4e0a\u4f20\u6587\u4ef6\u3001\u67e5\u8be2\u6587\u4ef6\u5217\u8868\u3001\u5220\u9664\u6587\u4ef6\u3001\u4e0b\u8f7d\u6587\u4ef6<\/code>\u7b49<\/p>\n<h2>minio<\/h2>\n<h3>\u4ec0\u4e48\u662foss<\/h3>\n<ul>\n<li><strong>OSS\uff08Object Storage Service\uff09<\/strong> \uff0c\u4e2d\u6587\u53eb\u00a0<strong>\u5bf9\u8c61\u5b58\u50a8\u670d\u52a1<\/strong>\uff0c\u662f\u4e00\u79cd\u4e13\u95e8\u7528\u6765\u5b58\u50a8\u548c\u7ba1\u7406\u6d77\u91cf\u00a0<strong>\u975e\u7ed3\u6784\u5316\u6570\u636e<\/strong>\uff08\u6bd4\u5982\u56fe\u7247\u3001\u89c6\u9891\u3001\u6587\u6863\u3001\u65e5\u5fd7\u7b49\uff09\u7684\u4e91\u5b58\u50a8\u670d\u52a1\u3002<\/li>\n<li>\u6211\u4eec\u53ef\u4ee5\u628a\u5b83\u60f3\u8c61\u6210\u4e00\u4e2a\u201c\u65e0\u9650\u5bb9\u91cf\u7684\u4e91\u786c\u76d8\u201d\uff0c\u4f46\u5b83\u4e0d\u662f\u50cf\u7535\u8111\u786c\u76d8\u90a3\u6837\u7528\u6587\u4ef6\u5939\u5206\u7c7b\u6587\u4ef6\uff0c\u800c\u662f\u7528\u00a0<strong>\u552f\u4e00\u7684\u6587\u4ef6\u540d\uff08Key\uff09<\/strong> \u00a0\u6765\u5b58\u53d6\u6570\u636e\u3002<\/li>\n<li>OSS \u5b58\u50a8\u7684\u662f\u00a0<strong>\u5bf9\u8c61\uff08Object\uff09<\/strong> \uff0c\u6bcf\u4e2a\u6587\u4ef6\u4f1a\u88ab\u5206\u914d\u4e00\u4e2a\u00a0<strong>\u552f\u4e00\u7684Key<\/strong>\uff08\u6bd4\u5982\u00a0<code>user123\/photo\/2024\/vacation.jpg<\/code>\uff09\uff0c\u6ca1\u6709\u590d\u6742\u7684\u76ee\u5f55\u5c42\u7ea7\u3002<\/li>\n<li>\u80fd\u5b58\u6d77\u91cf\u6570\u636e\uff0c\u5f3a\u7684\u53ef\u6015\uff01<\/li>\n<li>\u5c31\u662f<strong>\u7528\u6237\u4e0a\u4f20\u7684\u7167\u7247\/\u89c6\u9891<\/strong>\u00a0\u2192 \u5b58\u5230\u00a0<strong>OSS<\/strong>\uff08\u8282\u7701\u670d\u52a1\u5668\u7a7a\u95f4\uff0c\u9ad8\u901f\u5206\u53d1\uff09\u3002<\/li>\n<li><strong>APP\u8bbf\u95ee\u8fd9\u4e9b\u56fe\u7247<\/strong>\u00a0\u2192 \u76f4\u63a5\u901a\u8fc7OSS\u7684\u94fe\u63a5\uff08\u5982\u00a0<code>https:\/\/xxx.oss-aliyun.com\/user1\/photo.jpg<\/code>\uff09\u52a0\u8f7d\u3002<\/li>\n<li><strong>\u6211\u4eec\u719f\u77e5\u7684\u767e\u5ea6\u7f51\u76d8\uff0c\u80cc\u540e\u5c31\u662fOSS\u6280\u672f\u54e6<\/strong><\/li>\n<\/ul>\n<h3>\u4ec0\u4e48\u662fminio<\/h3>\n<p>\u901a\u4fd7\u6613\u61c2\u7684\u7406\u89e3\uff1a<\/p>\n<ul>\n<li>\u6211\u4eec\u77e5\u9053\uff0c\u6570\u636e\u8981\u5b58\u50a8\uff0c\u82e5\u662f\u7b80\u5355\u7684\u6570\u636e\uff0c\u6bd4\u5982\u59d3\u540d\u3001\u5e74\u9f84\u3001\u5bb6\u4e61\u7b49\u6570\u636e\u53ef\u4ee5\u5b58\u50a8\u5728\u6570\u636e\u5e93\u4e2d<\/li>\n<li>\u6bd4\u5982<code>MySql\u3001Oracle<\/code>\u7b49\u6570\u636e\u5e93\u4e2d<\/li>\n<li>\u4f46\u662f\uff0c\u65e5\u5e38\u7684\u9700\u6c42\uff0c\u8fd8\u9700\u8981\u5b58\u50a8\u4e00\u4e9b\u8bf8\u5982\u56fe\u7247\u3001\u89c6\u9891\u3001\u97f3\u9891\u7b49\u6587\u4ef6<\/li>\n<li>\u8fd9\u4e2a\u65f6\u5019<code>oss<\/code>\u5c31\u6d3e\u4e0a\u7528\u573a\u4e86\uff08\u4e13\u95e8\u4e3a\u6587\u4ef6\u5b58\u50a8\u800c\u751f\u7684\u670d\u52a1\uff09<\/li>\n<li>oss\u6709\u6536\u8d39\u7684\uff0c\u548c\u5f00\u6e90\u514d\u8d39\u7684<\/li>\n<li>\u6bd4\u5982<code>\u963f\u91cc\u4e91<\/code>\u3001<code>\u817e\u8baf\u4e91<\/code>\u90fd\u63d0\u4f9b\u4e86\u5bf9\u5e94\u7684<code>\u6536\u8d39oss\u670d\u52a1<\/code><\/li>\n<li>\u4f46\u662f\u5c0f\u4f01\u4e1a\u8003\u8651\u5230\u6210\u672c\uff0c\u53ef\u80fd\u4f1a\u9009\u62e9\u5f00\u6e90\u7684\u3001\u514d\u8d39\u7684<code>oss<\/code>\uff0c\u6bd4\u5982\u8bf4<code>minio<\/code><\/li>\n<li><code>\u4e0d\u662f\u963f\u91cc\u4e91\u548c\u817e\u8baf\u4e91\u4e70\u4e0d\u8d77\uff0c\u800c\u662fminio\u66f4\u5177\u6027\u4ef7\u6bd4\ud83d\ude0f\ud83d\ude0f\ud83d\ude0f<\/code><\/li>\n<\/ul>\n<p>\u9ad8\u5927\u4e0a\u7684\u4ecb\u7ecd\uff1a<\/p>\n<ul>\n<li><strong>MinIO<\/strong>\u00a0\u662f\u4e00\u4e2a\u9ad8\u6027\u80fd\u3001\u5f00\u6e90\u7684\u00a0<strong>\u5bf9\u8c61\u5b58\u50a8<\/strong>\u00a0\u89e3\u51b3\u65b9\u6848<\/li>\n<li>\u4e13\u4e3a\u5927\u89c4\u6a21\u6570\u636e\u5b58\u50a8\u548c\u68c0\u7d22\u8bbe\u8ba1\u3002\u5b83\u517c\u5bb9\u00a0<strong>Amazon S3 API<\/strong>\uff0c<\/li>\n<li>\u9002\u5408\u79c1\u6709\u4e91\u3001\u516c\u6709\u4e91\u6216\u6df7\u5408\u4e91\u73af\u5883<\/li>\n<li>\u5e38\u7528\u4e8e\u5b58\u50a8\u975e\u7ed3\u6784\u5316\u6570\u636e\uff08\u5982\u56fe\u7247\u3001\u89c6\u9891\u3001\u65e5\u5fd7\u6587\u4ef6\u7b49\uff09\u3002<\/li>\n<\/ul>\n<h3>\u4e0b\u8f7d\u5b89\u88c5<\/h3>\n<ul>\n<li>\u4e0b\u8f7d\u5730\u5740\uff1a<a href=\"https:\/\/link.segmentfault.com\/?enc=ODoR6www2LbBdDEL0LRUuw%3D%3D.k26U9DxII7D%2FG%2BLds7rmhfrm82WsKhat0UmqfntJnrK%2FlE6u6ROLSuVNLVlCefdFeruj2ohJjHSba1Jss9d1Aw%3D%3D\" rel=\"nofollow noopener noreferrer\" target=\"_blank\">https:\/\/www.minio.org.cn\/download.shtml#\/windows<\/a><\/li>\n<\/ul>\n<p><img decoding=\"async\" width=\"723\" height=\"537\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVdjWlm\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<ul>\n<li>\u4e0b\u8f7d\u597d\u4ee5\u540e\uff0c\u628a\u4e0b\u8f7d\u7684<code>minio.exe<\/code>\u7a0b\u5e8f\uff0c\u653e\u5728\u6587\u4ef6\u5939\u4e0b<\/li>\n<li>\u6bd4\u5982\uff0c\u8fd9\u91cc\u6211\u628a<code>minio.exe<\/code>\u7a0b\u5e8f\uff0c\u653e\u5728\u6211\u7684<code>C\u76d8<\/code>\u4e0b\u7684\uff0c\u65b0\u5efa\u7684<code>minio\u6587\u4ef6\u5939<\/code>\u91cc\u9762<\/li>\n<li>\u7136\u540e\u5728\u5f53\u524d\u76ee\u5f55\u4e0b\uff0c\u4f7f\u7528cmd\u6253\u5f00\u547d\u4ee4\u884c\uff0c\u5e76\u8f93\u5165\u547d\u4ee4<code>minio.exe server C:\/minio\/data<\/code><\/li>\n<li>\u610f\u601d\u662f\uff0c<code>minio<\/code>\u7684\u670d\u52a1\uff0c\u542f\u52a8\u5728<code>C\u76d8\u4e0b\u7684minio\u6587\u4ef6\u5939\u91cc\u9762\uff0c\u6240\u6709\u4e0a\u4f20\u7684\u6587\u4ef6\u5b58\u50a8\u5728\u8fd9\u4e2a\u76ee\u5f55\u7684data\u6587\u4ef6\u5939\u4e2d<\/code><\/li>\n<li>\u4e0d\u9700\u8981\u5728<code>minio<\/code>\u6587\u4ef6\u5939\u4e0b\uff0c\u518d\u521b\u5efa<code>data<\/code>\u6587\u4ef6\u5939\u4e86\uff0c\u4e0a\u8ff0\u547d\u4ee4\u4f1a\u5e2e\u6211\u81ea\u884c\u521b\u5efa<code>data<\/code>\u6587\u4ef6\u5939<\/li>\n<li>\u5982\u4e0b\u56fe\uff1a<\/li>\n<\/ul>\n<p><img decoding=\"async\" width=\"723\" height=\"472\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVdjWlw\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<ul>\n<li>\u5f53\uff0c\u6267\u884c<code>minio.exe server C:\/minio\/data<\/code>\u540e<\/li>\n<li>\u51fa\u73b0\u5982\u4e0b\u56fe\uff0c\u5c31\u8868\u660e\u542f\u52a8\u6210\u529f\u4e86<\/li>\n<\/ul>\n<p><img decoding=\"async\" width=\"723\" height=\"387\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVdjWlx\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<ul>\n<li>\u7136\u540e\uff0c\u5728\u6d4f\u89c8\u5668\u4e2d\uff0c\u8f93\u5165\u672c\u5730ip\u52a0\u7aef\u53e3\uff0c<code>minio\u9ed8\u8ba49000\u7aef\u53e3<\/code><\/li>\n<li><code>http:\/\/127.0.0.1:9000<\/code><\/li>\n<li>\u5c31\u53ef\u4ee5\u8bbf\u95ee\u5230<code>minio<\/code>\u7684<code>\u540e\u53f0UI\u670d\u52a1<\/code>\u4e86<\/li>\n<li>\u6ce8\u610f\uff0c<code>minio<\/code>\u7684\u63a5\u53e3\u7aef\u53e3\u9ed8\u8ba49000\uff0c\u4f46\u662f<code>\u540e\u53f0UI\u670d\u52a1<\/code>\u82e5\u4e0d\u6307\u5b9a\uff0c\u5c31\u4f1a\u968f\u673a\u5206\u914d\u7aef\u53e3<\/li>\n<li>\u8fd9\u91cc\u6211\u4eec\u4e0d\u7528\u523b\u610f\u53bb\u6307\u5b9a\u7aef\u53e3\uff0c\u5f53\u8bbf\u95ee9000\u7aef\u53e3\uff0c<code>minio<\/code>\u4f1a\u81ea\u52a8\u91cd\u5b9a\u5411\u5230<code>\u540e\u53f0UI\u670d\u52a1<\/code>\u7684\u7aef\u53e3\u7684<\/li>\n<li>\u7528\u6237\u540d\u548c\u9ed8\u8ba4\u5bc6\u7801\u90fd\u662f<code>minioadmin<\/code><\/li>\n<li>\u5982\u4e0b\u56fe\uff1a<\/li>\n<\/ul>\n<p><img decoding=\"async\" width=\"723\" height=\"388\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVdjWly\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<ul>\n<li>\u767b\u5f55\u4ee5\u540e\uff0c\u5982\u4e0b\u56fe\uff0c\u70b9\u51fb<code>Buckets<\/code>\u53bb\u521b\u5efa\u6876<\/li>\n<\/ul>\n<p><img decoding=\"async\" width=\"723\" height=\"409\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVdjWlz\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<ul>\n<li>\u628a\u6876\u8bbe\u7f6e\u4e3a\u516c\u5f00\u7684\uff0c<code>Public<\/code>\uff0c\u65b9\u4fbf\u6211\u4eec\u63a5\u53e3\u8bbf\u95ee<\/li>\n<\/ul>\n<p><img decoding=\"async\" width=\"723\" height=\"384\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVdjWlA\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<ul>\n<li>\u5230\u8fd9\u4e00\u6b65\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u901a\u8fc7\u63a5\u53e3\u8bbf\u95ee\u4e86<\/li>\n<\/ul>\n<h2>\u540e\u7aefExpress\u670d\u52a1<\/h2>\n<ul>\n<li>\u8fd9\u91cc\u7684\u6d41\u7a0b\uff0c\u5c31\u662f\u524d\u7aef\u4e0a\u4f20\u6587\u4ef6\uff0c\u8c03\u540e\u7aef\u63a5\u53e3<\/li>\n<li>\u540e\u7aef\u8c03\u7528minio\u7684\u670d\u52a1\uff0c\u628a\u524d\u7aef\u7ed9\u5230\u7684\u6587\u4ef6\u5b58\u5230minio\u4e2d<\/li>\n<li>\u5b58\u50a8\u6210\u529f\u4ee5\u540e\uff0c\u518d\u544a\u8bc9\u524d\u7aef\uff0c\u63a5\u53e3200\uff0c\u6ca1\u95ee\u9898\u4e86<\/li>\n<\/ul>\n<h3>\u9700\u8981\u7528\u5230\u7684\u5305<\/h3>\n<pre><code class=\"js\">{\n  &quot;dependencies&quot;: {\n    &quot;body-parser&quot;: &quot;^1.20.3&quot;, \/\/ \u89e3\u6790\u524d\u7aef\u8bf7\u6c42\u4f53\u4e2d\u5e26\u8fc7\u6765\u7684\u53c2\u6570\n    &quot;cors&quot;: &quot;^2.8.5&quot;, \/\/ \u653e\u5f00\u63a5\u53e3\u8de8\u57df\n    &quot;dayjs&quot;: &quot;^1.11.13&quot;, \/\/ \u683c\u5f0f\u5316\u65e5\u671f\u5e93\n    &quot;express&quot;: &quot;^4.21.1&quot;, \/\/ node\u7684\u8001\u724c\u6846\u67b6\n    &quot;minio&quot;: &quot;^7.1.3&quot;, \/\/ minio\u63d0\u4f9b\u7684\u5305\n    &quot;multer&quot;: &quot;^1.4.5-lts.1&quot;, \/\/ \u6587\u4ef6\u4e0a\u4f20\u4e13\u7528\u5e93\n  }\n}<\/code><\/pre>\n<h3>\u5f15\u5165\u5305\u7b49\u76f8\u5173\u51c6\u5907<\/h3>\n<pre><code class=\"js\">const express = require('express');\nconst bodyParser = require('body-parser');\nconst multer = require('multer');\nconst Minio = require('minio');\nconst dayjs = require('dayjs');\nconst cors = require('cors');\n\nconst app = express();\nconst port = 19000; \/\/ \u628a\u540e\u7aef\u670d\u52a1\u542f\u52a8\u572819000\u7aef\u53e3\n\napp.use(cors({ origin: '*' })); \/\/ \u653e\u5f00\u8de8\u57df\n\nconst expoUrl = 'http:\/\/127.0.0.1:9000'; \/\/ minio\u63d0\u4f9b\u7684\u63a5\u53e3\u5730\u5740\n\n\/\/ \u6587\u4ef6\u4e0a\u4f20\u914d\u7f6e\nconst upload = multer({\n    limits: {\n        fileSize: 20 * 1024 * 1024, \/\/ \u9650\u523620MB\n    },\n    fileFilter: (req, file, cb) =&gt; {\n        \/\/ \u8fd9\u91cc\u53ef\u4ee5\u6dfb\u52a0\u6587\u4ef6\u7c7b\u578b\u9650\u5236\n        cb(null, true);\n    },\n});\n\n\/\/ \u4e2d\u95f4\u4ef6\u914d\u7f6e\napp.use(bodyParser.json());\napp.use(bodyParser.urlencoded({ extended: true }));\n\n\/\/ \u670d\u52a1\u7aef\u53e3\u542f\u52a8\napp.listen(port, () =&gt; {\n    console.log(`\u670d\u52a1\u5668\u8fd0\u884c\u5728 http:\/\/localhost:${port}`);\n}); <\/code><\/pre>\n<h3>\u521b\u5efaminio\u5ba2\u6237\u7aef\u793a\u4f8b\u3001\u5e76\u786e\u4fddbucket\u5b58\u5728<\/h3>\n<pre><code class=\"js\">\/\/ MinIO \u5ba2\u6237\u7aef\u914d\u7f6e\nconst minioClient = new Minio.Client({\n    endPoint: '127.0.0.1',\n    port: 9000,\n    useSSL: false,\n    accessKey: 'minioadmin',\n    secretKey: 'minioadmin'\n});\n\n\/\/ \u786e\u4fdd bucket \u5b58\u5728\nconst bucketName = 'files';\nminioClient.bucketExists(bucketName, function (err, exists) {\n    if (err) {\n        return console.log(err);\n    }\n    if (!exists) {\n        minioClient.makeBucket(bucketName, function (err) {\n            if (err) {\n                return console.log('\u521b\u5efa bucket \u5931\u8d25:', err);\n            }\n            console.log('Bucket \u521b\u5efa\u6210\u529f');\n        });\n    }\n});<\/code><\/pre>\n<blockquote><p>bucket\u5b58\u5728\u5c31\u76f4\u63a5\u4f7f\u7528\uff0c\u4e0d\u5b58\u5728\u5c31\u521b\u5efa\u4e00\u4e2a\u540d\u4e3afiles\u7684\u6876<\/p><\/blockquote>\n<h3>\u4e0a\u4f20\u6587\u4ef6\u63a5\u53e3<\/h3>\n<pre><code class=\"js\">\/\/ \u6587\u4ef6\u4e0a\u4f20\u63a5\u53e3\napp.post('\/upload', upload.single('file'), async (req, res) =&gt; {\n    try {\n        if (!req.file) {\n            return res.status(400).json({ error: '\u6ca1\u6709\u6587\u4ef6\u88ab\u4e0a\u4f20' });\n        }\n\n        console.log('req.file---&gt;', req.file)\n\n        const decodedFileName = decodeFileName(req.file.originalname);\n\n        const fileName = dayjs().format('YYYYMMDDHHmmss') + '-' + decodedFileName\n\n        const metaData = {\n            \/\/ \u82e5\u6587\u4ef6\u540d\u4ee5.txt\u7ed3\u5c3e\uff0c\u5219\u8865\u5145\u7f16\u7801\u4e3a text\/plain; charset=utf-8 \u89e3\u51b3\u4e71\u7801\u95ee\u9898\n            'Content-Type': req.file.originalname.endsWith('.txt') ? 'text\/plain; charset=utf-8' : req.file.mimetype,\n            'Content-Disposition': 'inline'\n        };\n\n        \/\/ \u8c03\u7528minio\u7684putObject\uff0c\u628a\u6587\u4ef6\u5b58\u50a8\u8fdb\u53bb\n        await minioClient.putObject(bucketName, fileName, req.file.buffer, metaData);\n\n        res.json({\n            success: true,\n            fileName: fileName,\n            message: '\u6587\u4ef6\u4e0a\u4f20\u6210\u529f'\n        });\n    } catch (error) {\n        console.error('\u4e0a\u4f20\u9519\u8bef:', error);\n        res.status(500).json({ error: '\u6587\u4ef6\u4e0a\u4f20\u5931\u8d25' });\n    }\n});<\/code><\/pre>\n<p>\u6ce8\u610f\uff0c\u8fd9\u91cc\u7684originalname\u53ef\u80fd\u4f1a\u4e71\u7801\uff0c\u9700\u8981\u4f7f\u7528utf-8\u5b57\u7b26\u96c6\u6307\u5b9a\u4e00\u4e0b<\/p>\n<pre><code class=\"js\">\/\/ \u6587\u4ef6\u540d\u7f16\u7801\u5904\u7406\u51fd\u6570\nfunction decodeFileName(originalname) {\n    try {\n        \/\/ \u5c1d\u8bd5\u4f7f\u7528 Buffer \u8fdb\u884c\u89e3\u7801\n        return Buffer.from(originalname, 'latin1').toString('utf8');\n    } catch (error) {\n        console.error('\u6587\u4ef6\u540d\u89e3\u7801\u9519\u8bef:', error);\n        return originalname;\n    }\n}<\/code><\/pre>\n<h3>\u6587\u4ef6\u5217\u8868\u67e5\u8be2\u63a5\u53e3<\/h3>\n<p>\u8c03\u7528minio\u7684listObjects\u65b9\u6cd5\uff0c\u67e5\u8be2\u5217\u8868<\/p>\n<pre><code class=\"js\">\/\/ \u83b7\u53d6\u6587\u4ef6\u5217\u8868\u63a5\u53e3\napp.get('\/files', async (req, res) =&gt; {\n\n    try {\n        const files = [];\n        const stream = minioClient.listObjects(bucketName, '', true);\n\n        stream.on('data', function (obj) {\n            files.push({\n                name: obj.name,\n                size: obj.size,\n                lastModified: dayjs(obj.lastModified).format('YYYY-MM-DD HH:mm:ss'),\n                url: `${expoUrl}\/${bucketName}\/${obj.name}`\n            });\n        });\n\n        stream.on('end', function () {\n            \/\/ \u6309\u6700\u540e\u4fee\u6539\u65f6\u95f4\u964d\u5e8f\u6392\u5e8f\uff08\u6700\u65b0\u7684\u5728\u524d\uff09\n            files.sort((a, b) =&gt; {\n                return new Date(b.lastModified) - new Date(a.lastModified);\n            });\n            res.json(files);\n        });\n\n        stream.on('error', function (err) {\n            console.error('\u83b7\u53d6\u6587\u4ef6\u5217\u8868\u9519\u8bef:', err);\n            res.status(500).json({ error: '\u83b7\u53d6\u6587\u4ef6\u5217\u8868\u5931\u8d25' });\n        });\n    } catch (error) {\n        console.error('\u83b7\u53d6\u6587\u4ef6\u5217\u8868\u9519\u8bef:', error);\n        res.status(500).json({ error: '\u83b7\u53d6\u6587\u4ef6\u5217\u8868\u5931\u8d25' });\n    }\n});<\/code><\/pre>\n<h3>\u6839\u636e\u6587\u4ef6\u540d\u5220\u9664\u5bf9\u5e94\u7684\u6587\u4ef6<\/h3>\n<p>\u8c03\u7528minio\u7684removeObject\u65b9\u6cd5<\/p>\n<pre><code class=\"js\">\/\/ \u5220\u9664\u6587\u4ef6\u63a5\u53e3\napp.delete('\/files\/:fileName', async (req, res) =&gt; {\n    try {\n        const fileName = req.params.fileName;\n        await minioClient.removeObject(bucketName, fileName);\n        res.json({\n            success: true,\n            message: '\u6587\u4ef6\u5220\u9664\u6210\u529f'\n        });\n    } catch (error) {\n        console.error('\u5220\u9664\u6587\u4ef6\u9519\u8bef:', error);\n        res.status(500).json({ error: '\u6587\u4ef6\u5220\u9664\u5931\u8d25' });\n    }\n});<\/code><\/pre>\n<h3>\u83b7\u53d6\u6587\u4ef6\u4e0b\u8f7d\u94fe\u63a5\u63a5\u53e3<\/h3>\n<pre><code class=\"js\">\/\/ \u83b7\u53d6\u6587\u4ef6\u4e0b\u8f7d\u94fe\u63a5\u63a5\u53e3\napp.get('\/files\/:fileName', async (req, res) =&gt; {\n    try {\n        const fileName = req.params.fileName;\n        const url = `${expoUrl}\/${bucketName}\/${fileName}`;\n        res.json({\n            success: true,\n            url: url\n        });\n    } catch (error) {\n        console.error('\u83b7\u53d6\u6587\u4ef6\u94fe\u63a5\u9519\u8bef:', error);\n        res.status(500).json({ error: '\u83b7\u53d6\u6587\u4ef6\u94fe\u63a5\u5931\u8d25' });\n    }\n});<\/code><\/pre>\n<h2>\u524d\u7aefReact\u4ee3\u7801<\/h2>\n<h3>\u4f7f\u7528Antd\u7684Upload\u7ec4\u4ef6\u4e0a\u4f20\u6587\u4ef6<\/h3>\n<p><code>BASE_URL<\/code>\u4f5c\u4e3a\u540e\u7aef\u670d\u52a1\u7684\u63a5\u53e3\u5730\u5740\uff0c\u4e3a\uff1a<code>export const BASE_URL = 'http:\/\/127.0.0.1:19000';<\/code><\/p>\n<pre><code class=\"js\">interface UpContentProps {\n  updateList: () =&gt; void;\n}\n\nexport default function UpContent({ updateList }: UpContentProps) {\n\n  const props: UploadProps = {\n    action: `${BASE_URL}\/upload`, \/\/ \u4e0a\u4f20\u7684\u5730\u5740\n    showUploadList: false, \/\/ \u662f\u5426\u5c55\u793a\u6587\u4ef6\u5217\u8868\n    beforeUpload: (file) =&gt; { \/\/ \u4e0a\u4f20\u524d\u7684\u6587\u4ef6\u5927\u5c0f\u63a7\u5236\n      \/\/ \u5224\u65ad\u6587\u4ef6\u5927\u5c0f\n      if (file.size &gt; 1024 * 1024 * 10) {\n        message.error('\u6587\u4ef6\u5927\u5c0f\u4e0d\u80fd\u8d85\u8fc710MB');\n        return false;\n      }\n      return true;\n    },\n    onChange(info) {\n      if (info.file.status !== 'uploading') {\n        \/\/ console.log('\u6587\u4ef6\u4e0a\u4f20\u4e2d...');\n      }\n      if (info.file.status === 'done') {\n        message.success(`${info.file.name} \u4e0a\u4f20\u6210\u529f`);\n\n        \/\/ \u5237\u65b0\u5217\u8868\n        updateList();\n\n      } else if (info.file.status === 'error') {\n        message.error(`${info.file.name} \u4e0a\u4f20\u5931\u8d25`);\n      }\n    },\n  };\n\n  return (\n    &lt;div style={{ marginTop: '6px' }}&gt;\n      &lt;Button type='primary' icon={&lt;ReloadOutlined \/&gt;} onClick={() =&gt; updateList()} &gt;\u5237\u65b0\u5217\u8868&lt;\/Button&gt;&amp;nbsp;&amp;nbsp;\n      &lt;Upload {...props}&gt;\n        &lt;Button type=&quot;dashed&quot; icon={&lt;UploadOutlined \/&gt;}&gt;\u4e0a\u4f20\u6587\u4ef6&lt;\/Button&gt;\n      &lt;\/Upload&gt;\n    &lt;\/div&gt;\n  )\n}<\/code><\/pre>\n<h3>\u4f7f\u7528Antd\u7684Table\u7ec4\u4ef6\u5c55\u793a\u64cd\u4f5c\u6587\u4ef6<\/h3>\n<p>\u63a5\u53e3\u5982\u56fe\uff1a<\/p>\n<p><img decoding=\"async\" width=\"723\" height=\"435\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVdjWlB\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>\u5b9a\u4e49\u6570\u636e\u63a5\u53e3\uff0c\u53c2\u7167\u4e0a\u56fe\uff1a<\/p>\n<pre><code class=\"ts\">interface recordType {\n  name: string;\n  size: number;\n  lastModified: string;\n  url: string;\n}<\/code><\/pre>\n<p>\u5b9a\u4e49\u8868\u683c\u5217\uff0c\u53c2\u7167\u7b2c\u4e00\u5f20\u6548\u679c\u56fe<\/p>\n<pre><code class=\"js\">  const columns: ColumnsType&lt;recordType&gt; = [\n    {\n      title: '\u5e8f\u53f7',\n      key: 'index',\n      width: 80,\n      render: (_: any, __: any, index: number) =&gt; index + 1,\n      responsive: ['lg', 'md'], \/\/ \u53ea\u5728\u5927\u5c4f\u548c\u4e2d\u5c4f\u663e\u793a\n    },\n    {\n      title: '\u6587\u4ef6\u540d',\n      dataIndex: 'name',\n      key: 'name',\n      render: (value: string, record: recordType) =&gt; &lt;a href={record.url} target=&quot;_blank&quot;&gt;{value}&lt;\/a&gt;,\n    },\n    {\n      title: '\u6587\u4ef6\u5927\u5c0f',\n      dataIndex: 'size',\n      key: 'size',\n      render: (value: number) =&gt; &lt;span&gt;{formatSize(value)}&lt;\/span&gt;,\n      sorter: (a: recordType, b: recordType) =&gt; a.size - b.size,\n    },\n    {\n      title: '\u4e0a\u4f20\u65f6\u95f4',\n      dataIndex: 'lastModified',\n      key: 'lastModified',\n      sorter: (a: recordType, b: recordType) =&gt; new Date(a.lastModified).getTime() - new Date(b.lastModified).getTime(),\n    },\n    {\n      title: '\u64cd\u4f5c',\n      dataIndex: 'action',\n      key: 'action',\n      render: (_: any, record: recordType) =&gt; (\n        &lt;Space size=&quot;middle&quot;&gt;\n          &lt;Button danger type=&quot;link&quot; onClick={() =&gt; handleDelete(record.name)}&gt;\u5220\u9664&lt;\/Button&gt;\n          &lt;Button type=&quot;link&quot; onClick={() =&gt; handleDownload(record.name)}&gt;\u4e0b\u8f7d&lt;\/Button&gt;\n        &lt;\/Space&gt;\n      ),\n    },\n  ];<\/code><\/pre>\n<p>\u53d1\u8bf7\u6c42\u64cd\u4f5c\u6570\u636e<\/p>\n<pre><code class=\"js\">const DownContent = forwardRef((_, ref) =&gt; {\n\n  const [data, setData] = useState([]);\n  const [loading, setLoading] = useState(false);\n\n  const columns: ColumnsType&lt;recordType&gt; = [ ...... ]; \/\/ \u8868\u683c\u5217\u6570\u636e\n\n  useEffect(() =&gt; { getList() }, []) \/\/ \u521d\u59cb\u8bf7\u6c42\u5217\u8868\u6570\u636e\n\n  const getList = () =&gt; {\n    setLoading(true);\n    fetch(`${BASE_URL}\/files`)\n      .then(res =&gt; res.json())\n      .then(data =&gt; {\n        const formattedData = data.map((item: Record&lt;string, any&gt;, index: number) =&gt; ({\n          ...item,\n          key: item.name || index,  \/\/ \u4f7f\u7528\u6587\u4ef6\u540d\u6216\u7d22\u5f15\u4f5c\u4e3a key\n        }));\n        setData(formattedData);\n      }).finally(() =&gt; {\n        setLoading(false);\n      });\n  }\n\n  const handleDelete = (name: string) =&gt; {\n    Modal.confirm({\n      title: `\u786e\u5b9a\u5220\u9664\u6587\u4ef6\u300a${name}\u300b\u5417\uff1f`,\n      okText: '\u786e\u5b9a',\n      cancelText: '\u53d6\u6d88',\n      onOk: () =&gt; {\n        setLoading(true);\n        fetch(`${BASE_URL}\/files\/${name}`, {\n          method: 'DELETE',\n        }).then(res =&gt; res.json()).then(data =&gt; {\n          data.success &amp;&amp; getList();\n          message.success('\u5220\u9664\u6210\u529f');\n        }).finally(() =&gt; {\n          setLoading(false);\n        });\n      },\n      onCancel: () =&gt; {\n        console.log('\u53d6\u6d88');\n      },\n    });\n  }\n\n  const handleDownload = (name: string) =&gt; { \/\/ \u540c\u4e0afetch\u8bf7\u6c42\u903b\u8f91\uff0c\u4e0d\u8d58\u8ff0... }\n\n  return (\n    &lt;div style={{ marginTop: '20px' }}&gt;\n      &lt;Table loading={loading} columns={columns} dataSource={data} scroll={{ y: '72vh' }} pagination={{ pageSize: 10 }} \/&gt;\n    &lt;\/div&gt;\n  )\n});\n\nexport default DownContent;<\/code><\/pre>\n<ul>\n<li>\u81f3\u6b64\uff0c\u5c31\u53ef\u4ee5\u5b9e\u73b0\u5bf9\u5e94\u7684\u529f\u80fd\u4e86<\/li>\n<li>\u76f8\u4fe1\u5927\u5bb6\u628a\u4ee3\u7801\u62c9\u4e0b\u6765\uff0c\u5e76\u4e14\u770b\u770b\uff0c\u5f53\u9762\u8bd5\u5b98<\/li>\n<li><strong>\u8865\u5145\uff1a\u6211\u670b\u53cb\u9762\u8bd5\u7684\uff0c\u8fd9\u4e2a\u95eeoss\u9898\u76ee\u7684\u516c\u53f8\uff0c\u6302\u4e86\uff0c\u6700\u8fd1\u8fd8\u5728\u7ee7\u7eed\u9762\u8bd5\u5462&#8230;<\/strong><\/li>\n<\/ul>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000046659182\">https:\/\/segmentfault.com\/a\/1190000046659182<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6211\u6709\u4e00\u4e2a\u670b\u53cb\uff0c\u524d\u7aef\uff0c\u6700\u8fd1\u5728\u627e\u5de5\u4f5c\uff0c\u9762\u8bd5\u5b98\u5c31\u95ee\u4e86\u4ed6\uff0c\u5bf9oss\u4e86\u89e3\u5417\uff0c\u4ed6\u6ca1\u56de\u7b54\u4e0a\u6765\uff0c\u4e8e\u662f\u5c31\u6709\u4e86\u8fd9\u7bc7\u6587\u7ae0&#038;#8230 [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4],"tags":[],"class_list":["post-10020834","post","type-post","status-publish","format-standard","hentry","category-share"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10020834","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10020834"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10020834\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10020834"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10020834"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10020834"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}