{"id":2237,"date":"2024-09-20T21:47:42","date_gmt":"2024-09-20T13:47:42","guid":{"rendered":"https:\/\/qishiya.com\/?p=2237"},"modified":"2024-09-20T21:48:01","modified_gmt":"2024-09-20T13:48:01","slug":"worker-r2-%e5%9b%be%e7%89%87%e5%ad%98%e5%82%a8","status":"publish","type":"post","link":"https:\/\/qishiya.com\/?p=2237","title":{"rendered":"Worker R2 \u56fe\u7247\u5b58\u50a8\u6700\u4f18\u89e3"},"content":{"rendered":"\n<p><a href=\"https:\/\/chimage.cn\/\">\u5c3a\u7801\u54e5<\/a> \u6700\u8fd1\u5728\u5fae\u8c03\u4e00\u4e2a\u6a21\u578b\uff0c\u9700\u8981\u7528\u5230\u5f88\u591a\u56fe\u7247\u6765\u8bad\u7ec3\uff0c\u540c\u65f6\u81ea\u52a8\u5316\u6d4b\u8bd5\u7684\u65f6\u5019\uff0c\u4e5f\u4f1a\u4ea7\u751f\u5f88\u591a\u56fe\u7247\uff0c\u60f3\u76f4\u63a5\u653e\u5230\u817e\u8baf\u4e91\u7684 COS \u91cc\u9762\uff0c\u4f46\u662f COS \u8c03\u6574\u4e86\u7b56\u7565\uff0c\u53ea\u6709\u5907\u6848\u7684\u57df\u540d\u624d\u80fd\u76f4\u63a5\u9884\u89c8\uff0c\u4e0d\u7136\u9ed8\u8ba4\u53d8\u6210\u4e0b\u8f7d\u4e86\uff0c\u6211\u5c31\u770b\u4e86\u4e00\u4e0b cloudflare \u7684 Worker R2 \u7684\u65b9\u6848\uff0c\u786e\u5b9e\u5f88\u65b9\u4fbf\u3002<\/p>\n\n\n\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_61 counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title \" >\u76ee\u5f55<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #ff6200;color:#ff6200\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #ff6200;color:#ff6200\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/qishiya.com\/?p=2237\/#%E5%9C%BA%E6%99%AF\" title=\"\u573a\u666f\">\u573a\u666f<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/qishiya.com\/?p=2237\/#%E6%96%B9%E6%A1%88\" title=\"\u65b9\u6848\">\u65b9\u6848<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/qishiya.com\/?p=2237\/#Worker\" title=\"Worker\">Worker<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/qishiya.com\/?p=2237\/#R2\" title=\"R2\">R2<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/qishiya.com\/?p=2237\/#javascrpt_%E4%B8%8A%E4%BC%A0%E5%9B%BE%E7%89%87\" title=\"javascrpt \u4e0a\u4f20\u56fe\u7247\">javascrpt \u4e0a\u4f20\u56fe\u7247<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/qishiya.com\/?p=2237\/#%E6%B3%A8%E6%84%8F%E4%BA%8B%E9%A1%B9\" title=\"\u6ce8\u610f\u4e8b\u9879\">\u6ce8\u610f\u4e8b\u9879<\/a><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E5%9C%BA%E6%99%AF\"><\/span>\u573a\u666f<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>\u6211\u627e\u4e86\u51e0\u4e2a\u7ed9\u6211\u6807\u6ce8\u56fe\u7247\u7684\u4eba\uff0c\u4ed6\u4eec\u6807\u6ce8\u5b8c\u6210\u7684\u56fe\u7247\uff0c\u8981\u5b9e\u65f6\u4fdd\u5b58\uff0c\u8981\u5728\u6d4f\u89c8\u5668\u91cc\u9762\u4fee\u6539\uff0c\u7acb\u9a6c\u5c31\u8981\u4fdd\u5b58\u5230\u670d\u52a1\u5668\uff0c\u800c\u6211\u4e0d\u60f3\u81ea\u5df1\u901a\u8fc7\u670d\u52a1\u7aef\u4e2d\u8f6c\uff0c\u60f3\u76f4\u63a5\u5b58\u50a8\u5230\u4e91\u5b58\u50a8\u5982 COS \u6216\u8005 S3 \u76f8\u5173\u7684\u5b58\u50a8\u4e2d\u3002<\/p>\n\n\n\n<p>\u6709\u65f6\u5019\u751f\u6210\u7684\u56fe\u7247\uff0c\u901a\u8fc7\u547d\u4ee4\u884c\u76f4\u63a5 curl \u4e0a\u4f20\uff0c\u4e0d\u60f3\u641e\u590d\u6742\u7684token \u8ba4\u8bc1\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E6%96%B9%E6%A1%88\"><\/span>\u65b9\u6848<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>\u817e\u8baf\u4e91\u7684\u6211\u6d4b\u8bd5\u4e86\u4e00\u4e0b\uff0c\u6ee1\u8db3\u4e0d\u4e86\u6211\u7684\u573a\u666f\uff0c\u5e76\u4e14\u8981\u5f00\u8fd9\u4e2a\u670d\u52a1\uff0c\u90a3\u4e2a\u670d\u52a1\uff0c\u8fd9\u4e9b\u90fd\u662f\u6536\u8d39\u7684\uff0c\u6211\u5b8c\u6210\u4ed8\u8d39\u540e\uff0c\u6700\u540e\u4e00\u4e2a\u9884\u89c8\u5c31\u4e0d\u884c\uff0c\u8981\u5907\u6848\u7684\u57df\u540d\uff0c\u4e0d\u5f97\u4e0d\u9009\u62e9\u522b\u7684\u65b9\u6848\u3002<\/p>\n\n\n\n<p>\u7136\u540e\u5c31\u76f4\u63a5\u4e0a\u4f20\u5230 R2\uff0c\u6211\u770b\u4e86\u4e00\u4e0b\u76f8\u5173\u7684 Api\uff0c\u867d\u7136\u662f\u517c\u5bb9S3 \u534f\u8bae\uff0c\u4f46\u662f\u8ba9 Cursor \u548c claude \u66f4\u6539\u4e86\u51e0\u6b21\uff0c\u90fd\u9047\u5230\u5404\u79cd\u95ee\u9898\u3002<\/p>\n\n\n\n<p>\u7136\u540e\u770b\u4e86\u4e00\u4e0b\u5f88\u591a\u4eba\u7528 Worker \u548c R2 \u76f4\u63a5\u5173\u8054\uff0c\u786e\u5b9e\u80fd\u6ee1\u8db3\u6211\u7684\u8981\u6c42\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Worker\"><\/span>Worker<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Worker \u6211\u76f4\u63a5\u8ba9 Cluade  \u7ed9\u6211\u5199\u7684\u4ee3\u7801\uff0c\u540c\u65f6\u53c2\u8003\u4e86\u5b98\u65b9\u7684<a href=\"https:\/\/developers.cloudflare.com\/r2\/api\/workers\/workers-api-usage\/\">\u793a\u4f8b<\/a>\u3002<\/p>\n\n\n\n<p>\u4ee3\u7801\u6ca1\u6709\u591a\u5c11\uff0c\u5e76\u4e14\u8fd8\u52a0\u4e86\u4e00\u4e2a\u7b80\u5355\u7684\u6743\u9650\u9a8c\u8bc1\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const ALLOW_LIST = &#91;\"cat-pic.jpg\"];\n\nfunction corsHeaders(request) {\n  return {\n    \"Access-Control-Allow-Origin\": \"*\",\n    \"Access-Control-Allow-Methods\": \"GET,HEAD,POST,PUT,DELETE,OPTIONS\",\n    \"Access-Control-Allow-Headers\": \"Content-Type,X-Custom-Auth-Key\",\n    \"Access-Control-Max-Age\": \"86400\",\n  };\n}\n\n\n\/\/ Check requests for a pre-shared secret\nconst hasValidHeader = (request, env) => {\n  return request.headers.get(\"X-Custom-Auth-Key\") === env.AUTH_KEY_SECRET;\n};\n\nfunction authorizeRequest(request, env, key) {\n  switch (request.method) {\n    case \"PUT\":\n    case \"DELETE\":\n      return hasValidHeader(request, env);\n    case \"GET\":\n      \/\/return ALLOW_LIST.includes(key);\n      return true;\n    default:\n      return false;\n  }\n}\n\nexport default {\n  async fetch(request, env) {\n    \/\/ \u5904\u7406 OPTIONS \u8bf7\u6c42\n    if (request.method === \"OPTIONS\") {\n      return new Response(null, {\n        headers: corsHeaders(request),\n      });\n    }\n\n    const url = new URL(request.url);\n    const key = url.pathname.slice(1);\n\n    if (!authorizeRequest(request, env, key)) {\n      return new Response(\"Forbidden\", {\n        status: 403,\n        headers: corsHeaders(request)\n      });\n    }\n\n    switch (request.method) {\n      case \"PUT\":\n        await env.MY_BUCKET.put(key, request.body);\n        return new Response(`Put ${key} successfully!`, {\n          headers: corsHeaders(request)\n        });\n      case \"GET\":\n        const object = await env.MY_BUCKET.get(key);\n\n        if (object === null) {\n          return new Response(\"Object Not Found\", {\n            status: 404,\n            headers: corsHeaders(request)\n          });\n        }\n\n        const headers = new Headers(corsHeaders(request));\n        object.writeHttpMetadata(headers);\n        headers.set(\"etag\", object.httpEtag);\n\n        return new Response(object.body, {\n          headers,\n        });\n      case \"DELETE\":\n        await env.MY_BUCKET.delete(key);\n        return new Response(\"Deleted!\", {\n          headers: corsHeaders(request)\n        });\n\n      default:\n        return new Response(\"Method Not Allowed\", {\n          status: 405,\n          headers: {\n            ...corsHeaders(request),\n            Allow: \"PUT, GET, DELETE, OPTIONS\",\n          },\n        });\n    }\n  },\n};\n<\/code><\/pre>\n\n\n\n<p>\u4e3b\u8981\u662f\u52a0\u4e86\u4e00\u4e9b\u8de8\u57df\u7684\u903b\u8f91\u5728\u91cc\u9762\uff0c\u4e0d\u7136\u5728\u6d4f\u89c8\u5668\u901a\u8fc7 Javascrip \u4e0a\u4f20\u56fe\u7247\u7684\u65f6\u5019\uff0c\u4f1a\u78b0\u5230\u8de8\u57df\u7684\u95ee\u9898\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"R2\"><\/span>R2<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>R2 \u6b63\u786e\u7684\u5efa\u5c31\u53ef\u4ee5\uff0c\u6ca1\u6709\u4ec0\u4e48\u8981\u7279\u522b\u6ce8\u610f\u7684\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"javascrpt_%E4%B8%8A%E4%BC%A0%E5%9B%BE%E7%89%87\"><\/span>javascrpt \u4e0a\u4f20\u56fe\u7247<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>  \/\/ \u751f\u6210\u56fe\u7247\u5e76\u4e0a\u4f20\u5230\u6307\u5b9a\u670d\u52a1\nasync function generateAndUploadImage() {\n    const canvas = document.createElement('canvas');\n    canvas.width = 300;\n    canvas.height = 300;\n    const ctx = canvas.getContext('2d');\n    ctx.fillStyle = 'white';\n    ctx.fillRect(0, 0, canvas.width, canvas.height);\n    ctx.fillStyle = 'black';\n    ctx.font = '30px Arial';\n    ctx.fillText('hello world', 50, 150);\n\n    \/\/ \u751f\u6210\u72ec\u4e00\u65e0\u4e8c\u7684\u6587\u4ef6\u540d\n    const uniqueFileName = generateUniqueFileName();\n\n    canvas.toBlob(function(blob) {\n        const formData = new FormData();\n        formData.append('file', blob, uniqueFileName);\n\n        fetch(`https:\/\/worker.abcc.com\/${uniqueFileName}`, {\n            method: 'PUT',\n            headers: {\n                'X-Custom-Auth-Key': 'key'\n            },\n            body: blob\n        })\n        .then(response => {\n            if (!response.ok) {\n                throw new Error('Network response was not ok');\n            }\n            return response.text();\n        })\n        .then(data => {\n            console.log('Success:', data);\n            alert(`Image uploaded successfully as ${uniqueFileName}!`);\n        })\n        .catch((error) => {\n            console.error('Error:', error);\n            alert('Error uploading image: ' + error.message);\n        });\n    }, 'image\/jpeg');\n}<\/code><\/pre>\n\n\n\n<p> <\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E6%B3%A8%E6%84%8F%E4%BA%8B%E9%A1%B9\"><\/span>\u6ce8\u610f\u4e8b\u9879<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>\u4e00\u5b9a\u8981\u81ea\u5b9a\u4e49\u57df\u540d\uff0c\u4e0d\u7136\u6709\u4e9b\u5730\u65b9\u4e0d\u80fd\u8bbf\u95ee\uff0c\u81ea\u5b9a\u4e49\u57df\u540d\u597d\u50cf\u95ee\u9898\u4e0d\u5927\u3002<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5c3a\u7801\u54e5 \u6700\u8fd1\u5728\u5fae\u8c03\u4e00\u4e2a\u6a21\u578b\uff0c\u9700\u8981\u7528\u5230\u5f88\u591a\u56fe\u7247\u6765\u8bad\u7ec3\uff0c\u540c\u65f6\u81ea\u52a8\u5316\u6d4b\u8bd5\u7684\u65f6\u5019\uff0c\u4e5f\u4f1a\u4ea7\u751f\u5f88\u591a\u56fe\u7247\uff0c\u60f3\u76f4\u63a5\u653e\u5230\u817e\u8baf\u4e91\u7684 COS \u91cc\u9762\uff0c\u4f46\u662f COS \u8c03\u6574\u4e86\u7b56\u7565\uff0c\u53ea\u6709\u5907\u6848\u7684\u57df\u540d\u624d\u80fd\u76f4\u63a5\u9884\u89c8\uff0c\u4e0d\u7136\u9ed8\u8ba4\u53d8\u6210\u4e0b\u8f7d\u4e86\uff0c\u6211\u5c31\u770b\u4e86\u4e00\u4e0b cloudflare \u7684  &#8230; <a title=\"Worker R2 \u56fe\u7247\u5b58\u50a8\u6700\u4f18\u89e3\" class=\"read-more\" href=\"https:\/\/qishiya.com\/?p=2237\" aria-label=\"\u66f4\u591a Worker R2 \u56fe\u7247\u5b58\u50a8\u6700\u4f18\u89e3\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[372],"tags":[],"class_list":["post-2237","post","type-post","status-publish","format-standard","hentry","category-372"],"_links":{"self":[{"href":"https:\/\/qishiya.com\/index.php?rest_route=\/wp\/v2\/posts\/2237","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/qishiya.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/qishiya.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/qishiya.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/qishiya.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2237"}],"version-history":[{"count":2,"href":"https:\/\/qishiya.com\/index.php?rest_route=\/wp\/v2\/posts\/2237\/revisions"}],"predecessor-version":[{"id":2239,"href":"https:\/\/qishiya.com\/index.php?rest_route=\/wp\/v2\/posts\/2237\/revisions\/2239"}],"wp:attachment":[{"href":"https:\/\/qishiya.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2237"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/qishiya.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2237"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/qishiya.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2237"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}