Le applicazioni spesso devono servire file statici come JavaScript, immagini e CSS oltre a gestire le richieste dinamiche. Le app nell'ambiente standard possono pubblicare file statici da un'opzione Google Cloud come Cloud Storage, pubblicarli direttamente o utilizzare una rete di distribuzione di contenuti (CDN) di terze parti. L'hosting del tuo sito statico su Google Cloud può costare meno rispetto all'utilizzo di un hosting tradizionale provider, in quanto Google Cloud fornisce un livello gratuito ## Servire file da Cloud Storage Cloud Storage può ospitare asset statici per app web dinamiche. I vantaggi dell'utilizzo di Cloud Storage anziché servire direttamente dalla tua app includono: - Cloud Storage funziona essenzialmente come una rete di distribuzione dei contenuti. Ciò non richiede alcuna configurazione speciale perché per impostazione predefinita qualsiasi oggetto leggibile viene memorizzato nella cache nella rete globale di Cloud Storage - Il carico della tua app verrà ridotto scaricando il servizio di asset statici su Cloud Storage. A seconda del numero di risorse statiche di cui disponi e della frequenza di accesso, questo può ridurre notevolmente il costo di esecuzione dell'app - I costi della larghezza di banda per l'accesso ai contenuti possono spesso essere inferiori con Cloud Storage Puoi caricare le tue risorse su Cloud Storage utilizzando il file Strumento da riga di comando gsutil o l'API Cloud Storage La libreria client di Google Cloud fornisce un client Go 1.11 idiomatico a Cloud Storage, per l'archiviazione e il recupero dei dati con Cloud Storage in un'app App Engine Esempio di pubblicazione da un bucket Cloud Storage Questo semplice esempio crea un bucket Cloud Storage e carica gli asset statici utilizzando l'interfaccia a riga di comando di Google Cloud: Crea un secchio. È comune, ma non obbligatorio, assegnare al bucket un nome dopo l'ID del progetto. Il nome del bucket deve essere univoco a livello globale gsutil mb gsyour-bucket-name>Imposta l'ACL per concedere l'accesso in lettura agli elementi nel bucket gsutil defacl set public-read gsyour-bucket-name>Carica gli elementi nel bucket. Il rsynccommand è in genere il modo più semplice e veloce per caricare e aggiornare le risorse. Potresti anche usare cp gsutil -m rsync -r ./static gsyour-bucket-name>/static Ora puoi accedere alle tue risorse statiche tramite 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.