ろぼいんブログ
更新:

Cloudflare R2で「We encountered an internal error. Please try again.」というエラーが出たときの対処法

@aws-sdk/client-s3 を使って、Amazon S3互換のCloudflare R2にファイルをアップロードするプログラムを書いていたところ、「We encountered an internal error. Please try again.」というエラーが発生しました。

この記事では、このエラーの原因と対処法を解説します。

原因

Cloudflare R2はAmazon S3互換を謳っていますが、100%の互換性があるわけではありません。

ドキュメントの S3 API compatibility を確認すると、対応していない機能もあることがわかります。

まれによくあるパターンとして、S3の仕様変更にR2が対応できておらず、その結果として@aws-sdk/client-s3からR2を使おうとするとエラーになるというものがあります。

今回のエラーは、@aws-sdk/client-s3のバージョン3.729.0以降から導入された仕様変更が原因でした。

AWS SDK for JavaScriptのドキュメントには、次のように書かれています。

Beginning with version 3.729.0 of the AWS SDK for JavaScript, the SDK provides default integrity protections by automatically calculating a CRC32 checksum for uploads.

日本語訳:
AWS SDK for JavaScriptのバージョン3.729.0以降、このSDKはアップロードのCRC32チェックサムを自動的に計算することで、デフォルトの整合性保護を提供します。

——『 Data integrity protection with Amazon S3 checksums - AWS SDK for JavaScript

この仕様変更が原因でエラーが出るようになったようです。R2のドキュメントにも次のように書かれています。

Client version 3.729.0 introduced a modification to the default checksum behavior from the client that is currently incompatible with R2 APIs.

日本語訳:
クライアントバージョン3.729.0では、クライアントからのチェックサムのデフォルトの挙動に現在R2 APIと互換性のない変更が加えられました。

——『 aws-sdk-js-v3 · Cloudflare R2 docs

同様に、AWS CLIでもバージョン2.23.0以降と1.37.0以降では、R2と互換性のない変更が加えられているようです。

Client versions 2.23.0 and 1.37.0 introduced a modification to the default checksum behavior from the client that is currently incompatible with R2 APIs.

日本語訳:
クライアントバージョン2.23.01.37.0では、クライアントからのチェックサムのデフォルトの挙動に現在R2 APIと互換性のない変更が加えられました。

——『 aws CLI · Cloudflare R2 docs

このように、S3の仕様変更にR2が対応していなかったことでエラーが発生したようです。

対処法

この問題の対処法は、R2の前述のドキュメントに書かれていました。

@aws-sdk/client-s3を使っている場合は、バージョン3.726.1にダウングレードするか、requestChecksumCalculationオプションとresponseChecksumValidationオプションを指定します。

try{(()=>{function a(e){if(!e)return;let t=e.getAttribute("tabindex")!==null,n=e.scrollWidth>e.clientWidth;n&&!t?e.setAttribute("tabindex","0"):!n&&t&&e.removeAttribute("tabindex")}var u=window.requestIdleCallback||(e=>setTimeout(e,1)),i=window.cancelIdleCallback||clearTimeout;function l(e){let t=new Set,n,r;return new ResizeObserver(c=>{c.forEach(o=>t.add(o.target)),n&&clearTimeout(n),r&&i(r),n=setTimeout(()=>{r&&i(r),r=u(()=>{t.forEach(o=>e(o)),t.clear()})},250)})}function d(e,t){e.querySelectorAll?.(".expressive-code pre > code").forEach(n=>{let r=n.parentElement;r&&t.observe(r)})}var s=l(a);d(document,s);var b=new MutationObserver(e=>e.forEach(t=>t.addedNodes.forEach(n=>{d(n,s)})));b.observe(document.body,{childList:!0,subtree:!0});document.addEventListener("astro:page-load",()=>{d(document,s)});})();}catch(e){console.error("[EC] tabindex-js-module failed:",e)}try{(()=>{function i(o){let e=document.createElement("pre");Object.assign(e.style,{opacity:"0",pointerEvents:"none",position:"absolute",overflow:"hidden",left:"0",top:"0",width:"20px",height:"20px",webkitUserSelect:"auto",userSelect:"all"}),e.ariaHidden="true",e.textContent=o,document.body.appendChild(e);let a=document.createRange();a.selectNode(e);let n=getSelection();if(!n)return!1;n.removeAllRanges(),n.addRange(a);let r=!1;try{r=document.execCommand("copy")}finally{n.removeAllRanges(),document.body.removeChild(e)}return r}async function l(o){let e=o.currentTarget,a=e.dataset,n=!1,r=a.code.replace(/\u007f/g,` `);try{await navigator.clipboard.writeText(r),n=!0}catch{n=i(r)}if(!n||e.parentNode?.querySelector(".feedback"))return;let t=document.createElement("div");t.classList.add("feedback"),t.append(a.copied),e.before(t),t.offsetWidth,requestAnimationFrame(()=>t?.classList.add("show"));let c=()=>!t||t.classList.remove("show"),d=()=>{!t||parseFloat(getComputedStyle(t).opacity)>0||(t.remove(),t=void 0)};setTimeout(c,1500),setTimeout(d,2500),e.addEventListener("blur",c),t.addEventListener("transitioncancel",d),t.addEventListener("transitionend",d)}function s(o){o.querySelectorAll?.(".expressive-code .copy button").forEach(e=>e.addEventListener("click",l))}s(document);var u=new MutationObserver(o=>o.forEach(e=>e.addedNodes.forEach(a=>{s(a)})));u.observe(document.body,{childList:!0,subtree:!0});document.addEventListener("astro:page-load",()=>{s(document)});})();}catch(e){console.error("[EC] copy-js-module failed:",e)}
const s3 = new S3Client({
// ...既存のコード
requestChecksumCalculation: "WHEN_REQUIRED",
responseChecksumValidation: "WHEN_REQUIRED"
});

また、AWS CLIを使っている場合は、バージョン2.22.35または1.36.40にダウングレードするか、--checksum-algorithm CRC32フラグを指定します。

おすすめアイテム

※このリンクを経由して商品を購入すると、当サイトの運営者が報酬を得ることがあります。詳細はこちら

このサイトを支援する

Buy Me a CoffeeまたはGitHub Sponsorsで支援していただけると、サイトの運営やコンテンツ制作の励みになります。定期的な支援と一度限りの支援がありますので、お間違いのないようにお願いします。

Buy me a coffee

著者のアイコン画像

生まれた時から、母国語よりも先にJavaScriptを使っていました。ネットの海のどこにもいなくてどこにでもいます。

Webフロントエンドプログラマーで、テクノロジーに関する話題を追いかけています。動画編集やプログラミングが趣味で、たまにデザインなどもやっています。主にTypeScriptを使用したWebフロントエンド開発を専門とし、便利で実用的なブラウザー拡張機能を作成しています。また、個人ブログを通じて、IT関連のニュースやハウツー、技術的なプログラミング情報を発信しています。

最新記事