前言
在資料工程師的工作中,常會使用雲端 Google Cloud Platform (GCP) 的 Google Cloud Storage (GCS) 作為 Data Lake 儲存一些非結構化資料,而操作 GCS 的指令我都是用 gsutil 來進行,但近期發現有另一個指令是 gcloud storage ,所以這篇文章記錄我針對這兩種指令實測後發現的差異,以及官方文件的建議。
什麼是 gsutil 指令?
gsutil是一個 Python application,主要用來讓使用者可以透過 Command Line 操作 Cloud Storage,操作包含:- Creating and deleting buckets.
- Uploading, downloading, and deleting objects.
- Listing buckets and objects.
- Moving, copying, and renaming objects.
- Editing object and bucket ACLs.
什麼是 gcloud 指令?
gcloud是 GCP 主要的 CLI 工具,用途更廣不限於 GCS,操作包含:- Manages authentication, local configuration and developer workflow.
- Interactions with the Google Cloud APIs.
官方建議使用 gcloud storage 取代 gsutil
官方已經 不推薦使用 gsutil,一律建議使用 gcloud storage
Should you use gsutil?
Generally, you should use
gcloud storagecommands instead ofgsutilcommands:

進到
gsutil頁面也都會請使用gcloud storage的提醒gsutil is not the recommended CLI for Cloud Storage. Use gcloud storage commands in the Google Cloud CLI instead.

官方實測數據證實,gcloud storage 有顯著的效率提升
When transferring 100 files that were 100MB in size, gcloud storage is 79% faster than gsutil on download and 33% faster on upload.
With a 10GB file, gcloud storage is 94% faster than gsutil on download and 57% faster on upload.
自行實測結果
實測環境
- 直接利用 GCP Cloud Shell 去執行「複製 GCS Bucket 檔案」的指令
- 實測的指令都執行二重複,避免結果因短暫的網速或系統狀況影響
- 採用 Linux 內建的 time 指令來評估,主要看的 output 是 real 的秒數
gsutilversion:- 5.27
gcloudversion:- Google Cloud SDK 475.0.0
- alpha 2024.05.03
- core 2024.05.03
實測結果表
| 檔案大小 \ 平均指令花費時間 | gsutil cp (sec) | gcloud storage cp (sec) | gcloud storage速度提升(%) |
| 小檔案(249 MiB) | 5.4 | 3.6 | + 40% |
| 大檔案(1.0 GiB) | 23.6 | 6.3 | + 74% |
以下是我詳細的實測過程
實測 1:複製一個 249.1 MiB 小型檔案
gsutil cp: 5.4 sec
$ time gsutil cp gs://my_bucket/small_size.avro .
Copying gs://my_bucket/small_size.avro...
\ [1 files][249.1 MiB/249.1 MiB]
Operation completed over 1 objects/249.1 MiB.
real 0m5.399s
user 0m4.935s
sys 0m2.090s
$ time gsutil cp gs://my_bucket/small_size.avro .
Copying gs://my_bucket/small_size.avro...
\ [1 files][249.1 MiB/249.1 MiB]
Operation completed over 1 objects/249.1 MiB.
real 0m5.400s
user 0m5.009s
sys 0m2.031s
gcloud storage cp: 3.2 sec ( Speed Up 40% )
$ time gcloud storage cp gs://my_bucket/small_size.avro .
Copying gs://my_bucket/small_size.avro to file://./small_size.avro
Completed files 1/1 | 249.1MiB/249.1MiB
Average throughput: 266.7MiB/s
real 0m3.961s
user 0m2.790s
sys 0m1.228s
$ time gcloud storage cp gs://my_bucket/small_size.avro .
Copying gs://my_bucket/small_size.avro to file://./small_size.avro
Completed files 1/1 | 249.1MiB/249.1MiB
Average throughput: 275.9MiB/s
real 0m3.271s
user 0m2.771s
sys 0m1.189s
實測 2:複製一個 1.0 GiB 大型檔案
gsutil cp: 24 sec
- throughput: 56 MiB/s
$ time gsutil cp gs://my_bucket/large_size.avro .
Copying gs://my_bucket/large_size.avro...
| [1 files][ 1.0 GiB/ 1.0 GiB] 27.2 MiB/s
Operation completed over 1 objects/1.0 GiB.
real 0m24.059s
user 0m18.037s
sys 0m10.691s
$ time gsutil cp gs://my_bucket/large_size.avro .
Copying gs://my_bucket/large_size.avro...
\ [1 files][ 1.0 GiB/ 1.0 GiB] 31.0 MiB/s
Operation completed over 1 objects/1.0 GiB.
real 0m23.220s
user 0m18.151s
sys 0m10.641s
gcloud storage cp: 6.3 sec ( Speed Up 74% )
- Average throughput: 313.1MiB/s
$ time gcloud storage cp gs://my_bucket/large_size.avro .
Copying gs://my_bucket/large_size.avro to file://./large_size.avro
Completed files 1/1 | 1.0GiB/1.0GiB | 305.9MiB/s
Average throughput: 307.0MiB/s
real 0m6.340s
user 0m5.446s
sys 0m3.770s
$ time gcloud storage cp gs://my_bucket/large_size.avro .
Copying gs://my_bucket/large_size.avro to file://./large_size.avro
Completed files 1/1 | 1.0GiB/1.0GiB | 311.8MiB/s
Average throughput: 313.1MiB/s
real 0m6.351s
user 0m5.486s
sys 0m3.675s
總結
除了官方的實測外,我個人實測也是 gcloud storage 的效率明顯高出 gsutil 許多,而且複製越大的檔案效率提升越明顯!
指令更改的幅度沒有很大,所以如果是還在使用 gsutil 來操作 GCS 的話,我還滿推薦可以盡快轉換成 gcloud storage,輕鬆無痛提升大約 70% 的 Data Transfer 效率。
這次主要測試了 200 MB 和 1 GB 單一檔案的複製速度,如果是多個超小型檔案的話(例如每個檔案只有 2 KB 之類的),gsutil 使用 -m 參數 (multi-threading/multi-processing) 在 cp 和 rm 的速度是比 gcloud storage 要快的唷!(實測表格可以參考文末補充資料)
所以我個人的總結是
檔案大小在 MB 以上的檔案使用
gcloud storage cp
MB 以下的檔案使用
gsutil -m cp和 rm
另外要注意的是,有些 gcloud 指令版本可能比較舊,會出現找不到指令的問題,可能要改用 gcloud alpha storage ,雖然是堪用,但我實測上感覺有不少 bug 存在(像是 gcloud alpha storage cp 時資料夾亂掉、gcloud 指令莫名 crashed 等),所以如果是 alpha 版本的話要小心使用~
補充資料
- 其他關於
gcloud storage的資訊- 如果是比較舊的 gcloud 版本,可能會需要改成
gcloud alpha storage gcloud storage預設為 Parallelism,不是 Sequential
- 如果是比較舊的 gcloud 版本,可能會需要改成
gcloud storage實用參數--user-output-enabled=false- 可以不顯示
gcloud的輸出 - 像是使用在 Airflow Log 就很實用,因為當檔案很多時,如果 log 都 print 出來 Airflow UI 會跑不出來
- 可以不顯示
--skip-unsupported (-U)- 可以略過不支援的 object types
- 有時候可以避免一些檔案異常造成指令執行過程發生意外狀況
- 國外網友實測,換指令後他們的 Data Transfer 效率提升了 300%!
- 300% improvement in data transfer into our buckets by simply just moving over from gsutil cp to gcloud storage cp Our object write throughput shot up as well by 265%!
- 連結:From gsutil to gcloud storage: Introducing Simplicity and Improved Performance
- 超小型檔案(2 KiB)的複製 (cp) 速度實測
| 檔案數量 \ 平均指令花費時間 | gsutil -m cp (sec) | gcloud storage cp (sec) |
| 1 | 1.9 | 2.3 |
| 187 | 9.9 | 10.5 |
- 超小型檔案(2 KiB)的移除 (rm) 速度實測
| 檔案數量 \ 平均指令花費時間 | gsutil -m rm (sec) | gcloud storage rm (sec) |
| 1 | 2.0 | 2.4 |
| 48 | 2.4 | 4.4 |
| 187 | 3.2 | 9.5 |
| 504 | 4.6 | 21.4 |
| 1400 | 8.5 | 54.4 |