使用 gcloud 工具探索物件儲存空間

本頁面說明如何使用 gcloud 指令列工具在 Cloud Storage 中執行基本工作。

Cloud Storage 產生的費用取決於您耗用的資源。這項快速入門導覽課程使用的 Cloud Storage 資源通常不到 $0.01 美元。

事前準備

  1. Install the Google Cloud CLI.

  2. 設定 gcloud CLI 以使用您的聯合身分。

    詳情請參閱「 使用聯合身分登入 gcloud CLI」。

  3. 如要初始化 gcloud CLI,請執行下列指令:

    gcloud init
  4. Create or select a Trusted Cloud project.

    • Create a Trusted Cloud project:

      gcloud projects create PROJECT_ID

      Replace PROJECT_ID with a name for the Trusted Cloud project you are creating.

    • Select the Trusted Cloud project that you created:

      gcloud config set project PROJECT_ID

      Replace PROJECT_ID with your Trusted Cloud project name.

  5. Verify that billing is enabled for your Trusted Cloud project.

  6. Grant roles to your user account. Run the following command once for each of the following IAM roles: roles/storage.admin

    gcloud projects add-iam-policy-binding PROJECT_ID --member="user:USER_IDENTIFIER" --role=ROLE

    Replace the following:

  7. 建立值區

    「bucket」是 Cloud Storage 中存放資料的基本容器。

    如何建立值區:

    1. 開啟終端機視窗。
    2. 使用 gcloud storage buckets create 指令和專屬名稱來建立值區:

      gcloud storage buckets create gs://my-awesome-bucket/ --uniform-bucket-level-access --location=LOCATION_NAME

      指令使用名稱為「my-awesome-bucket」的值區。您必須選擇自己擁有且全域專屬的值區名稱。

      如果執行成功,指令會傳回下列內容:

      Creating gs://my-awesome-bucket/...

    您已經建立值區,可以開始儲存您的資料!

    Creating gs://my-awesome-bucket/...
    ServiceException: 409 Bucket my-awesome-bucket already exists.
    

    請使用其他值區名稱再試一次。

    將物件上傳到值區

    要上傳到 bucket 的小貓圖片。

    1. 在上方的圖片上按一下滑鼠右鍵,並將圖片儲存至電腦的任意位置 (例如桌面)。

    2. 使用 gcloud storage cp 指令將位於儲存位置的圖片複製到您建立的值區中:

      gcloud storage cp Desktop/kitten.png gs://my-awesome-bucket

      如果執行成功,指令會傳回下列內容:

      Copying file://Desktop/kitten.png [Content-Type=image/png]...
      Uploading   gs://my-awesome-bucket/kitten.png:       0 B/164.3 KiB
      Uploading   gs://my-awesome-bucket/kitten.png:       164.3 KiB/164.3 KiB

      您剛剛在值區中儲存了一個物件。

    從 bucket 下載物件

    1. 使用 gcloud storage cp 指令將儲存在值區中的圖片下載至電腦任意位置 (例如桌面):

      gcloud storage cp gs://my-awesome-bucket/kitten.png Desktop/kitten2.png

      如果執行成功,指令會傳回下列內容:

      Copying gs://my-awesome-bucket/kitten.png...
      Downloading file://Desktop/kitten2.png:               0 B/164.3 KiB
      Downloading file://Desktop/kitten2.png:               164.3 KiB/164.3 KiB

      您剛剛下載了值區中的物件。

    將物件複製到值區中的資料夾

    1. 使用 gcloud storage cp 指令建立資料夾,並將圖片複製到資料夾中:

      gcloud storage cp gs://my-awesome-bucket/kitten.png gs://my-awesome-bucket/just-a-folder/kitten3.png

      如果執行成功,指令會傳回下列內容:

      Copying gs://my-awesome-bucket/kitten.png [Content-Type=image/png]...
      Copying     ...my-awesome-bucket/just-a-folder/kitten3.png: 164.3 KiB/164.3 KiB

      您剛剛已將圖片複製到值區中的新資料夾。

    列出值區或資料夾的內容

    1. 使用 gcloud storage ls 指令列出值區頂層的內容:

      gcloud storage ls gs://my-awesome-bucket

      如果成功,指令會傳回類似以下的訊息:

      gs://my-awesome-bucket/kitten.png
      gs://my-awesome-bucket/just-a-folder/

      您剛剛看到了值區頂層的內容。

    列出物件的詳細資料

    1. 使用帶有 --long 旗標的 gcloud storage ls 指令,取得圖片的一些詳細資料:

      gcloud storage ls gs://my-awesome-bucket/kitten.png --long

      如果成功,指令會傳回類似以下的訊息:

      2638  2016-02-26T23:05:14Z  gs://my-awesome-bucket/kitten.png
      TOTAL: 1 objects, 168243.2 bytes (164.3 KiB)

      您剛剛取得了圖片大小和建立日期的相關資訊。

    將物件設為可公開存取

    1. 使用 gcloud storage buckets add-iam-policy-binding 指令授予權限,讓所有使用者都能讀取值區圖片:

      gcloud storage buckets add-iam-policy-binding gs://my-awesome-bucket --member=allUsers --role=roles/storage.objectViewer

      如果回應包含下列內容,表示指令成功:

      bindings:
        - members:
          - allUsers
          role: roles/storage.objectViewer
      

      現在任何人都可以取得您的圖片。

    2. 如要移除這個存取權,請使用下列指令:

      gcloud storage buckets remove-iam-policy-binding gs://my-awesome-bucket --member=allUsers --role=roles/storage.objectViewer

      如果系統未傳回錯誤,即代表指令執行成功。

      您已移除值區中圖片的公開存取權。

    向使用者提供值區的存取權

    1. 使用 gcloud storage buckets add-iam-policy-binding 指令授予特定電子郵件地址在值區中新增物件的權限:

      gcloud storage buckets add-iam-policy-binding gs://my-awesome-bucket --member=user:example-service-account@example-project.s3ns-system.iam.gserviceaccount.com --role=roles/storage.objectCreator

      如果回應包含下列內容,表示指令成功:

      bindings:
        - members:
          - user:example-service-account@example-project.s3ns-system.iam.gserviceaccount.com
          role: roles/storage.objectCreator
      

      現在其他使用者可以將項目加入值區。

    2. 如要移除這個權限,請使用以下指令:

      gcloud storage buckets remove-iam-policy-binding gs://my-awesome-bucket --member=user:example-service-account@example-project.s3ns-system.iam.gserviceaccount.com --role=roles/storage.objectCreator

      如果系統未傳回錯誤,即代表指令執行成功。

      您剛剛移除了使用者對這個值區的存取權。

    刪除物件

    1. 使用 gcloud storage rm 指令刪除其中一張圖片:

      gcloud storage rm gs://my-awesome-bucket/kitten.png

      如果執行成功,指令會傳回下列內容:

      Removing gs://my-awesome-bucket/kitten.png...

      Cloud Storage 中已不存在這個圖片副本 (雖然您在資料夾 just-a-folder/ 中產生的副本仍然存在)。

    清除所用資源

    如要避免系統向您的 Trusted Cloud 帳戶收取本頁面所用資源的費用,請刪除含有這些資源的 Trusted Cloud 專案。

    1. 開啟終端機視窗 (如果尚未開啟)。
    2. 使用 gcloud storage rm 指令並加上 --recursive 標記,刪除值區及其中的內容:

      gcloud storage rm gs://my-awesome-bucket --recursive

      如果成功,指令會傳回類似以下的訊息:

      Removing gs://my-awesome-bucket/just-a-folder/cloud-storage.logo.png#1456530077282000...
      Removing gs://my-awesome-bucket/...

      您刪除了值區和其內容。

    後續步驟