{"slug":"eo-function","title":"对标CF Worker？拿EdgeOne边缘函数做一个随机图API！","description":"很早就知道EdgeOne边缘函数，一直都没来得及体验，今天上手后发现它真的很强大","coverImage":"https://b.acofork.com/img/eo-function-historical-cover.webp","coverWidth":289,"coverHeight":226,"date":"2025-07-31","tags":["服务器"],"pinned":false,"wordCount":208,"html":"<h2 id=\"正式开始\">正式开始</h2><p>前往 <a href=\"https://github.com/afoim/EdgeOne_Function_PicAPI\" target=\"_blank\" rel=\"noopener noreferrer\">afoim/EdgeOne_Function_PicAPI: 适用于EdgeOne边缘函数的随机图API</a></p>\n<p>复制 <code>worker.js</code> 代码</p>\n<p>部署到EdgeOne边缘函数</p>\n<p><img src=\"https://b.acofork.com/img/eo-function-4274a5c6-c3d5-468b-8c98-d515a0a22762.webp\" alt=\"\" width=\"925\" height=\"921\" loading=\"eager\" decoding=\"async\" /></p>\n<p>将代码开头的 <code>R2_CONFIG</code> 设为你自己的</p>\n<div class=\"markdown-code\" data-language=\"js\"><div class=\"markdown-code-header\"><span class=\"markdown-code-language\"><svg class=\"code-language-icon\" aria-hidden=\"true\" width=\"16\" height=\"16\" viewbox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" xmlns=\"http://www.w3.org/2000/svg\"><path d=\"M8 3H7a2 2 0 0 0-2 2v5a2 2 0 0 1-2 2 2 2 0 0 1 2 2v5c0 1.1.9 2 2 2h1\"></path><path d=\"M16 21h1a2 2 0 0 0 2-2v-5c0-1.1.9-2 2-2a2 2 0 0 0-2-2V5a2 2 0 0 0-2-2h-1\"></path></svg><span>js</span></span><button type=\"button\" class=\"code-block-copy\" aria-label=\"复制代码\" title=\"复制代码\"><svg class=\"code-block-copy-icon\" aria-hidden=\"true\" width=\"16\" height=\"16\" viewbox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" xmlns=\"http://www.w3.org/2000/svg\"><rect width=\"14\" height=\"14\" x=\"8\" y=\"8\" rx=\"2\" ry=\"2\"></rect><path d=\"M4 16c-1.1 0-2-.9-2-2V4c0-1.1.9-2 2-2h10c1.1 0 2 .9 2 2\"></path></svg></button></div><pre><code class=\"hljs language-js\"><span class=\"hljs-keyword\">var</span> <span class=\"hljs-variable constant_\">R2_CONFIG</span> = {\n  <span class=\"hljs-attr\">region</span>: <span class=\"hljs-string\">'auto'</span>,\n  <span class=\"hljs-attr\">service</span>: <span class=\"hljs-string\">'s3'</span>,\n  <span class=\"hljs-attr\">accountId</span>: <span class=\"hljs-string\">''</span>,\n  <span class=\"hljs-attr\">accessKeyId</span>: <span class=\"hljs-string\">''</span>,\n  <span class=\"hljs-attr\">secretAccessKey</span>: <span class=\"hljs-string\">''</span>,\n  <span class=\"hljs-attr\">bucketName</span>: <span class=\"hljs-string\">''</span>\n};</code></pre></div><p>配置你的R2，将横屏随机图放到 <code>ri/h</code> 和 <code>ri/v</code> 。保证跟代码中的路径一样</p>\n<div class=\"markdown-code\" data-language=\"js\"><div class=\"markdown-code-header\"><span class=\"markdown-code-language\"><svg class=\"code-language-icon\" aria-hidden=\"true\" width=\"16\" height=\"16\" viewbox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" xmlns=\"http://www.w3.org/2000/svg\"><path d=\"M8 3H7a2 2 0 0 0-2 2v5a2 2 0 0 1-2 2 2 2 0 0 1 2 2v5c0 1.1.9 2 2 2h1\"></path><path d=\"M16 21h1a2 2 0 0 0 2-2v-5c0-1.1.9-2 2-2a2 2 0 0 0-2-2V5a2 2 0 0 0-2-2h-1\"></path></svg><span>js</span></span><button type=\"button\" class=\"code-block-copy\" aria-label=\"复制代码\" title=\"复制代码\"><svg class=\"code-block-copy-icon\" aria-hidden=\"true\" width=\"16\" height=\"16\" viewbox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" xmlns=\"http://www.w3.org/2000/svg\"><rect width=\"14\" height=\"14\" x=\"8\" y=\"8\" rx=\"2\" ry=\"2\"></rect><path d=\"M4 16c-1.1 0-2-.9-2-2V4c0-1.1.9-2 2-2h10c1.1 0 2 .9 2 2\"></path></svg></button></div><pre><code class=\"hljs language-js\">    <span class=\"hljs-comment\">// 根据路径确定前缀</span>\n    <span class=\"hljs-keyword\">var</span> prefix = <span class=\"hljs-string\">''</span>;\n    <span class=\"hljs-keyword\">if</span> (pathname === <span class=\"hljs-string\">'/h'</span>) {\n      prefix = <span class=\"hljs-string\">'ri/h/'</span>;\n    } <span class=\"hljs-keyword\">else</span> <span class=\"hljs-keyword\">if</span> (pathname === <span class=\"hljs-string\">'/v'</span>) {\n      prefix = <span class=\"hljs-string\">'ri/v/'</span>;\n    } <span class=\"hljs-keyword\">else</span> <span class=\"hljs-keyword\">if</span> (pathname === <span class=\"hljs-string\">'/'</span>) {</code></pre></div><p>访问 <code>/h</code> 则展示一张横屏随机图，访问 <code>/v</code> 则展示一张竖屏随机图</p>\n<p><img src=\"https://b.acofork.com/img/eo-function-fe7629b7-2acd-4e84-bd0c-d66ee7a54528.webp\" alt=\"\" width=\"1920\" height=\"1032\" loading=\"lazy\" decoding=\"async\" /></p>\n<p>如果需要绑定域名请设置触发规则</p>\n<p><img src=\"https://b.acofork.com/img/eo-function-33d931d4-e7cd-4d5d-afd8-85b787524391.webp\" alt=\"\" width=\"1364\" height=\"253\" loading=\"lazy\" decoding=\"async\" /></p>\n<h2 id=\"注意\">注意</h2><p>边缘函数每月有300万次的请求数限制，暂不知道超出是否扣费</p>\n","toc":[{"id":"正式开始","text":"正式开始","depth":2},{"id":"注意","text":"注意","depth":2}]}