將後端 bucket 設為 Cloud CDN 的來源
本快速入門導覽課程說明如何使用 Google Cloud 控制台,將後端 bucket 設為 Cloud CDN 的來源。
如要直接在 Google Cloud 控制台中,按照這項工作的逐步指南操作,請按一下「Guide me」(逐步引導):
事前準備
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
如要使用現有專案進行本指南中的操作,請確認您具有所需的權限。如果您建立新專案,則已具備必要權限。
Verify that billing is enabled for your Google Cloud project.
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
如要使用現有專案進行本指南中的操作,請確認您具有所需的權限。如果您建立新專案,則已具備必要權限。
Verify that billing is enabled for your Google Cloud project.
必要的角色
請確認您具備下列角色,可完成本教學課程中的工作:
Storage 管理員角色 (
roles/compute.storageAdmin)Compute 負載平衡器管理員角色 (
roles/compute.loadBalancerAdmin)
建立 Cloud Storage 值區
「bucket」是 Cloud Storage 中存放資料的基本容器。
如要建立 bucket,請按照下列步驟操作:
-
前往 Google Cloud 控制台的「Cloud Storage bucket」頁面。
-
點選 「Create」(建立)。
操作示範 。 -
在
「Name your bucket」(為 bucket 命名) 部分,為 bucket 輸入不重複的名稱。按一下「Continue」(繼續)。
- 在
「Choose where to store your data」(選擇資料的儲存位置) 部分,執行下列操作:- 將「Location type」(位置類型) 設為「Region」(區域),然後從清單中選取「
us-east1 (South Carolina)」。 - 按一下「Continue」(繼續)。
- 將「Location type」(位置類型) 設為「Region」(區域),然後從清單中選取「
- 在
「Choose a storage class for your data」(為資料選擇儲存空間級別) 部分,執行下列操作:- 依序選取「Set a default class」(設定預設級別) 和「Standard」。
- 按一下「Continue」(繼續)。
- 在
「Choose how to control access to objects」(選取如何控制物件的存取權) 部分,執行下列操作:- 取消勾選「Enforce public access prevention on this bucke」(強制禁止公開存取這個 bucket) 核取方塊。
- 將「Access control」(存取控管) 設為「Uniform」(統一)。
- 按一下「Continue」(繼續)。
- 在
「Choose how to protect object data」(選擇保護物件資料的方式) 部分,保留已選取的預設值,然後按一下「Create」(建立) 。
將物件上傳至 bucket
將下列圖片儲存至您的裝置:
- 在「Bucket details」(bucket 詳細資料) 頁面的
「Objects」(物件) 分頁中,按一下「Upload files」(上傳檔案)。 - 在檔案對話方塊中,選取您下載的檔案。
將 bucket 設為公開
- 在「Bucket details」(bucket 詳細資料) 頁面上,按一下
「Permissions」(權限) 分頁標籤。 -
確認檢視方式已設為
「View by principals」(按照主體查看) ,然後點選「Grant access」(授予存取權) 。隨即會顯示「Add principals」(新增主體) 窗格。
- 在
「New principals」(新增主體) 欄位中輸入allUsers。 - 在
「Select a role」(選取角色) 清單中,依序選取「Cloud Storage」>「Storage Object Viewer」(Storage 物件檢視者)。 - 按一下
「Save」(儲存) 。 - 在「Are you sure you want to make this resource public?」(確定要將這項資源設為公開嗎?) 頁面上,按一下「Allow public access」(允許公開存取)。
-
如要確認物件是否已公開分享,請點選
「Objects」(物件) 分頁標籤,回到物件清單。物件的「Public access」(公開存取) 欄應會顯示「Public to internet」(在網際網路上公開)。按下「Copy URL」(複製網址) 按鈕會產生可分享的網址,範例如下:https://storage.googleapis.com/YOUR_BUCKET_NAME/kitten.png
建立後端 bucket
如要建立後端 bucket 和外部應用程式負載平衡器,請按照下列步驟操作:
前往 Google Cloud 控制台的「Cloud CDN」頁面。
按一下
「Add origin」(新增來源) 。在「New origin」(新增來源) 頁面的
「Origin name」(來源名稱) 中,輸入來源名稱。將
「Origin type」(來源類型) 設為「Backend bucket」(後端 bucket)。將
「Define your backend bucket」(定義後端 bucket) 設為「New backend bucket」(新增後端 bucket)。在「Cloud Storage bucket」欄位中,按一下「Browse」(瀏覽)。
選取您先前建立的 Cloud Storage bucket,然後依序點選「Select」(選取) 和「Next」(下一步)。
在
「Attach a load balancer」(連接負載平衡器) 部分,選取「Create new load balancer for me」(為我建立新的負載平衡器),然後輸入負載平衡器的名稱,再點選「Next」(下一步)。「Cache performance」(快取效能) 維持設為選取的預設值,然後按一下
「Done」(完成) 。新的來源會顯示在來源清單中。來源可能需要幾分鐘才會建立完畢。請等到顯示來源建立完畢的通知後,再繼續執行下一步。
測試 Cloud CDN
-
前往 Google Cloud 控制台的「Load balancing」(負載平衡) 頁面。
按一下您先前建立的負載平衡器
名稱 。隨即會顯示「Load balancer details」(負載平衡器詳細資料) 頁面。
記下
「IP:Port」 底下顯示的 IPv4 位址。在新的瀏覽器分頁中輸入以下網址 (須包含負載平衡器的 IP 位址),測試 Cloud CDN:
http://IP_ADDRESS/kitten.png瀏覽器應該會顯示含有您所上傳圖片的頁面。
重新整理頁面多次。
回到「Load balancer details」(負載平衡器詳細資料) 頁面,然後按一下「Monitoring」(監控)。
稍候幾分鐘,再觀察提供的流量。
直接從後端 bucket 提供的流量會帶有「SERVED_FROM_BACKEND_BUCKET」標籤。從 Cloud CDN 提供的流量則帶有「SERVED_FROM_CACHE」標籤。詳情請參閱「監控指標」。
清理
為了避免系統向您的 Google Cloud 帳戶收取本頁面所用資源的費用,請按照下列步驟操作。
刪除負載平衡器和後端 bucket:
-
前往 Google Cloud 控制台的「Load balancing」(負載平衡) 頁面。
勾選您所建立負載平衡器旁的核取方塊,然後按一下「Delete」(刪除)。
在開啟的頁面中,勾選您所建立後端 bucket 旁的核取方塊,然後確認要刪除負載平衡器與所選資源。
-
刪除 Cloud Storage bucket:
-
前往 Google Cloud 控制台的「Cloud Storage bucket」頁面。
- 勾選您所建立 bucket 旁的核取方塊,然後按一下「Delete」(刪除)。
- 在「Delete bucket」(刪除 bucket) 對話方塊中,確認要刪除該 bucket,然後按一下「Delete」(刪除)。
-
前往 Google Cloud 控制台的「Cloud Storage bucket」頁面。
後續步驟
後續課程與相關資源: