MAGAZINE

ルーターマガジン

インフラ/運用

cloudflare R2ベータ版をaws cliから操作する

2022.08.19
Pocket

こんにちは。ルーターエンジニアのtoyamaです。
今回は、ベータ版が公開されたばかりのcloudflare R2ストレージとaws S3の速度比較を行ったのでその結果を共有したいと思います。S3の準備記事はこちらを参考におねがいします。

クローリングデータの納品にも使えるAWS-SDK for RubyでのS3基本操作Tips

本記事のデータは、2022年8月中旬のものです。

準備

まずはcloudflareのアカウントを作成し、R2ストレージを購入します。
月10GBまでは無料のようです。すごいですね。

次にバケットを作成します。

次にcliからアクセスするための認証情報を作成します。
R2管理画面からR2 API トークンの管理を押して作成します。
編集権限ありで作成します。


作成すると、「アクセスキーID」、「シークレットアクセスキー」が表示されますので、必ず保存しましょう。
aws configureで設定します。

$ aws configure --profile r2-test AWS Access Key ID [********************]: AWS Secret Access Key [************************]: Default region name [None]: Default output format [None]:

↓のコマンドのアカウントIDを↑の画像のアカウントIDにしてエラーがでなければ接続完了です。

aws s3 ls --profile r2-test --endpoint-url https://{アカウントID}.r2.cloudflarestorage.com s3://r2-test/

S3 API compatibility

CLIから操作できる情報はこちらのページにまとめられています。

実験内容

今回は、弊社でよく使うaws cli、ruby gemの2種類の操作でのアップロード、ダウンロード速度を比較してみました。

  • テストファイル作成
$ dd if=/dev/zero of=test100M bs=1M count=100
  • S3テストCLIコマンド
$ time aws s3 --profile s3-test cp ./test100M s3://s3-test/r2test/ $ time aws s3 --profile s3-test cp s3://s3-test/r2test/test100M ./test_download
  • R2テストCLIコマンド
$ time aws s3 cp --profile r2-test --endpoint-url https://{アカウントID}.r2.cloudflarestorage.com ./test100M s3://r2-test/test/ $ time aws s3 cp --profile r2-test --endpoint-url https://{アカウントID}.r2.cloudflarestorage.com s3://r2-test/test/test100M ./test_download

ruby gemテストスクリプト

require 'time' require 'aws-sdk-s3' require 'optparse' class S3Helper def initialize(connect) if connect == 'r2' @aws_setting = { bucket_name: 'r2-test', profile: 'r2-test', region: 'auto', endpoint: 'https://{アカウントID}.r2.cloudflarestorage.com' } elsif connect == 's3' @aws_setting = { bucket_name: 's3-test', profile: 's3-test', region: 'ap-northeast-1'} end @resource = Aws::S3::Resource.new(**@aws_setting.slice(:profile, :region, :endpoint)) end def upload_to_s3(data, file_path, s3_resource = @resource, bucket = @aws_setting[:bucket_name]) output_file = s3_resource.bucket(bucket).object(file_path) output_file.put(body: data) end def download_from_s3(s3_file_path, s3_resource = @resource, bucket = @aws_setting[:bucket_name]) input_file = s3_resource.bucket(bucket).object(s3_file_path) input_file.download_file('download_test.txt') end end ## ruby speed_test.rb {r2|s3} upload_times = [] download_times = [] test_txt = File.read('./test100M') s3_helper = S3Helper.new(ARGV[0]) 1.step(5) do |i| now = Time.now s3_helper.upload_to_s3(test_txt, "upload_test") upload_time = Time.now - now p upload_time upload_times << upload_time now = Time.now s3_helper.download_from_s3("upload_test") download_time = Time.now - now p download_time download_times << download_time end pp 'upload_times' pp upload_times pp upload_times.sum.fdiv(upload_times.length) pp 'download_times' pp download_times pp download_times.sum.fdiv(download_times.length)
$ ruby speed_test.rb {s3|r2}

結果

  • CLIテスト結果

    • ダウンロード速度

      S3 R2
      1回目 14.071s 13.159s
      2回目 13.194s 12.666s
      3回目 11.562s 12.821s
      4回目 13.567s 13.383s
      5回目 11.923s 17.528s
      平均 12.863s 13.911s
    • アップロード速度

      S3 R2
      1回目 5.016s 31.518s
      2回目 4.561s 27.918s
      3回目 5.076s 24.931s
      4回目 4.297s 31.482s
      5回目 4.208s 27.770s
      平均 4.631s 28.723s
  • ruby gemテスト結果

    • ダウンロード速度

      S3 R2
      1回目 11.222s 12.135s
      2回目 11.022s 12.385s
      3回目 11.169s 14.871s
      4回目 12.321s 12.971s
      5回目 11.438s 12.063s
      平均 11.434s 12.885s
    • アップロード速度

      S3 R2
      1回目 12.355s 6.378s
      2回目 5.920s 6.540s
      3回目 7.275s 5.801s
      4回目 5.895s 7.147s
      5回目 6.121s 6.278s
      平均 7.513s 6.429s

まとめ

実験結果から、CLIでは、R2へのアップロード速度がS3に比べてとても遅いということがわかりました。
ruby gemからの利用では、R2のダウンロード、アップロードともに速度面での問題はなさそうです。
cloudflareには他にも便利なサービスがあるので、これからWEBサービスを作成する方は、cloudflareを選択肢にいれるのは充分ありだと思います。

Pocket

CONTACT

お問い合わせ・ご依頼はこちらから