1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Cloudflare Workers を使って Google Cloud Storage から R2 にプログレッシブ移行する

Last updated at Posted at 2023-09-25

目的

以下の記事を参考にしながら、Google Cloud Storage (GCS) から Cloudflare R2 にプログレッシブに移行します。

S3 と書いてあるところには、GCS も使えるため、試してみます。
(もちろん AWS S3 にも使えます)

R2 にオブジェクトがなければ、GCS から取ってきて次回以降アクセス用に R2 に保存する流れになります。

今回利用するコード

こちらにあるものを利用します。

GCS バケット準備

移行元となる GCS バケットを作成し、サンプルイメージをアップロードします。

gcloud storage buckets create gs://example-r2-migration --location=asia-northeast1
gsutil cp img/*.jpg gs://example-r2-migration
gsutil ls gs://example-r2-migration 
gs://example-r2-migration/1.jpg
gs://example-r2-migration/2.jpg
gs://example-r2-migration/3.jpg
gs://example-r2-migration/4.jpg
gs://example-r2-migration/5.jpg

R2 バケット準備

移行先となる Cloudflare R2 バケットを作成します。

wrangler r2 bucket create example-gcs-migration  

image.png

シークレット設定

GCS のアクセスに必要な認証情報をシークレットとして設定します。

wrangler secret put AWS_ACCESS_KEY_ID
wrangler secret put AWS_SECRET_ACCESS_KEY

Workers デプロイ

Cloudflare Workers をデプロイします。

% wrangler deploy
 ⛅️ wrangler 3.8.0 (update available 3.9.0)
-----------------------------------------------------
Your worker has access to the following bindings:
- R2 Buckets:
  - R2: example-gcs-migration
- Vars:
  - AWS_DEFAULT_REGION: "asia-northeast1"
  - AWS_SERVICE: "s3"
  - AWS_S3_BUCKET_SCHEME: "https:"
  - AWS_S3_BUCKET: "example-r2-migration.storage.googleap..."
Total Upload: 12.24 KiB / gzip: 3.87 KiB
Uploaded gcs-to-r2 (1.44 sec)
Published gcs-to-r2 (3.72 sec)
  https://gcs-to-r2.example.workers.dev
Current Deployment ID: a6bcb442-d219-4061-95b9-81ca32663726

プログレッシブ移行の確認

以下のように画像にアクセスしてページを表示します。

open https://gcs-to-r2.example.workers.dev/1.jpg

image.png

すると、R2 にアクセスした画像が保存されたことが確認できます。

image.png

残りの画像も問題なくプログレッシブ移行できました。

open https://gcs-to-r2.example.workers.dev/2.jpg
open https://gcs-to-r2.example.workers.dev/3.jpg
open https://gcs-to-r2.example.workers.dev/4.jpg
open https://gcs-to-r2.example.workers.dev/5.jpg

image.png

まとめ

ワンタイム移行では、一度に諸々を切り替える必要がありますが、

プログレッシブ移行では、既存の運用はそのままにしつつ、徐々に移行することが可能です。

Cloudflare Workers も手軽に使えるので、R2 への移行の1つの選択肢となりえるでしょう。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?