Applikationer behöver ofta betjäna statiska filer som JavaScript, bilder och CSS förutom att hantera dynamiska förfrågningar. Appar i standardmiljön kan visa statiska filer från ett Google Cloud-alternativ som Cloud Storage, servera dem direkt eller använda ett tredjepartsnätverk för innehållsleverans (CDN). Att vara värd för din statiska webbplats på Google Cloud kan kosta mindre än att använda en traditionell värdtjänst leverantör, eftersom Google Cloud tillhandahåller en gratis nivå ## Betjänar filer från Cloud Storage Cloud Storage kan vara värd för statiska tillgångar för dynamiska webbappar. Fördelarna med att använda Cloud Storage istället för att visa direkt från din app inkluderar: - Cloud Storage fungerar i huvudsak som ett innehållsleveransnätverk. Detta kräver ingen speciell konfiguration eftersom alla läsbara objekt som standard cachelagras i det globala Cloud Storage-nätverket - Din app belastning kommer att minskas genom att betjänande statiska tillgångar laddas ner till molnlagring. Beroende på hur många statiska tillgångar du har och åtkomstfrekvensen kan detta minska kostnaden för att driva din app med en betydande mängd – Bandbreddsavgifterna för åtkomst till innehåll kan ofta vara lägre med Cloud Storage Du kan ladda upp dina tillgångar till Cloud Storage genom att använda gsutil kommandoradsverktyg eller Cloud Storage API Google Cloud Client Library tillhandahåller en idiomatisk Go 1.11-klient till Cloud Storage, för att lagra och hämta data med Cloud Storage i en App Engine-app Exempel på servering från en Cloud Storage-hink Detta enkla exempel skapar en Cloud Storage-bucket och laddar upp statiska tillgångar med Google Cloud CLI: Skapa en hink. Det är vanligt, men inte obligatoriskt, att döpa din hink efter ditt projekt-ID. Namnet på hinken måste vara globalt unikt gsutil mb gsyour-bucket-name>Ställ in ACL för att ge läsåtkomst till objekt i hinken gsutil defacl set public-read gsyour-bucket-name>Ladda upp föremål till hinken. De rsynccommand är vanligtvis det snabbaste och enklaste sättet att ladda upp och uppdatera tillgångar. Du kan också använda cp gsutil -m rsync -r ./static gsyour-bucket-name>/static Du kan nu komma åt dina statiska tillgångar via 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.