跳至主要内容

[GCP] Google Cloud Storage API

  • 如果該檔案要作為 Cloud Speech 使用,不可壓縮為 gzip。

概念

  • Bucket
    • 根據需求(存取頻率)選擇對應的 classes,這會影響到價格,但不會影響到實際操作的體驗(例如,API、速度)

Node.js Client

Google Cloud Snippets @ PJCHENder Github

檢視並篩選檔案

getFiles @ Google Cloud Snippets > PJCHENder Github

gsutil 工具

warning

提醒:使用 gsutil 前需要先安裝 google cloud sdk 並進行 gCloud init

# 列出 bucket 內的資料
$ gsutil ls gs://bucket
$ gsutil ls gs://pjchender_cloud_storage/2020

上傳物件(檔案)

cp @ gsutil

# 上傳單一檔案
# -n: No-clobber,檔案存在時不會覆蓋重傳
# -r: 複製該資料夾內的資料夾
$ gsutil cp [LOCAL_OBJECT_LOCATION] gs://[DESTINATION_BUCKET_NAME]/

# 上傳整個資料夾內的檔案(有時候會掉檔案)
$ gsutil -m cp -r -n [LOCAL_DIR] gs://pjchender_cloud_storage/

# 進到該資料夾後輸入(比較保險)
$ gsutil -m cp -r -n . gs://pjchender_cloud_storage/foobar/ # 記得要先在 GCP 上建立資料夾

移動物件(檔案)/修改檔名

mv @ gsutil

# -p: preserve ACLs
# -m: multi-threaded/multi-processing

# 將某一資料夾搬移到某一資料夾內
$ gsutil -m mv gs://my_bucket/old_prefix gs://my_bucket/new_prefix

$ gsutil -m mv gs://pjchender_cloud_storage/foobar gs://pjchender_cloud_storage/2014

下載物件(檔案)

# 下載單一檔案
$ gsutil cp gs://pjchender_cloud_storage/[OBJECT_NAME] [OBJECT_DESTINATION]

# 下載整個資料夾中的所有物件(檔案)
$ gsutil -m cp -r gs://pjchender_cloud_storage/foo [LOCAL_DIR]

# 下載所有某類型的檔案
$ gsutil cp 'gs://my-bucket/*.txt' .
warning

提醒:當使用 wildcard(*)時,zsh 會先進行處理,導致出現 Not Found 的錯誤,因此需要使用單引號把路徑包起來

檢視使用的容量

# 檢視某一 bucket 使用的總容量
gsutil du -sh gs://pjchender_cloud_storage

檢查檔案數量

gsutil ls "gs://pjchender_cloud_storage" | grep -v "/$" | wc -l

價格

Cloud Storage Pricing

除了下述費用之外,還有其他額外的費用,需進一步檢視 Cloud Storage Pricing 的說明。

資料儲存費用(Data Storage)

Imgur

操作費用(Operation)

Cloud Storage Pricing 中清楚說明 Class A Operations, Class B Operations 分別指的是什麼,這些操作每個月都會提供免費的額度,超過的話則會收費。一般來說,「刪除」的動作是不用收費的;「下載(GET)」的動作比較便宜,屬於 Class B(每月免費 50,000 次);但是「新增」、「複製」、「更新」、「列出清單(list)」鑿是屬於 Class A(每月免費 5,000 次)。

Imgur

Imgur

中英對照

英文中文
bucket值區

Q & A