アプリケーションは、動的リクエストの処理に加えて、JavaScript、画像、CSS などの静的ファイルを提供する必要があることがよくあります。スタンダード環境のアプリは、Cloud Storage などの Google Cloud オプションから静的ファイルを提供したり、それらを直接提供したり、サードパーティのコンテンツ配信ネットワーク (CDN) を使用したりできます。Google Cloud で静的サイトをホストすると、従来のホスティングを使用するよりもコストが低くなります。プロバイダー、Google Cloud は無料枠を提供するため ## 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 を使用してデータを保存および取得するために、慣用的な Go 1.11 クライアントを 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 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.