غالبًا ما تحتاج التطبيقات إلى تقديم ملفات ثابتة مثل JavaScript والصور و CSS بالإضافة إلى التعامل مع الطلبات الديناميكية. يمكن للتطبيقات في البيئة القياسية أن تخدم ملفات ثابتة من خيار Google Cloud مثل التخزين السحابي ، أو تخدمها مباشرة ، أو تستخدم شبكة توصيل محتوى لجهة خارجية (CDN). يمكن أن تكلف استضافة موقعك الثابت على Google Cloud أقل من استخدام استضافة تقليدية مزود ، حيث يوفر Google Cloud طبقة مجانية
## خدمة الملفات من التخزين السحابي
يمكن لـ Cloud Storage استضافة أصول ثابتة لتطبيقات الويب الديناميكية. تشمل مزايا استخدام Cloud Storage بدلاً من الخدمة مباشرة من تطبيقك ما يلي:
- يعمل التخزين السحابي بشكل أساسي كشبكة توصيل محتوى. هذا لا يتطلب أي تكوين خاص لأنه بشكل افتراضي يتم تخزين أي كائن قابل للقراءة مؤقتًا في شبكة التخزين السحابية العالمية
- سيتم تقليل تحميل تطبيقك عن طريق إلغاء تحميل الأصول الثابتة لخدمة التخزين السحابي. اعتمادًا على عدد الأصول الثابتة لديك وتكرار الوصول ، يمكن أن يقلل ذلك من تكلفة تشغيل تطبيقك بمقدار كبير
- غالبًا ما تكون رسوم النطاق الترددي للوصول إلى المحتوى أقل مع التخزين السحابي
يمكنك تحميل الأصول الخاصة بك إلى Cloud Storage باستخدام ملف
أداة سطر أوامر gsutil
أو Cloud Storage API
توفر مكتبة Google Cloud Client Library عميل Go 1.11 الاصطلاحي إلى Cloud Storage ، لتخزين البيانات واستردادها باستخدام Cloud Storage في تطبيق App Engine
مثال للخدمة من حاوية التخزين السحابي
ينشئ هذا المثال البسيط حاوية التخزين السحابي وتحميل الأصول الثابتة باستخدام Google Cloud CLI:
قم بإنشاء دلو. من الشائع ، ولكن ليس مطلوبًا ، تسمية الحاوية الخاصة بك بعد معرف المشروع الخاص بك. يجب أن يكون اسم الحاوية فريدًا بشكل عام
gsutil mb gsyour-bucket-name>قم بتعيين قائمة التحكم بالوصول (ACL) لمنح حق الوصول للقراءة إلى العناصر الموجودة في الحاوية
تعيين gsutil defacl 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.