除了处理动态请求之外,应用程序通常还需要提供静态文件,例如 JavaScript、图像和 CSS。标准环境中的应用程序可以从 Cloud Storage 等 Google Cloud 选项提供静态文件,直接提供这些文件,或使用第三方内容分发网络 (CDN)。在 Google Cloud 上托管静态网站的成本低于使用传统托管提供商,因为 Google Cloud 提供免费套餐 ## 从云存储服务文件 Cloud Storage 可以托管动态网络应用的静态资产。使用 Cloud Storage 而不是直接从您的应用提供服务的好处包括: - 云存储本质上是一个内容分发网络。这不需要任何特殊配置,因为默认情况下任何可读对象都缓存在全局云存储网络中 - 通过将服务静态资产卸载到云存储,您的应用程序的负载将减少。根据您拥有的静态资产数量和访问频率,这可以显着降低运行应用程序的成本 - 使用云存储通常可以减少访问内容的带宽费用 您可以使用以下方法将资产上传到 Cloud Storage gsutil 命令行工具 或云存储 API Google Cloud Client Library 为 Cloud Storage 提供了一个惯用的 Go 1.11 客户端,用于在 App Engine 应用程序中使用 Cloud Storage 存储和检索数据 从 Cloud Storage 存储桶提供服务的示例 这个简单的示例创建一个 Cloud Storage 存储桶并使用 Google Cloud CLI 上传静态资产: 创建一个桶。在您的项目 ID 之后命名您的存储桶很常见,但不是必需的。桶名必须全局唯一 gsutil mb gsyour-bucket-name>设置 ACL 以授予对存储桶中项目的读取访问权限 gsutil defacl set public-read gsyour-bucket-name>将项目上传到存储桶。这 rsync 命令通常是上传和更新资产的最快和最简单的方法。你也可以使用 cp gsutil -m rsync -r ./static gsyour-bucket-name>/static 您现在可以通过以下方式访问您的静态资产 httpsstorage.googleapis.com//static For more details on how to use Cloud Storage to serve static assets, including how to serve from a custom domain name, refer to How to Host a Static Website Serving files from other Google Cloud services You also have the option of using Cloud CDN or other Google Cloud storage services ## Serving files directly from your app To serve static files for Go 1.11 in the standard environment, you define the handlers in your app.yaml file using either the static_dir or static_files elements The content in the static files or static directories are unaffected by the scaling settings in your app.yaml file. Requests to static files or static directories are handled by the App Engine infrastructure directly, and do not reach the language runtime of the application Configuring your static file handlers To configure your app to serve the ./public directory from the /static URL, you define a handler in your app.yaml file The following demonstrates how to serve the static files of a sample app's ./public directory. The template for this app's index.html page instructs the browser to load the main.css file, for example: /css/main.css"> The ./public directory is defined in the static_dir element of the project's app.yaml file: 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 The handlers section in the above example handles three URL patterns: The /favicon.icohandler maps a request specifically for /favicon.icoto a file named favicon.icoin the app's root directory The /statichandler maps requests for URLs that start with /static. When App Engine receives a request for a URL beginning with /static, it maps the remainder of the path to files in the ./publicdirectory. If an appropriate file is found in the directory, the contents of that file are returned to the client The handler matches all other URLs and directs them to your app URL path patterns are tested in the order they appear in app.yaml, therefore the pattern for your static files should be defined before the pattern For more information, see the app.yaml reference ## Serving from a third-party content delivery network You can use any external third-party CDN to serve your static files and cache dynamic requests but your app might experience increased latency and cost For improved performance, you should use a third-party CDN that supports CDN Interconnect.