Aplikacje często muszą obsługiwać pliki statyczne, takie jak JavaScript, obrazy i CSS, oprócz obsługi żądań dynamicznych. Aplikacje w standardowym środowisku mogą udostępniać pliki statyczne z opcji Google Cloud, takiej jak Cloud Storage, udostępniać je bezpośrednio lub korzystać z sieci dostarczania treści (CDN) innej firmy. Hosting witryny statycznej w Google Cloud może kosztować mniej niż korzystanie z tradycyjnego hostingu dostawcy, ponieważ Google Cloud zapewnia bezpłatny poziom
## Udostępnianie plików z Cloud Storage
Cloud Storage może przechowywać zasoby statyczne dla dynamicznych aplikacji internetowych. Korzyści płynące z używania Cloud Storage zamiast udostępniania bezpośrednio z aplikacji obejmują:
- Cloud Storage zasadniczo działa jako sieć dostarczania treści. Nie wymaga to żadnej specjalnej konfiguracji, ponieważ domyślnie każdy możliwy do odczytu obiekt jest buforowany w globalnej sieci Cloud Storage
- Obciążenie Twojej aplikacji zostanie zmniejszone dzięki przeniesieniu obsługi zasobów statycznych do Cloud Storage. W zależności od liczby posiadanych zasobów statycznych i częstotliwości uzyskiwania dostępu może to znacznie obniżyć koszty obsługi aplikacji
- Opłaty za przepustowość dostępu do treści mogą być często niższe w przypadku Cloud Storage
Możesz przesłać swoje zasoby do Cloud Storage za pomocą
narzędzie wiersza poleceń gsutil
lub Cloud Storage API
Biblioteka klienta Google Cloud zapewnia idiomatycznego klienta Go 1.11 do Cloud Storage, do przechowywania i pobierania danych z Cloud Storage w aplikacji App Engine
Przykład udostępniania z zasobnika Cloud Storage
Ten prosty przykład tworzy zasobnik Cloud Storage i przesyła zasoby statyczne za pomocą Google Cloud CLI:
Utwórz wiadro. Powszechne, ale nie wymagane, jest nazywanie zasobnika na podstawie identyfikatora projektu. Nazwa zasobnika musi być globalnie unikatowa
gsutil mb gsyour-bucket-name>Ustaw listę ACL, aby przyznać dostęp do odczytu elementom w zasobniku
gsutil defacl ustaw do odczytu publicznego gsyour-bucket-name>Prześlij elementy do zasobnika. The
rsynccommand to zazwyczaj najszybszy i najłatwiejszy sposób przesyłania i aktualizowania zasobów. Możesz też skorzystać
cp
gsutil -m rsync -r ./static gsyour-nazwa-zasobnika>/static
Możesz teraz uzyskać dostęp do swoich zasobów statycznych za pośrednictwem
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.