Οι εφαρμογές συχνά χρειάζεται να εξυπηρετούν στατικά αρχεία όπως JavaScript, εικόνες και CSS εκτός από τον χειρισμό δυναμικών αιτημάτων. Οι εφαρμογές στο τυπικό περιβάλλον μπορούν να προβάλλουν στατικά αρχεία από μια επιλογή Google Cloud, όπως το Cloud Storage, να τα εξυπηρετούν απευθείας ή να χρησιμοποιούν ένα δίκτυο παράδοσης περιεχομένου τρίτου μέρους (CDN). Η φιλοξενία του στατικού ιστότοπού σας στο Google Cloud μπορεί να κοστίσει λιγότερο από τη χρήση μιας παραδοσιακής φιλοξενίας παρόχου, καθώς το Google Cloud παρέχει μια δωρεάν βαθμίδα
## Εξυπηρέτηση αρχείων από το Cloud Storage
Το Cloud Storage μπορεί να φιλοξενήσει στατικά στοιχεία για δυναμικές εφαρμογές ιστού. Τα οφέλη από τη χρήση του Cloud Storage αντί της απευθείας προβολής από την εφαρμογή σας περιλαμβάνουν:
- Το Cloud Storage ουσιαστικά λειτουργεί ως δίκτυο παράδοσης περιεχομένου. Αυτό δεν απαιτεί καμία ειδική διαμόρφωση, επειδή από προεπιλογή οποιοδήποτε αναγνώσιμο αντικείμενο αποθηκεύεται προσωρινά στο παγκόσμιο δίκτυο αποθήκευσης Cloud
- Ο φόρτος της εφαρμογής σας θα μειωθεί με τη μεταφόρτωση στατικών στοιχείων προβολής στο Cloud Storage. Ανάλογα με τον αριθμό στατικών στοιχείων που έχετε και τη συχνότητα πρόσβασης, αυτό μπορεί να μειώσει το κόστος λειτουργίας της εφαρμογής σας κατά ένα σημαντικό ποσό
- Οι χρεώσεις εύρους ζώνης για την πρόσβαση σε περιεχόμενο μπορεί συχνά να είναι μικρότερες με το Cloud Storage
Μπορείτε να ανεβάσετε τα στοιχεία σας στο Cloud Storage χρησιμοποιώντας το
εργαλείο γραμμής εντολών gsutil
ή το Cloud Storage API
Η Βιβλιοθήκη Google Cloud Client παρέχει ένα ιδιωματικό πρόγραμμα-πελάτη Go 1.11 στο Cloud Storage, για αποθήκευση και ανάκτηση δεδομένων με το Cloud Storage σε μια εφαρμογή App Engine
Παράδειγμα εξυπηρέτησης από κάδο Cloud Storage
Αυτό το απλό παράδειγμα δημιουργεί έναν κάδο αποθήκευσης Cloud και ανεβάζει στατικά στοιχεία χρησιμοποιώντας το Google Cloud CLI:
Δημιουργήστε έναν κουβά. Είναι σύνηθες, αλλά δεν απαιτείται, να ονομάσετε τον κάδο σας με το αναγνωριστικό του έργου σας. Το όνομα του κάδου πρέπει να είναι μοναδικό παγκοσμίως
γκσουτίλ mb gsyour-bucket-name>Ρυθμίστε το ACL ώστε να εκχωρεί πρόσβαση ανάγνωσης σε στοιχεία του κάδου
σύνολο gsutil defacl 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.