Google Cloud Storageのクロスバケットレプリケーションを試してみる

Google Cloud Storageのクロスバケットレプリケーションを試してみる

Clock Icon2024.09.30

はじめに

データアナリティクス事業本部のkobayashiです。

GoogleCloudのCloud Storage(GCS)でバケット間のレプリケーションがプレビューになったので早速試してみました。その内容をまとめます。

Google Cloud release notes  |  Documentation

GCSのクロスバケットレプリケーションとは

GCSのバケット間レプリケーションを使用することで、ソースバケットから宛先バケットへ、新規および更新されたオブジェクトを非同期にコピーすることができます。仕様としてはStorage Transfer Serviceを使用してオブジェクトろソースバケットからターゲットバケットに非同期にコピーする機能です。
使用する場面としてはデータを別リージョンにリプリケーションして障害時に復旧させるために使用する場合、コンプライアンス要件でデータの複製を求められる場合、別リージョンへのデータ移行が考えられます。

https://cloud.google.com/storage/docs/using-cross-bucket-replication#view-replication-job

では早速試してみます。

GCSのクロスバケットレプリケーションを試してみる

クロスバケットレプリケーションを設定する

始めにソースバケットとターゲットバケットを作成します。
ここではあえてマルチリージョンを選択した上でasiausで別リージョンにしてみます。

$ gcloud storage buckets create gs://example-bucket-crr-test-source --location=asia
$ gcloud storage buckets create gs://example-bucket-crr-test-target --location=us

次にクロスバケットレプリケーションのジョブを作成しますが、先にGoogle Cloudが管理する2つのサービスアカウントに対してロールを付与する必要があります。
始めにproject-{プロジェクト番号}@storage-transfer-service.iam.gserviceaccount.comに対してPub/Sub Editorのroleroles/pubsub.editoとStorage Adminの
roleroles/storage.adminを付与する必要があります。
次にservice-{プロジェクト番号}@gs-project-accounts.iam.gserviceaccount.comに対してPub/Sub Publisherのroleroles/pubsub.publisherを付与する必要があります。

$ gcloud projects add-iam-policy-binding kobayashi-masahiro \
  --member="serviceAccount:project-233151396088@storage-transfer-service.iam.gserviceaccount.com" \
  --role="roles/storage.admin"
$ gcloud projects add-iam-policy-binding kobayashi-masahiro \
  --member="serviceAccount:project-233151396088@storage-transfer-service.iam.gserviceaccount.com" \
  --role="roles/pubsub.editor"
$ gcloud projects add-iam-policy-binding kobayashi-masahiro \
  --member="serviceAccount:service-233151396088@gs-project-accounts.iam.gserviceaccount.com" \
  --role="roles/pubsub.publisher"

これで必要なサービスアカウントに必要なロールが与えられたのでクロスバケットレプリケーションのジョブを作成します。

$ gcloud alpha transfer jobs create gs://example-bucket-crr-test-source gs://example-bucket-crr-test-target --replication

これでクロスバケットレプリケーションの設定は終わりました。
一度マネージメントコンソールでクロスバケットレプリケーションの設定を確認してみます。
バケット詳細 > 構成の中にクロスバケットレプリケーションの項目があるのでこちらをクリクすると設定内容が表示されます。

source-bucket

gcloudコマンドからも同様の内容が確認できます。

$ gcloud alpha transfer jobs list --job-type=replication
NAME                                                                           LATEST_OPERATION_NAME
replication-example-bucket-crr-test-source-example-bucket-crr-test-target  transferJobs-replication-example-bucket-crr-test-source-example-bucket-crr-test-target-1043061581904043395

クロスバケットレプリケーションしてみる

ではソースバケットにオブジェクトを作成してクロスバケットレプリケーションが行われることを確認します。

$ gcloud storage cp ./utf_ken_all.csv gs://example-bucket-crr-test-source/
Copying file://./utf_ken_all.csv to gs://example-bucket-crr-test-source/utf_ken_all.csv

Average throughput: 56.7MiB/s
$ gcloud storage cp ./utf_ken_all-2.csv gs://example-bucket-crr-test-source/
Copying file://./utf_ken_all-2.csv to gs://example-bucket-crr-test-source/utf_ken_all-2.csv

Average throughput: 34.6MiB/s

ソースバケットとターゲットバケットのオブジェクトを確認してみます。
オブジェクトを作成した直後はターゲットバケットにオブジェクトが存在しません。

$ gcloud storage ls -l gs://example-bucket-crr-test-source/
  36677886  2024-09-30T00:13:23Z  gs://example-bucket-crr-test-source/utf_ken_all-2.csv
  18338943  2024-09-30T00:13:20Z  gs://example-bucket-crr-test-source/utf_ken_all.csv
TOTAL: 2 objects, 55016829 bytes (52.47MiB)
$ gcloud storage ls -l gs://example-bucket-crr-test-target/
TOTAL: 0 objects, 0 bytes (0B)

しばらくしてから再度オブジェクトを確認してみるとクロスバケットレプリケーションが行われオブジェクトがターゲットバケットにレプリケーションされていることがわかります。

$ gcloud storage ls -l gs://example-bucket-crr-test-source/
  36677886  2024-09-30T00:13:23Z  gs://example-bucket-crr-test-source/utf_ken_all-2.csv
  18338943  2024-09-30T00:13:20Z  gs://example-bucket-crr-test-source/utf_ken_all.csv
TOTAL: 2 objects, 55016829 bytes (52.47MiB)
$ gcloud storage ls -l gs://example-bucket-crr-test-target/
  36677886  2024-09-30T00:13:36Z  gs://example-bucket-crr-test-target/utf_ken_all-2.csv
  18338943  2024-09-30T00:13:34Z  gs://example-bucket-crr-test-target/utf_ken_all.csv
TOTAL: 2 objects, 55016829 bytes (52.47MiB)

次にオブジェクトを更新してみます。

$ gcloud storage cp ./utf_ken_all.csv gs://example-bucket-crr-test-source/
Copying file://./utf_ken_all.csv to gs://example-bucket-crr-test-source/utf_ken_all.csv

Average throughput: 35.0MiB/s

オブジェクトの更新もクロスバケットレプリケーションでレプリケーションされてターゲットバケットのオブジェクトが更新されていることがわかります。

$ gcloud storage ls -l gs://example-bucket-crr-test-source/
  36677886  2024-09-30T00:13:23Z  gs://example-bucket-crr-test-source/utf_ken_all-2.csv
  22670663  2024-09-30T00:18:48Z  gs://example-bucket-crr-test-source/utf_ken_all.csv
TOTAL: 2 objects, 59348549 bytes (56.60MiB)
$ gcloud storage ls -l gs://example-bucket-crr-test-target/
  36677886  2024-09-30T00:13:36Z  gs://example-bucket-crr-test-target/utf_ken_all-2.csv
  22670663  2024-09-30T00:18:31Z  gs://example-bucket-crr-test-target/utf_ken_all.csv
TOTAL: 2 objects, 59348549 bytes (56.60MiB)

まとめ

Google Cloud Storageのバケット間のレプリケーションがであるクロスバケットレプリケーションを試してみました。GCSのロケーションが異なってもレプリケーションを行え非常に便利な機能ですので色々使い勝手がありそうです。

最後まで読んで頂いてありがとうございました。

この記事をシェアする

facebook logohatena logotwitter logo

© Classmethod, Inc. All rights reserved.