Media CDN 提供內容傳遞、快取卸載、來源遮蔽、要求授權,以及與 Google Cloud外部應用程式負載平衡器、記錄和監控平台的整合功能。
Media CDN 提供多種 REST API 資源:
EdgeCacheService
,負責用戶端面向的設定 (TLS、IP 位址)、路由、CDN 設定 (快取模式、TTL、簽署) 和安全政策。EdgeCacheOrigin
,負責為任何以 HTTP 為基礎的來源設定個別來源,以及在內容無法使用或無法存取時重試的條件。例如,在備援影片封裝程式設定中。- (選用)
EdgeCacheKeyset
,其中包含一組用於驗證基礎架構 / CMS 是否已為用戶端要求簽署的公開金鑰。EdgeCacheKeysets
與EdgeCacheService
相關聯,可跨多個服務使用。
以下範例說明這些資源,其中顯示 EdgeCacheService
終止流量,並將流量路由至不同的 EdgeCacheOrigins
。
EdgeCacheService
會終止流量、使用選用的 EdgeCacheKeyset
檢查已簽署的要求,並將要求轉送至三個不同的 EdgeCacheOrigins
。權限
您必須具備建立 Media CDN 資源所需的 Identity and Access Management 權限。Media CDN 有下列預先定義的 IAM 角色:
roles/networkservices.edgeCacheAdmin
roles/networkservices.edgeCacheUser
roles/networkservices.edgeCacheViewer
啟用必要服務
如要設定及部署 Media CDN 服務,您必須為專案啟用 Network Services API 和 Certificate Manager API。
控制台
gcloud
啟用 Network Services API:
gcloud services enable networkservices.googleapis.com
啟用 Certificate Manager API:
gcloud services enable certificatemanager.googleapis.com
如要進一步瞭解如何啟用及停用服務,請參閱「服務使用情形」說明文件。
範例設定
以下資源清單說明 Media CDN 的代表性設定:
EdgeCacheOrigin
:- 以 Cloud Storage 為基礎的來源,如果物件不在 Cloud Storage 中 (例如 HTTP 404) 或遇到 5xx 錯誤,就會重試快取擷取作業,以便使用其他來源 (AWS S3)。
一個
EdgeCacheKeyset
,其中包含:- 兩個 Ed25519 公開金鑰,用於驗證已簽署的要求。
- 在範例設定中,您可以每月輪替金鑰,並保留兩個金鑰供實際工作環境使用。
一個
EdgeCacheService
包含兩個路徑,包括:- 與 Cloud Storage 來源相關聯的資訊清單路徑,快取 TTL 較短。
- 影片片段的路徑,由已簽署要求保護,並與 Cloud Storage 來源相關聯,已設定為快取所有回應。
啟用 IPv4、IPv6、記錄功能 (預設),以及設定代管型 SSL 憑證
以下範例顯示此設定的 gcloud
輸出內容:
gcloud edge-cache origins describe prod-media-origin
id: "2295067926314745283" creationTimestamp: "2019-11-13T09:53:48.757-08:00" name: "prod-media-origin" description: "" originAddress: "gs://bucket_name/" failoverOrigin: "s3-origin" retryConditions: [HTTP_5XX, NOT_FOUND] originProtocol: HTTP2 timeouts: connectTimeout: 5s maxAttemptsTimeout: 10 responseTimeout: 6s
id: "2295067926314745283" creationTimestamp: "2019-11-13T09:53:48.757-08:00" name: "s3-origin" description: "" originAddress: "media.example.com.s3.amazonaws.com" retryConditions: [HTTP_5XX, NOT_FOUND] originProtocol: HTTP2
gcloud edge-cache keysets describe prod-keyset
id: "2295067926314745283" creationTimestamp: "2019-11-13T09:53:48.757-08:00" name: "prod-keyset" publicKeys: - name: "sept-2020-key" value: "DThVLjhAKm3VYOvLBAwFZ5XbjVyF98Ias8NZU0WEM9w=" - name: "aug-2020-key" value: "3nQa82ScYgDDAxJrKCqumSEg60VNODGR5dGAveJWsw4="
gcloud edge-cache services describe prod-media-service
name: "prod-media-service" edgeSslCertificates: - "media-example-com-cert" - "video-serving-example-com-cert" requireTls: true routing: hostRules: - description: "prod hostnames" hosts: - "media.example.com" - "video-serving.example.net" pathMatcher: "routes" pathMatchers: - name: "routes" routeRules: - priority: 1 description: "prod video segments" origin: "prod-media-origin" matchRules: - pathTemplateMatch: "/**.ts" # HLS segments - pathTemplateMatch: "/**.m4s" # DASH / CMAF segments routeAction: cdnPolicy: cacheMode: "FORCE_CACHE_ALL" clientTtl: 3600s defaultTtl: 86400s signedRequestMode: REQUIRE_SIGNATURES signedRequestKeySet: "prod-keyset" headerAction: responseHeadersToAdd: - headerName: cache-status headerValue: "{cdn_cache_status}" - headerName: proxy-status headerValue: "{proxy_status}" - priority: 2 description: "prod manifest endpoints" origin: "prod-media-origin" matchRules: - pathTemplateMatch: "/**.m3u8" # HLS playlists - pathTemplateMatch: "/**.mpd" # DASH manifests routeAction: urlRewrite: pathPrefixRewrite: "/output/manifests" cdnPolicy: cacheMode: "CACHE_ALL_STATIC" clientTtl: 10s defaultTtl: 30s maxTtl: 120s headerAction: responseHeadersToAdd: - headerName: cache-status headerValue: "{cdn_cache_status}" - headerName: proxy-status headerValue: "{proxy_status}" - priority: 3 # catch all routes should be the lowest priority route description: "catch all route" origin: "prod-media-origin" matchRules: - prefixMatch: / headerAction: responseHeadersToAdd: - headerName: cache-status headerValue: "{cdn_cache_status}" - headerName: proxy-status headerValue: "{proxy_status}"
Media CDN 的設定選項
如要設定 Media CDN,您可以使用下列工具:
- Google Cloud 控制台
- 匯入的 YAML 或 JSON 檔案
- 直接使用 API
使用 Google Cloud 主控台
如需在Google Cloud 控制台中設定 Media CDN 的操作說明,請參閱快速入門。
匯入及匯出設定
您可以使用 gcloud CLI 從 YAML 或 JSON 檔案匯出及匯入設定,以便與持續推送系統整合,或使用基礎架構做為程式碼工具。您可以複製設定,在測試環境中測試隔離的服務,然後再更新實際工作環境,並將設定快照納入版本控制。
系統不會匯入僅供輸出的欄位,且在匯入設定時會隱含排除這些欄位。具體情況如下:
- 系統不會匯入 IP 位址,因為 IP 位址是專屬於各項服務。服務無法共用 IP 位址。
- 資源的
selfLink
,以資源名稱為依據。 - 系統自動產生的資源
id
。
如要匯出服務 (EdgeCacheOrigin
或 EdgeCacheKeyset
),請對每項資源使用 export
子指令。舉例來說,如要匯出服務設定:
gcloud edge-cache services export SERVICE_NAME \ --destination=my-service.yaml
Exported [projects/my-project/locations/global/edgeCacheServices/SERVICE_NAME] to 'my-service.yaml'.
同樣地,您可以匯入現有的服務設定,做為新服務或現有服務的更新:
gcloud edge-cache services import new-staging-service \ --source=my-service.yaml
使用非同步 API 作業
根據預設,用於建立、更新或刪除資源的 gcloud
指令會阻斷,且只有在工作完成 (成功或失敗) 時才會傳回。根據預設,REST API 為非同步。
在某些情況下,您可能需要以非同步方式提出這些要求。您可以提供 --async
標記,讓指令立即傳回作業 ID。您可以使用這個 ID 進行檢查和輪詢,確認工作是否成功、是否傳回錯誤,以及錯誤訊息為何。
您可以檢查個別作業 (依據 ID),詳細瞭解錯誤。舉例來說,如果您設定 logConfig.sampleRate
而不設定 logConfig.enable = true
,系統會傳回以下錯誤:
gcloud edge-cache operations describe operation-1611525680496-5b9ac8fbb7f58-90a7a822-f0c1e8c6
done: true error: message: "Logs sample rate must not be specified without enabling logging." name: projects/my-project/locations/global/operations/operation-1611525680496-5b9ac8fbb7f58-90a7a822-f0c1e8c6
如要查看所有近期作業、作業狀態和完成情況,您可以執行下列指令:
gcloud edge-cache operations list
END_TIME ID TARGET DONE operation-1611095421009-5b9486244bf21-cc6b5924-628b8e2a True operation-1611096056610-5b94888273fe6-2da85286-8c810f8e True operation-1611095551517-5b9486a0c251e-c2e1bbbb-de4aa8a5 True