如何使用桶存储在 google flex/app 引擎环境中提供静态文件?

How can I use bucket storage to serve static files on google flex/app engine environment?

我有一个 nodejs 后端和一个 reactjs 前端。我正在使用 gcloud flex 环境(应用程序引擎)并希望使用 CDN 提供所有前端文件。我不希望请求触及我的 nodejs 服务器。我无法配置我的项目 app.yaml 来做同样的事情。

我怀疑我的请求不是由 CDN 提供的,因为如果我在我的 nodejs 代码中注释以下行,我将无法再访问 index.html。

app.use('/', express.static(path.resolve('./frontend/dist')));

下面是 YAML 文件。

handlers:
- url: /(.*\.html)
  mime_type: text/html 
  static_files: frontend/dist/ 
  upload: frontend/dist/(.*\.html)

- url: /styles/(.*\.css) 
  mime_type: text/css 
  static_files: frontend/dist/styles/ 
  upload: frontend/dist/styles/(.*\.css)

- url: /scripts/(.*\.js) 
  mime_type: text/javascript 
  static_files: frontend/dist/scripts/ 
  upload: frontend/dist/scripts/(.*\.js)

- url: /images/(.*\.(bmp|gif|ico|jpeg|jpg|png)) 
  static_files: frontend/dist/images/ 
  upload: frontend/dist/images/(.*\.(bmp|gif|ico|jpeg|jpg|png))

- url: / 
  static_files: frontend/dist/index.html 
  upload: frontend/dist/index.html

-  url: /.* 
  script: IGNORED
  secure: always

有没有一种方法可以配置 App Engine,使静态文件请求不响应我的 nodejs 后端服务器?

谢谢

你搞混了 standard GAE env app.yaml elements (the static content config) into your flex env app app.yaml

在 flex 环境中提供静态内容是不同的。

您基于 express.static 的静态文件服务方法实际上对应于 Serving from your application:

Serving from your application

Most web frameworks include support for serving static files. In this sample, the application uses the express.static middleware to serve files from the ./public directory to the /static URL.

要在请求不影响您的应用程序的情况下提供静态内容,您需要遵循 Serving from Cloud Storage:

Example of serving static files from a Cloud Storage bucket

This simple example creates a Cloud Storage bucket and uploads static assets using the Cloud SDK:

  1. Create a bucket. It's common, but not required, to name your bucket after your project ID. The bucket name must be globally unique.

    gsutil mb gs://<your-bucket-name>
    
  2. Set the ACL to grant read access to items in the bucket.

    gsutil defacl set public-read gs://<your-bucket-name>
    
  3. Upload items to the bucket. The rsync command is typically the fastest and easiest way to upload and update assets. You could also use cp.

    gsutil -m rsync -r ./static gs://<your-bucket-name>/static
    

You can now access your static assets via https://storage.googleapis.com/<your-bucket-name>/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.

For more information on how to use the Cloud Storage API to dynamically upload, download, and manipulate files from within your application, see Using Cloud Storage.