डायनेमिक अनुरोधों को संभालने के अलावा अनुप्रयोगों को अक्सर जावास्क्रिप्ट, छवियों और सीएसएस जैसी स्थिर फ़ाइलों की सेवा देने की आवश्यकता होती है। मानक वातावरण में ऐप्स क्लाउड स्टोरेज जैसे Google क्लाउड विकल्प से स्थिर फ़ाइलों की सेवा कर सकते हैं, उन्हें सीधे सेवा दे सकते हैं, या तृतीय-पक्ष सामग्री वितरण नेटवर्क (CDN) का उपयोग कर सकते हैं। Google क्लाउड पर अपनी स्थिर साइट को होस्ट करने से पारंपरिक होस्टिंग का उपयोग करने की तुलना में कम लागत आ सकती है। प्रदाता, क्योंकि Google क्लाउड एक निःशुल्क टियर प्रदान करता है
## क्लाउड स्टोरेज से फाइल सर्व करना
क्लाउड स्टोरेज डायनेमिक वेब ऐप्स के लिए स्थिर संपत्तियों को होस्ट कर सकता है। अपने ऐप से सीधे सेवा देने के बजाय क्लाउड स्टोरेज का उपयोग करने के लाभों में शामिल हैं:
- क्लाउड स्टोरेज अनिवार्य रूप से कंटेंट डिलीवरी नेटवर्क के रूप में काम करता है। इसके लिए किसी विशेष कॉन्फ़िगरेशन की आवश्यकता नहीं होती है क्योंकि डिफ़ॉल्ट रूप से किसी भी पढ़ने योग्य वस्तु को वैश्विक क्लाउड स्टोरेज नेटवर्क में कैश किया जाता है
- क्लाउड स्टोरेज को स्थिर संपत्तियों को ऑफलोड करके आपके ऐप का लोड कम हो जाएगा। आपके पास कितनी स्थिर संपत्तियां हैं और एक्सेस की आवृत्ति के आधार पर, यह आपके ऐप को चलाने की लागत को एक महत्वपूर्ण राशि से कम कर सकता है
- सामग्री तक पहुँचने के लिए बैंडविड्थ शुल्क अक्सर क्लाउड स्टोरेज से कम हो सकता है
आप का उपयोग करके अपनी संपत्तियों को क्लाउड स्टोरेज पर अपलोड कर सकते हैं
gsutil कमांड लाइन टूल
या क्लाउड स्टोरेज एपीआई
ऐप इंजन ऐप में क्लाउड स्टोरेज के साथ डेटा को स्टोर करने और पुनर्प्राप्त करने के लिए Google क्लाउड क्लाइंट लाइब्रेरी क्लाउड स्टोरेज के लिए एक मुहावरेदार गो 1.11 क्लाइंट प्रदान करता है।
क्लाउड स्टोरेज बकेट से सर्व करने का उदाहरण
यह सरल उदाहरण क्लाउड स्टोरेज बकेट बनाता है और Google क्लाउड सीएलआई का उपयोग करके स्थिर संपत्ति अपलोड करता है:
एक बाल्टी बनाएँ। अपनी परियोजना आईडी के बाद अपनी बकेट का नाम देना सामान्य है, लेकिन आवश्यक नहीं है। बकेट का नाम विश्व स्तर पर अद्वितीय होना चाहिए
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.