Go 1.11 已終止支援,並將於 2026 年 1 月 31 日
淘汰。淘汰後,您將無法部署 Go 1.11 應用程式,即使貴機構先前使用機構政策重新啟用舊版執行階段的部署作業也一樣。現有的 Go 1.11 應用程式在
淘汰日期過後,仍會繼續執行並接收流量。建議您
遷移至最新支援的 Go 版本。
儲存和提供靜態檔案
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
除了處理動態要求以外,應用程式通常需要提供 JavaScript、圖片和 CSS 等靜態檔案。標準環境中的應用程式可以透過 Cloud Storage 等 Google Cloud 選項提供靜態檔案,也可以直接提供檔案,或是使用第三方內容傳遞聯播網 (CDN)。
Google Cloud 提供
免費方案,因此您在 Google Cloud 中託管靜態網站的費用可能會比使用傳統的主機供應商來得更低。
從 Cloud Storage 提供檔案
您可以將動態網頁應用程式的靜態資產託管於 Cloud Storage。使用 Cloud Storage (而不直接從應用程式提供檔案) 的優點如下:
- Cloud Storage 在本質上就是當成內容傳遞聯播網使用。根據預設,凡是可公開讀取的物件在全域 Cloud Storage 網路中都能夠快取,因此不需要特別進行設定。
- 無須向 Cloud Storage 提供靜態資產,因此可減輕應用程式的負載。這種做法可能有助大幅降低應用程式的執行成本,實際情況視您擁有的靜態資產數量以及存取的頻率而定。
- 如果使用 Cloud Storage 存取內容,頻寬費用通常較低。
您可以使用 Google Cloud CLI 或 Cloud Storage API 將資產上傳至 Cloud Storage。
Google Cloud 用戶端程式庫可以向 Cloud Storage 提供您慣用的 Go 1.11 用戶端,以便透過 App Engine 應用程式使用 Cloud Storage 來儲存及擷取資料。
從 Cloud Storage 值區提供資產的範例
這個簡單的範例會建立 Cloud Storage 儲存桶,並使用 gcloud CLI 上傳靜態資產:
建立值區。用專案 ID 來當做值區的名稱是很常見的做法,但您不一定採用這種命名方式。值區名稱不得重複。
gcloud storage buckets create gs://<var>YOUR_BUCKET_NAME</var>
設定 ACL,針對值區中的項目授予讀取權限。
gcloud storage buckets add-iam-policy-binding gs://<var>YOUR_BUCKET_NAME</var> --member=allUsers --role=roles/storage.objectViewer
將項目上傳到值區。使用 rsync
指令通常可以最快速且最輕鬆地上傳及更新資產。您也可以使用 cp
。
gcloud storage rsync ./static gs://<var>YOUR_BUCKET_NAME</var>/static --recursive
您現在可以透過 https://storage.googleapis.com/<var>YOUR_BUCKET_NAME</var>/static/...
來存取靜態資產。
如要進一步瞭解如何使用 Cloud Storage 來提供靜態資產,包括從自訂網域名稱提供資產的方式,請參閱如何託管靜態網站。
透過其他 Google Cloud 服務提供檔案
您也可以選擇使用 Cloud CDN 或其他 Google Cloud 儲存空間服務。
直接透過應用程式提供檔案
如要在標準環境中為 Go 1.11 提供靜態檔案,請使用 static_dir
或 static_files
元素,在 app.yaml
檔案中定義處理常式。
靜態檔案或靜態目錄中的內容不會受到 app.yaml
檔案中的縮放設定影響。對靜態檔案或靜態目錄的要求會直接由 App Engine 基礎架構處理,不會傳送至應用程式的語言執行階段。
設定靜態檔案處理常式
如要將應用程式設為從 /static
網址提供 ./public
目錄,請在 app.yaml
檔案中定義處理常式。
以下示範如何提供範例應用程式 ./public
目錄中的靜態檔案。這個應用程式的 index.html
頁面範本會指示瀏覽器載入 main.css
檔案,例如:
<link type="text/css" rel="stylesheet" href="/static/css/main.css">
./public
目錄是在專案 app.yaml
檔案的 static_dir
元素中定義:
handlers:
- url: /favicon\.ico
static_files: favicon.ico
upload: favicon\.ico
- url: /static
static_dir: public
- url: /.*
secure: always
redirect_http_response_code: 301
script: auto
上述範例中的 handlers
部分會處理三個網址模式:
/favicon.ico 處理常式會將專門針對 /favicon.ico
的要求,對應至應用程式根目錄中名為 favicon.ico
的檔案。
/static 處理程序會將開頭為 /static
的網址要求對應至 當 App Engine 收到以 /static
開頭的網址要求時,會將路徑的其餘部分對應至 ./public
目錄中的檔案。如果在目錄中找到適當的檔案,系統會將該檔案的內容傳回給用戶端。
/.* 處理程序會比對所有其他網址,並將這些網址導向至您的應用程式。
系統會依照模式在 app.yaml
中的出現順序進行測試,因此應先定義靜態檔案的模式,再定義 /.*
模式。詳情請參閱 app.yaml
參考資料。
從第三方內容傳遞聯播網提供
您可以使用任何外部第三方 CDN 來提供靜態檔案以及快取動態要求,但應用程式的延遲時間和成本可能會隨之增加。
建議您使用支援 CDN 互連網路的第三方 CDN,以獲得更好的效能。
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-09-04 (世界標準時間)。
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["難以理解","hardToUnderstand","thumb-down"],["資訊或程式碼範例有誤","incorrectInformationOrSampleCode","thumb-down"],["缺少我需要的資訊/範例","missingTheInformationSamplesINeed","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-09-04 (世界標準時間)。"],[[["\u003cp\u003eGoogle Cloud offers multiple options for serving static files, including Cloud Storage, direct serving, and third-party content delivery networks (CDNs), which can be used in addition to handling dynamic requests.\u003c/p\u003e\n"],["\u003cp\u003eCloud Storage functions as a content delivery network (CDN) by default, caching publicly readable objects globally, reducing app load, and often decreasing bandwidth costs.\u003c/p\u003e\n"],["\u003cp\u003eStatic files can be directly served from an App Engine app by defining handlers in the \u003ccode\u003eapp.yaml\u003c/code\u003e file using \u003ccode\u003estatic_dir\u003c/code\u003e or \u003ccode\u003estatic_files\u003c/code\u003e, with requests being handled directly by the App Engine infrastructure.\u003c/p\u003e\n"],["\u003cp\u003eConfiguring static file handlers in \u003ccode\u003eapp.yaml\u003c/code\u003e involves mapping URL patterns to specific files or directories, such as the example of mapping requests beginning with \u003ccode\u003e/static\u003c/code\u003e to the \u003ccode\u003e./public\u003c/code\u003e directory.\u003c/p\u003e\n"],["\u003cp\u003eUsing a third-party CDN is an option for serving static files, but using a CDN that supports CDN Interconnect is suggested for better performance, and it's important to consider potential increased latency and costs.\u003c/p\u003e\n"]]],[],null,["# Storing and Serving Static Files\n\nApplications often need to serve static files such as JavaScript, images, and\nCSS in addition to handling dynamic requests. Apps in the standard\nenvironment can serve static files from a Google Cloud option\nlike Cloud Storage, serve them directly, or use a third-party content\ndelivery network (CDN).\nHosting your static site on Google Cloud can cost less than using a traditional hosting provider, as Google Cloud provides a [free tier](/free).\n\nServing files from Cloud Storage\n--------------------------------\n\n[Cloud Storage](/storage) can host\nstatic assets for dynamic web apps. The benefits of using Cloud Storage\ninstead of serving directly from your app include:\n\n- Cloud Storage essentially works as a [content delivery network](https://wikipedia.org/wiki/Content_delivery_network). This does not require any special configuration because by default any publicly readable object is cached in the global Cloud Storage network.\n- Your app's load will be reduced by offloading serving static assets to Cloud Storage. Depending on how many static assets you have and the frequency of access, this can reduce the cost of running your app by a significant amount.\n- Bandwidth charges for accessing content can often be less with Cloud Storage.\n\nYou can upload your assets to Cloud Storage by using the\n[Google Cloud CLI](/storage/docs/uploading-objects)\nor the [Cloud Storage API](/storage/docs).\n\nThe Google Cloud Client Library provides an idiomatic\n[Go 1.11 client to Cloud Storage](https://cloud.google.com/storage/docs/reference/libraries#client-libraries-install-go),\nfor storing and retrieving data with Cloud Storage in an\nApp Engine app.\n\n\u003cbr /\u003e\n\n### Example of serving from a Cloud Storage bucket\n\nThis simple example creates a Cloud Storage bucket and uploads static\nassets using the gcloud CLI:\n\n1. Create a bucket. It's common, but not required, to name your bucket after\n your project ID. The bucket name must be globally unique.\n\n gcloud storage buckets create gs://\u003cvar\u003eYOUR_BUCKET_NAME\u003c/var\u003e\n\n2. Set the ACL to grant read access to items in the bucket.\n\n gcloud storage buckets add-iam-policy-binding gs://\u003cvar\u003eYOUR_BUCKET_NAME\u003c/var\u003e --member=allUsers --role=roles/storage.objectViewer\n\n3. Upload items to the bucket. The `rsync` command is typically the fastest and\n easiest way to upload and update assets. You could also use `cp`.\n\n gcloud storage rsync ./static gs://\u003cvar\u003eYOUR_BUCKET_NAME\u003c/var\u003e/static --recursive\n\nYou can now access your static assets via\n`https://storage.googleapis.com/\u003cvar\u003eYOUR_BUCKET_NAME\u003c/var\u003e/static/...`.\n\nFor more details on how to use Cloud Storage to serve static assets,\nincluding how to serve from a custom domain name, refer to [How to Host a Static\nWebsite](/storage/docs/website-configuration).\n\n### Serving files from other Google Cloud services\n\nYou also have the option of using\n[Cloud CDN](/cdn/docs/overview) or other Google Cloud storage\nservices.\n\nServing files directly from your app\n------------------------------------\n\n\nTo serve static files for Go 1.11 in the standard environment,\nyou define the handlers in your `app.yaml` file using either the\n[`static_dir`](/appengine/docs/legacy/standard/go111/config/appref#handlers_static_dir)\nor\n[`static_files`](/appengine/docs/legacy/standard/go111/config/appref#handlers_static_files)\nelements.\n\nThe content in the static files or static directories are unaffected\nby the scaling settings in your `app.yaml` file. Requests to static files or\nstatic directories are handled by the App Engine infrastructure\ndirectly, and do not reach the language runtime of the application.\n\n### Configuring your static file handlers\n\nTo configure your app to serve the `./public` directory from the `/static` URL,\nyou define a handler in your `app.yaml` file.\n\nThe following demonstrates how to serve the static files of a sample\napp's `./public` directory. The template for this app's `index.html` page\ninstructs the browser to load the `main.css` file, for example: \n\n```carbon\n\u003clink type=\"text/css\" rel=\"stylesheet\" href=\"/static/css/main.css\"\u003e\n```\n\nThe `./public` directory is defined in the `static_dir` element of the project's\n`app.yaml` file: \n\n```gdscript\nhandlers:\n - url: /favicon\\.ico\n static_files: favicon.ico\n upload: favicon\\.ico\n\n - url: /static\n static_dir: public\n\n - url: /.*\n secure: always\n redirect_http_response_code: 301\n script: auto\n```\n\nThe `handlers` section in the above example handles three URL patterns:\n\n- The **/favicon.ico** handler maps a request specifically for `/favicon.ico`\n to a file named `favicon.ico` in the app's root directory.\n\n- The **/static** handler maps requests for URLs that start with `/static`.\n When App Engine receives a request for a URL beginning with `/static`,\n it maps the remainder of the path to files in the `./public` directory. If an\n appropriate file is found in the directory, the contents of that file are\n returned to the client.\n\n- The **/.**\\* handler matches all other URLs and directs them to your app.\n\nURL path patterns are tested in the order they appear in `app.yaml`, therefore\nthe pattern for your static files should be defined before the `/.*` pattern.\nFor more information, see the [`app.yaml`\nreference](/appengine/docs/legacy/standard/go111/config/appref#handlers).\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\nServing from a third-party content delivery network\n---------------------------------------------------\n\nYou can use any external third-party CDN to serve your static files and cache\ndynamic requests but your app might experience increased latency and cost.\n\nFor improved performance, you should use a third-party CDN that supports\n[CDN Interconnect](/network-connectivity/docs/cdn-interconnect)."]]