Các ứng dụng thường cần phân phát các tệp tĩnh như JavaScript, hình ảnh và CSS ngoài việc xử lý các yêu cầu động. Các ứng dụng trong môi trường tiêu chuẩn có thể phân phát các tệp tĩnh từ tùy chọn Google Cloud như Cloud Storage, phân phối chúng trực tiếp hoặc sử dụng mạng phân phối nội dung của bên thứ ba (CDN). Lưu trữ trang web tĩnh của bạn trên Google Cloud có thể tốn ít chi phí hơn so với sử dụng dịch vụ lưu trữ truyền thống nhà cung cấp, vì Google Cloud cung cấp một bậc miễn phí
## Cung cấp tệp từ Cloud Storage
Cloud Storage có thể lưu trữ nội dung tĩnh cho các ứng dụng web động. Lợi ích của việc sử dụng Cloud Storage thay vì phân phối trực tiếp từ ứng dụng của bạn bao gồm:
- Cloud Storage về cơ bản hoạt động như một mạng phân phối nội dung. Điều này không yêu cầu bất kỳ cấu hình đặc biệt nào vì theo mặc định, bất kỳ đối tượng nào có thể đọc được đều được lưu vào bộ đệm trong mạng Lưu trữ đám mây toàn cầu
- Tải ứng dụng của bạn sẽ được giảm bớt bằng cách giảm tải việc cung cấp nội dung tĩnh cho Cloud Storage. Tùy thuộc vào số lượng nội dung tĩnh bạn có và tần suất truy cập, điều này có thể giảm đáng kể chi phí chạy ứng dụng của bạn
- Phí băng thông để truy cập nội dung thường có thể ít hơn với Cloud Storage
Bạn có thể tải nội dung của mình lên Cloud Storage bằng cách sử dụng
công cụ dòng lệnh gsutil
hoặc API lưu trữ đám mây
Thư viện ứng dụng khách Google Cloud cung cấp ứng dụng khách Go 1.11 đặc trưng cho Cloud Storage, để lưu trữ và truy xuất dữ liệu bằng Cloud Storage trong ứng dụng App Engine
Ví dụ về phân phối từ bộ chứa Cloud Storage
Ví dụ đơn giản này tạo nhóm Lưu trữ đám mây và tải nội dung tĩnh lên bằng Google Cloud CLI:
Tạo một cái xô. Thông thường, nhưng không bắt buộc, đặt tên nhóm của bạn theo ID dự án của bạn. Tên thùng phải là duy nhất trên toàn cầu
gsutil mb gsyour-bucket-name>Đặt ACL để cấp quyền truy cập đọc cho các mục trong nhóm
gsutil defacl set gsyour-bucket-name đọc công khai>Tải các mục lên thùng. Các
rsynccommand thường là cách nhanh nhất và dễ dàng nhất để tải lên và cập nhật nội dung. Bạn cũng có thể sử dụng
cp
gsutil -m rsync -r ./static gsyour-bucket-name>/static
Bây giờ bạn có thể truy cập tài sản tĩnh của mình thông qua
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.