iT邦幫忙

2025 iThome 鐵人賽

DAY 23
0
自我挑戰組

一路side project - 學習筆記系列 第 23

[Day 23] x Cloud Storage CLI 快速入門:從建立儲存桶到公開檔案 [學習筆記]

  • 分享至 

  • xImage
  •  

今天來看 Set Up an App Dev Environment on Google Cloud 這course 的 Cloud Storage: Qwik Start - CLI/SDK,用指令(Cloud Shell/CLI)完成「把檔案放到 GCS、取回、整理、公開」的整個基本流程, 我們開始 ~~ 。


Cloud Storage: Qwik Start - CLI/SDK

  • Bucket = 儲存空間的最上層容器(Global 且 unique名稱)。
  • Object = 你實際存放的檔案(圖片、CSV、備份等)。
  • 會用 gcloud storage(新一代 CLI)與一次 gsutil(舊工具)完成:建立 bucket → 上傳檔案 → 下載/複製/列出 → 設定公開讀取。

Task 1. Create a bucket

  • 在專案底下 建立bucket (buckets create)

     <YOUR_BUCKET_NAME> 為全域命名空間且對外可見,所以有嚴格的命名原則。

gcloud storage buckets create gs://<YOUR-BUCKET-NAME>
  • 用這command建立的bucket 內部都是default setting
  • 不可同名,同名會跳 already exists

bucket 命名原則

  • Do not include sensitive information in the bucket name, because the bucket namespace is global and publicly visible.
  • Bucket names must contain only lowercase letters, numbers, dashes (-), underscores (_), and dots (.). Names containing dots require verification.
  • Bucket names must start and end with a number or letter.
  • Bucket names must contain 3 to 63 characters. Names containing dots can contain up to 222 characters, but each dot-separated component can be no longer than 63 characters.
  • Bucket names cannot be represented as an IP address in dotted-decimal notation (for example, 192.168.5.4).
  • Bucket names cannot begin with the "goog" prefix.
  • Bucket names cannot contain "google" or close misspellings of "google".
  • Also, for DNS compliance and future compatibility, you should not use underscores (_) or have a period adjacent to another period or dash. For example, ".." or "-." or ".-" are not valid in DNS names.

Task 2. Upload an object into your bucket

  • 先抓一張測試圖片,下面再用 cp 上傳到 bucket (本機→雲端)
  1. Download this image (ada.jpg) into your bucket

    curl https://upload.wikimedia.org/wikipedia/commons/thumb/a/a4/Ada_Lovelace_portrait.jpg/800px-Ada_Lovelace_portrait.jpg --output ada.jpg
    
  2. Use the gcloud storage cp command to upload the image from the location where you saved it to the bucket you created:

    gcloud storage cp ada.jpg gs://YOUR-BUCKET-NAME
    
  3. Remove the downloaded image:

    rm ada.jpg
    

    https://ithelp.ithome.com.tw/upload/images/20251006/20154764xJEyJsxeex.png

Task 3. Download an object from your bucket

  • 從 GCS 把剛剛上傳的檔案拉回本機 (雲端→本機)

  • gcloud storage cp: download the image you stored in your bucket

    gcloud storage cp -r gs://YOUR-BUCKET-NAME/ada.jpg .
    

Task 4. Copy an object to a folder in the bucket

  • gcloud storage cp : create a folder called image-folder and copy the image (ada.jpg) into it

    gcloud storage cp gs://YOUR-BUCKET-NAME/ada.jpg gs://YOUR-BUCKET-NAME/image-folder/
    
    • 在 bucket 內用「類資料夾」前綴(prefix)整理檔案

    補充:
    在 GCS 裡的「資料夾」其實不是真正的檔案系統資料夾,而是物件名稱的字首(prefix)加上慣例的斜線 /。我們看到的資料夾,只是用「名字長得像路徑」來視覺化分組, 藉由命名來分組、列出、搬移與套規則,但底層沒有真正的目錄樹。

    • e.g.
      move “folder”
      (本質是改名=改物件 key)
      gcloud storage mv gs://my-bucket/ada.jpg gs://my-bucket/image-folder/ada.jpg
      (底層是 copy+delete 的概念,但 mv 幫你處理)
    • GCS 不會真的建立目錄結構,但因為名字裡包含 /,Cloud Console 與 ls 會把同一個前綴(例如 image-folder/)當成「一個資料夾」來展示。
      可以看到 image-folder 對 Cloud Console 是 imgae ;image-folder/是資料夾
      https://ithelp.ithome.com.tw/upload/images/20251006/20154764NOzFAuto9U.png
      https://ithelp.ithome.com.tw/upload/images/20251006/20154764GIs2ChqmWP.png

Task 5. List contents of a bucket or folder

  • 列出 bucket 內有哪些物件或前綴

  • gcloud storage ls : list the contents of the bucket

    gcloud storage ls gs://YOUR-BUCKET-NAME
    

    https://ithelp.ithome.com.tw/upload/images/20251006/2015476402IVLA29Sa.png

Task 6. List details for an object

  • 顯示物件細節(大小、更新時間、儲存級別等)

  • gcloud storage ls :  with the -l flag to get some details about the image file you uploaded to your bucket

    gcloud storage ls -l gs://YOUR-BUCKET-NAME/ada.jpg
    

    https://ithelp.ithome.com.tw/upload/images/20251006/20154764786ta1KOGt.png

Task 7. Make your object publicly accessible

  • 單一物件設成「任何人可讀取」的公開 URL。

  • 安全觀念:只讓需要公開的特定物件公開;避免整桶公開。

  • gsutil acl ch : grant all users read permission for the object stored in your bucket

    gsutil acl ch -u AllUsers:R gs://YOUR-BUCKET-NAME/ada.jpg
    
    • -u:針對「user / 單一主體」新增授權
      (也可用 email、服務帳號、群組;這裡用特殊主體 AllUsers)。
    • AllUsers:R:主體 + 權限。R=Read
    • gs://.../ada.jpg:目標是物件而非整個 bucket。

    小提醒:R/W/O 是物件 ACL 的傳統權限碼
    R(Reader):可讀。
    W(Writer):可寫(較少用在物件 ACL)。
    O(Owner):擁有者,可改 ACL、刪除等。

Validate that your image is publicly available.

  • Go to Navigation menu > Cloud Storage, then click on the name of your bucket.

可以看到我們照片欄位的 Public link 旁出現 Copy URL 按鈕,複製URL, 開啟新的瀏覽器貼上可看到照片。
https://ithelp.ithome.com.tw/upload/images/20251006/201547649nnrNwe8rI.png

https://ithelp.ithome.com.tw/upload/images/20251006/20154764zPyMYaISQn.png

Task 8. Remove public access

  1. Remove this permission

    刪除物件 ada.jpg 上 ACL 的 AllUsers 身份,因此不再「任何人」可讀

    gsutil acl ch -d AllUsers gs://YOUR-BUCKET-NAME/ada.jpg
    
    • -d:delete 指定主體在 ACL 中的授權。
  2. 驗證方式: 在 Console 重新整理,Public access 會變成 Not public;若用原公開連結打開,通常會變成Access Denied
    https://ithelp.ithome.com.tw/upload/images/20251006/201547646WeVgGJQN4.png

Delete objects

  1. gcloud storage rm :delete an object - the image file in your bucket 

    gcloud storage rm gs://YOUR-BUCKET-NAME/ada.jpg
    
    • 刪除 bucket 根目錄下那個 ada.jpg
    • 若要刪除一整個前綴(「資料夾」): 改成 gs://YOUR-BUCKET-NAME/image-folder/**
  2. Refresh the console. The copy of the image file is no longer stored on Cloud Storage (though the copy you made in the image-folder/ folder still exists).


上一篇
[Day 22]跟著 Lab 練習用 Compute Engine 打造可自動擴展的網站服務 (Host a Web App on Google Cloud ) [學習筆記]
下一篇
[Day 24] GCP x IAM 權限控管 [學習筆記]
系列文
一路side project - 學習筆記25
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言