Aplicațiile trebuie adesea să servească fișiere statice, cum ar fi JavaScript, imagini și CSS, pe lângă gestionarea solicitărilor dinamice. Aplicațiile din mediul standard pot servi fișiere statice dintr-o opțiune Google Cloud, cum ar fi Cloud Storage, le pot servi direct sau pot folosi o rețea de livrare a conținutului (CDN) terță parte. Găzduirea site-ului dvs. static pe Google Cloud poate costa mai puțin decât utilizarea unei găzduiri tradiționale. furnizor, deoarece Google Cloud oferă un nivel gratuit
## Servirea fișierelor din Cloud Storage
Cloud Storage poate găzdui active statice pentru aplicații web dinamice. Beneficiile utilizării Cloud Storage în loc de a servi direct din aplicația dvs. includ:
- Cloud Storage funcționează în esență ca o rețea de livrare a conținutului. Acest lucru nu necesită nicio configurație specială, deoarece în mod implicit orice obiect care poate fi citit este stocat în cache în rețeaua globală de stocare în cloud
- Încărcarea aplicației dvs. va fi redusă prin descărcarea de difuzare a activelor statice în Cloud Storage. În funcție de câte active statice aveți și de frecvența accesului, acest lucru poate reduce costul rulării aplicației dvs. cu o sumă semnificativă
- Taxele pentru lățimea de bandă pentru accesarea conținutului pot fi adesea mai mici cu Cloud Storage
Vă puteți încărca activele în Cloud Storage utilizând
instrument de linie de comandă gsutil
sau API-ul Cloud Storage
Biblioteca client Google Cloud oferă un client Go 1.11 idiomatic pentru Cloud Storage, pentru stocarea și preluarea datelor cu Cloud Storage într-o aplicație App Engine
Exemplu de servire dintr-o găleată Cloud Storage
Acest exemplu simplu creează un compartiment de stocare în cloud și încarcă elemente statice folosind Google Cloud CLI:
Creați o găleată. Este obișnuit, dar nu obligatoriu, să vă denumiți compartimentul după ID-ul proiectului. Numele compartimentului trebuie să fie unic la nivel global
gsutil mb gsyour-bucket-name>Setați ACL-ul pentru a acorda acces de citire articolelor din compartiment
gsutil defacl set public-read gsyour-bucket-name>Încărcați articole în găleată. The
rsynccommand este de obicei cea mai rapidă și mai ușoară modalitate de a încărca și actualiza materiale. Ai putea folosi, de asemenea
cp
gsutil -m rsync -r ./static gsyour-bucket-name>/static
Acum vă puteți accesa activele statice prin
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.