Go 1.11 已达到支持终止期限,将于 2026 年 1 月 31 日
弃用。弃用后,您将无法部署 Go 1.11 应用,即使您的组织之前曾使用组织政策重新启用旧版运行时的部署也是如此。现有的 Go 1.11 应用在
弃用日期之后将继续运行并接收流量。我们建议您
迁移到最新支持的 Go 版本。
存储和传送静态文件
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
除了处理动态请求之外,应用通常还需要传送静态文件(如 JavaScript、图片和 CSS)。标准环境中的应用可以通过 Google Cloud 选项(如 Cloud Storage)传送静态文件,直接传送或使用第三方内容分发网络 (CDN)。
Google Cloud 提供
免费层级,因此在 Google Cloud 上托管静态网站的费用可能低于使用传统托管服务提供商的费用。
从 Cloud Storage 传送文件
Cloud Storage 可为动态 Web 应用托管静态资源。与直接从应用传送内容相比,使用 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 传送静态文件,请在 app.yaml
文件中使用 static_dir
或 static_files
元素定义处理程序。
静态文件或静态目录中的内容不受 app.yaml
文件中的扩缩设置的影响。对静态文件或静态目录的请求由 App Engine 基础架构直接处理,不会到达应用的语言运行时。
配置静态文件处理程序
如需将应用配置为从 /static
网址传送 ./public
目录,请在 app.yaml
文件中定义处理程序。
下面演示了如何传送示例应用 ./public
目录中的静态文件。此应用 index.html
页面的模板会指示浏览器加载 main.css
文件,例如:
<link type="text/css" rel="stylesheet" href="/static/css/main.css">
项目的 app.yaml
文件的 static_dir
元素中定义了 ./public
目录:
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 Interconnect 的第三方 CDN。
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-20。
[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["很难理解","hardToUnderstand","thumb-down"],["信息或示例代码不正确","incorrectInformationOrSampleCode","thumb-down"],["没有我需要的信息/示例","missingTheInformationSamplesINeed","thumb-down"],["翻译问题","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-08-20。"],[[["\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)."]]