แอปพลิเคชันมักต้องให้บริการไฟล์แบบสแตติก เช่น JavaScript รูปภาพ และ CSS นอกเหนือจากการจัดการคำขอแบบไดนามิก แอปในสภาพแวดล้อมมาตรฐานสามารถให้บริการไฟล์สแตติกจากตัวเลือก Google Cloud เช่น Cloud Storage ให้บริการโดยตรง หรือใช้เครือข่ายการจัดส่งเนื้อหาของบุคคลที่สาม (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 Client Library จัดเตรียมไคลเอนต์ Go 1.11 ที่ใช้สำนวนไปยัง Cloud Storage สำหรับจัดเก็บและดึงข้อมูลด้วย Cloud Storage ในแอป App Engine ตัวอย่างการให้บริการจากที่ฝากข้อมูล Cloud Storage ตัวอย่างง่ายๆ นี้สร้างที่เก็บข้อมูล Cloud Storage และอัปโหลดเนื้อหาคงที่โดยใช้ Google Cloud CLI: สร้างถัง เป็นเรื่องปกติ แต่ไม่จำเป็น ในการตั้งชื่อบัคเก็ตของคุณตามรหัสโปรเจ็กต์ของคุณ ชื่อบัคเก็ตต้องไม่ซ้ำกัน gsutil mb gsyour-bucket-name>ตั้งค่า ACL เพื่อให้สิทธิ์การอ่านรายการในบัคเก็ต gsutil defacl ตั้งค่า gsyour-bucket-name>ให้อ่านแบบสาธารณะ อัปโหลดรายการไปที่บัคเก็ต เดอะ rsynccommand เป็นวิธีที่เร็วและง่ายที่สุดในการอัปโหลดและอัปเดตเนื้อหา คุณสามารถใช้ ซีพี 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.