Anwendungen müssen häufig statische Dateien wie JavaScript, Bilder und CSS bereitstellen, zusätzlich zur Verarbeitung dynamischer Anforderungen. Apps in der Standardumgebung können statische Dateien von einer Google Cloud-Option wie Cloud Storage bereitstellen, sie direkt bereitstellen oder ein Content Delivery Network (CDN) eines Drittanbieters verwenden. Das Hosten Ihrer statischen Website in Google Cloud kann weniger kosten als die Verwendung eines herkömmlichen Hostings Anbieter, da Google Cloud eine kostenlose Stufe bietet ## Bereitstellen von Dateien aus Cloud Storage Cloud Storage kann statische Assets für dynamische Web-Apps hosten. Zu den Vorteilen der Verwendung von Cloud Storage anstelle der Bereitstellung direkt aus Ihrer App gehören: - Cloud Storage funktioniert im Wesentlichen als Netzwerk zur Bereitstellung von Inhalten. Dies erfordert keine spezielle Konfiguration, da standardmäßig jedes lesbare Objekt im globalen Cloud Storage-Netzwerk zwischengespeichert wird - Die Auslastung Ihrer App wird reduziert, indem die Bereitstellung statischer Assets an Cloud Storage ausgelagert wird. Je nachdem, wie viele statische Assets Sie haben und wie oft Sie darauf zugreifen, kann dies die Kosten für den Betrieb Ihrer App erheblich senken - Bandbreitengebühren für den Zugriff auf Inhalte können mit Cloud Storage oft geringer sein Sie können Ihre Assets mithilfe von in Cloud Storage hochladen gsutil-Befehlszeilentool oder die Cloud Storage-API Die Google Cloud-Clientbibliothek stellt einen idiomatischen Go 1.11-Client für Cloud Storage bereit, um Daten mit Cloud Storage in einer App Engine-Anwendung zu speichern und abzurufen Beispiel für die Bereitstellung aus einem Cloud Storage-Bucket Dieses einfache Beispiel erstellt einen Cloud Storage-Bucket und lädt statische Assets mit der Google Cloud CLI hoch: Erstellen Sie einen Eimer. Es ist üblich, aber nicht erforderlich, Ihren Bucket nach Ihrer Projekt-ID zu benennen. Der Bucket-Name muss global eindeutig sein gsutil mb gsyour-bucket-name>Legen Sie die ACL fest, um Lesezugriff auf Elemente im Bucket zu gewähren gsutil defacl set public-read gsyour-bucket-name>Laden Sie Elemente in den Bucket hoch. Das rsynccommand ist in der Regel die schnellste und einfachste Methode zum Hochladen und Aktualisieren von Assets. Könntest du auch verwenden vgl gsutil -m rsync -r ./static gsyour-bucket-name>/static Sie können jetzt über auf Ihre statischen Assets zugreifen 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.