Rclone を使って Azure Blob Storage から Amazon S3 へオブジェクトを同期する

2023.01.09

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

いわさです。

Amazon QuickSight で Azure Blob Storage に格納されている CSV データを利用したいシーンがありました。

しかし、本日時点で QuickSight には Azure へのコネクタが用意されていません。

少し前のアップデートで Amazon Athena の Azure Data Lake Storage コネクタを使うと Azure Data Lake Storage オプションを有効化した Azure Blob Storage に対してクエリが実行出来ます。

しかし、ドキュメントを見てみると接続インターフェースが JDBC になっており、これはおそらく Azure Synapse を通してクエリすることになるので、用途や利用頻度によってはオーバースペックになることもありそうです。

他のオプションとして Azure Blob Storage から Amazon S3 にオブジェクトを同期し、そこから Athena などを通して QuickSight から利用する方法が考えられます。

以前 Azure Blob Storage と Amazon S3 間でデータのやり取りを何度か行ったことがあるのですが、実は全て Amazon S3 から Azure Blob Storage への単方向しか出来ませんでした。

調べてみたところ Rclone を使うことで実現出来そうだということがわかりました。

なお、Rclone を使った Azure Blob Storage から Amazon S3 への同期方法は AWS Prescriptive Guidance Patterns でも紹介されています。

本記事ではまずはローカル環境で Rclone を使って、Azure Blob Storage から Amazon S3 へのファイル同期を試してみたいと思います。
なお、検証環境として Apple M1 Max - Ventura 13.1 を使用しています。

準備と構成

各ストレージサービスを作成しオブジェクトをアップロード

Azure Blob Storage に以下のようにオブジェクトをアップロードしておきます。

Amazon S3 バケットにも以下のように別のオブジェクトをアップロードしておきます。

Rclone で Azure Blob Storage を構成する

Rclone を使う記事は DevelopersIO でもいくつか存在しています。
クラウドサービス間の連携ということで以下の記事が一番やりたいことに近いものでした。

AWS 側の設定については割愛しますので上記記事を参考にしてください。
そのままで問題ないはずです。

Rclone の Azure Blob Storage の構成については公式ドキュメントは以下となります。

まずは新規remoteを Azure Blob Storage として追加します。

% rclone config                   
Current remotes:

Name                 Type
====                 ====
s3                   s3

e) Edit existing remote
n) New remote
d) Delete remote
r) Rename remote
c) Copy remote
s) Set configuration password
q) Quit config
e/n/d/r/c/s/q> n

Enter name for new remote.
name> azureblob

Option Storage.
Type of storage to configure.
Choose a number from below, or type in your own value.
 1 / 1Fichier
   \ (fichier)
:
29 / Mega
   \ (mega)
30 / Microsoft Azure Blob Storage
   \ (azureblob)
31 / Microsoft OneDrive
   \ (onedrive)
:
50 / seafile
   \ (seafile)
Storage> 30

次にストレージアカウント名を指定します。
構成オプションはいくつか用意されていて、SAS URL を使う場合はここでの設定は不要です。
また、Azure Storage Emulator に対しての接続構成も可能のようで、その場合も不要です。

Option account.
Azure Storage Account Name.
Set this to the Azure Storage Account Name in use.
Leave blank to use SAS URL or Emulator, otherwise it needs to be set.
If this is blank and if env_auth is set it will be read from the
environment variable `AZURE_STORAGE_ACCOUNT_NAME` if possible.
Enter a value. Press Enter to leave empty.
account> hoge0109azure

Azure Blob Storage への認証方法を設定します。
今回のローカル実行はとりあえずお試しというところだったので、簡易的に共有キーを使いました。
セキュリティベストプラクティスからは離れた選択だと思いますので、実利用の際は別の方法も検討しましょう。

Rclone 自体は以下のように多様なオプションを備えています。

Option env_auth.
Read credentials from runtime (environment variables, CLI or MSI).
See the [authentication docs](/azureblob#authentication) for full info.
Enter a boolean value (true or false). Press Enter for the default (false).
env_auth> false

Option key.
Storage Account Shared Key.
Leave blank to use SAS URL or Emulator.
Enter a value. Press Enter to leave empty.
key> hogehogehogehoge==

Option sas_url.
SAS URL for container level access only.
Leave blank if using account/key or Emulator.
Enter a value. Press Enter to leave empty.
sas_url> 

Option tenant.
ID of the service principal's tenant. Also called its directory ID.
Set this if using
- Service principal with client secret
- Service principal with certificate
- User with username and password
Enter a value. Press Enter to leave empty.
tenant> 

Option client_id.
The ID of the client in use.
Set this if using
- Service principal with client secret
- Service principal with certificate
- User with username and password
Enter a value. Press Enter to leave empty.
client_id> 

Option client_secret.
One of the service principal's client secrets
Set this if using
- Service principal with client secret
Enter a value. Press Enter to leave empty.
client_secret> 

Option client_certificate_path.
Path to a PEM or PKCS12 certificate file including the private key.
Set this if using
- Service principal with certificate
Enter a value. Press Enter to leave empty.
client_certificate_path> 

Option client_certificate_password.
Password for the certificate file (optional).
Optionally set this if using
- Service principal with certificate
And the certificate has a password.
Choose an alternative below. Press Enter for the default (n).
y) Yes, type in my own password
g) Generate random password
n) No, leave this optional password blank (default)
y/g/n>

構成後は config ファイルに以下のように Amazon S3 と Azure Blob Storage の構成が追加されています。

% cat ~/.config/rclone/rclone.conf
[s3]
type = s3
provider = AWS
env_auth = true
region = ap-northeast-1
location_constraint = ap-northeast-1
acl = private
server_side_encryption = AES256
storage_class = STANDARD

[azureblob]
type = azureblob
account = hoge0109azure
key = hogehogehogehoge==

Rclone を使ってストレージサービスにアクセス

リスト

各サービスへの接続構成が完了したのでrclone lsコマンドを使ってファイルが参照出来るのか試してみます。

% rclone ls s3:hoge0109aws        
        8 fuga.csv
        9 fuga2.csv
% rclone ls azureblob:newcontainer
        8 hoge.csv
        5 hoge2.csv

参照することが出来ました。
各クラウドサービスへの認証はうまく出来ているようです。

コピー

ではrclone copyコマンドを使って Azure Blob Storage から Amazon S3 へのファイルコピーを行います。

特定の Blob コンテナーと S3 バケットを指定します。

% rclone copy azureblob:newcontainer s3:hoge0109aws

成功メッセージなどは表示されません。
失敗時はエラーメッセージが表示されます。

コマンド実行後に Amazon S3 バケットを確認してみます。

Azure Blob Storage のコンテナーに格納されていたオブジェクトが Amazon S3 バケットへコピーされています。
元から Amazon S3 バケットへ格納されていたオブジェクトは残っています。

同期

次にrclone syncコマンドを使って Azure Blob Storage のコンテナーを Amazon S3 のバケットへ同期させてみます。

コマンドが違うだけで指定方法は先程と同じです。

% rclone sync azureblob:newcontainer s3:hoge0109aws

Azure Blob Storage のコンテナーと Amazon S3 バケットが同期されました。
Amazon S3 バケットにだけ格納されていたオブジェクトは削除されています。

さいごに

本日は Rclone を使って Azure Blob Storage から Amazon S3 へオブジェクトを同期してみました。

AzCopy だけでは実現出来なかった Azure Blob Storage から Amazon S3 へのオブジェクト同期を行うことが出来ました。
ただ、今回はローカルでコマンドを実行しただけなので、今後実行環境をクラウドで、出来ればインフラの管理が不要なサービスを使って、自動化したいと思っています。

個人的には AWS Glue あるいは Amazon AppFlow のマネージドなソースコネクタとして Azure Blob Storage が追加されると嬉しいところですが。