Las aplicaciones a menudo necesitan servir archivos estáticos como JavaScript, imágenes y CSS además de manejar solicitudes dinámicas. Las aplicaciones en el entorno estándar pueden servir archivos estáticos desde una opción de Google Cloud como Cloud Storage, servirlos directamente o usar una red de entrega de contenido (CDN) de terceros. Alojar su sitio estático en Google Cloud puede costar menos que usar un alojamiento tradicional proveedor, ya que Google Cloud proporciona un nivel gratuito ## Sirviendo archivos desde Cloud Storage Cloud Storage puede alojar activos estáticos para aplicaciones web dinámicas. Los beneficios de usar Cloud Storage en lugar de servir directamente desde su aplicación incluyen: - Cloud Storage funciona esencialmente como una red de entrega de contenido. Esto no requiere ninguna configuración especial porque, de forma predeterminada, cualquier objeto legible se almacena en caché en la red global de almacenamiento en la nube. - La carga de su aplicación se reducirá al descargar activos estáticos de servicio a Cloud Storage. Según la cantidad de activos estáticos que tenga y la frecuencia de acceso, esto puede reducir el costo de ejecución de su aplicación en una cantidad significativa. - Los cargos por ancho de banda para acceder al contenido a menudo pueden ser menores con Cloud Storage Puede cargar sus activos en Cloud Storage usando el herramienta de línea de comandos gsutil o la API de almacenamiento en la nube Google Cloud Client Library proporciona un cliente Go 1.11 idiomático a Cloud Storage, para almacenar y recuperar datos con Cloud Storage en una aplicación de App Engine. Ejemplo de servir desde un depósito de Cloud Storage Este ejemplo simple crea un depósito de Cloud Storage y carga recursos estáticos mediante la CLI de Google Cloud: Crea un balde. Es común, pero no obligatorio, nombrar su depósito con el ID de su proyecto. El nombre del depósito debe ser globalmente único. gsutil mb gsnombre-de-su-depósito>Configure la ACL para otorgar acceso de lectura a los elementos en el depósito gsutil defacl set public-read gsyour-bucket-name>Sube elementos al cubo. Él rsynccommand suele ser la forma más rápida y sencilla de cargar y actualizar activos. También podrías usar c.p. gsutil -m rsync -r ./static gsnombre-de-su-depósito>/static Ahora puede acceder a sus activos estáticos a través de 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.