애플리케이션은 종종 동적 요청을 처리하는 것 외에도 JavaScript, 이미지 및 CSS와 같은 정적 파일을 제공해야 합니다. 가변형 환경의 앱은 Cloud Storage와 같은 Google Cloud 옵션에서 정적 파일을 제공하거나 직접 제공하거나 타사 콘텐츠 전송 네트워크(CDN)를 사용할 수 있습니다. ## Cloud Storage에서 파일 제공 Cloud Storage는 동적 웹 앱의 정적 자산을 호스팅할 수 있습니다. 앱에서 직접 제공하는 대신 Cloud Storage를 사용하면 다음과 같은 이점이 있습니다. - Cloud Storage는 기본적으로 콘텐츠 전송 네트워크로 작동합니다. 기본적으로 읽을 수 있는 개체는 글로벌 Cloud Storage 네트워크에 캐시되므로 특별한 구성이 필요하지 않습니다. - 정적 자산 제공을 Cloud Storage로 오프로드하여 앱의 로드가 줄어듭니다. 보유하고 있는 정적 자산의 수와 액세스 빈도에 따라 앱 실행 비용을 크게 줄일 수 있습니다. - 콘텐츠 액세스에 대한 대역폭 요금은 Cloud Storage를 사용하면 종종 더 적을 수 있습니다. 다음을 사용하여 애셋을 Cloud Storage에 업로드할 수 있습니다. gsutil 명령줄 도구 또는 Cloud Storage API Google Cloud 클라이언트 라이브러리는 App Engine 앱에서 Cloud Storage로 데이터를 저장하고 검색하기 위해 Cloud Storage에 관용적인 클라이언트를 제공합니다. Cloud Storage 버킷에서 제공하는 예 이 간단한 예는 Cloud Storage 버킷을 만들고 Google Cloud CLI를 사용하여 정적 자산을 업로드합니다. 버킷을 생성합니다. 프로젝트 ID를 따라 버킷 이름을 지정하는 것이 일반적이지만 필수는 아닙니다. 버킷 이름은 전역적으로 고유해야 합니다. gsutil mb gsyour-bucket-name>버킷의 항목에 대한 읽기 액세스 권한을 부여하도록 ACL 설정 gsutil defacl set public-read gsyour-bucket-name>항목을 버킷에 업로드합니다. 그만큼 rsynccommand는 일반적으로 자산을 업로드하고 업데이트하는 가장 빠르고 쉬운 방법입니다. 당신은 또한 사용할 수 있습니다 CP gsutil -m rsync -r ./static gsyour-bucket-name>/static 이제 다음을 통해 정적 자산에 액세스할 수 있습니다. 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 Serving files from your app is typically straightforward, however, there are a couple drawbacks that you should consider: - Requests for static files can use resources that otherwise would be used for dynamic requests - Depending on your configuration, serving files from your app can result in response latency, which can also affect when new instances are created for handling the load Example of serving static files with your app Go In Go, you can use the standard http.FileServer or http.ServeFile to serve files directly from your app // Package static demonstrates a static file handler for App Engine flexible environment. package main import ( "fmt" "net/http" "google.golang.org/appengine" ) func main() { // Serve static files from "static" directory. http.Handlestatic http.FileServer(http.Dir http.HandleFunc homepageHandler) appengine.Main() } const homepage = doctype html> Static Files /main.css">

This is a static file serving examplep>

Static Files /styles.css">

This is a static file serving examplep>

default doctype html html(lang="en") head title Static Files meta(charset='utf-8') link(rel="stylesheet", hrefstatic/main.css") body p This is a static file serving example The stylesheet itself is located at ./public/css, which is served from /static/main.css body { font-family: Verdana, Helvetica, sans-serif; background-color: #CCCCFF; } Other Node.js frameworks, such as Hapi, Koa, and Sails typically support serving static files directly from the application. Refer to their documentation for details on how to configure and use static content PHP The PHP runtime runs nginx to serve your app, which is configured to serve static files in your project directory. You must declare the document root by specifying document_root in your app.yaml file: runtime: php env: flex runtime_config: document_root: web Python Most web frameworks include support for serving static files. In this sample, the app uses Flask's built-in ability to serve files in ./static directory from the /static URL The app includes a view that renders the template. Flask automatically serves everything in the ./static directory without additional configuration import logging from flask import Flask, render_template app = Flaskname @app.route def hello return render_template('index.html') @app.errorhandler(500) def server_error(e): logging.exception('An error occurred during a request returnAn internal error occurred: See logs for full stacktrace. format(e), 500 if __name__ == main # This is used when running locally. Gunicorn is used to run the # application on Google App Engine. See entrypoint in app.yaml. app.run(host='127.0.0.1', port=8080, debug=True) The template rendered by the view includes a stylesheet located at /static/main.css Static FilesFlask automatically makes files in the 'static' directory available via '/static'./main.css">

This is a static file serving examplep>

doctype html html head title Serving Static Files link rel="stylesheet" hrefapplication.css" script srcapplication.js" body p This is a static file serving example The stylesheet is located at ./public/application.css which is served from /application.css body { font-family: Verdana, Helvetica, sans-serif; background-color: #CCCCFF; } Ruby on Rails The Ruby on Rails web framework serves files from the ./public directory by default. Static JavaScript and CSS files can also be generated by the Rails asset pipeline This example app has a layout view that includes all the app's stylesheets: doctype html html head title Serving Static Files = stylesheet_link_tag "application", media: "all" = javascript_include_tag "application" = csrf_meta_tags body = yield The stylesheet itself is a Sass file located at ./app/assets/stylesheets/main.css.sass body font-family: Verdana, Helvetica, sans-serif background-color: #CCCCFF By default, Rails apps do not generate or serve static assets when running in production The Ruby runtime executes rake assets:precompile during deployment to generate static assets and sets the RAILS_SERVE_STATIC_FILES environment variable to enable static file serving in production .NET Hello Static World

This is a static html documentp>