You can use Cloud Storage to store and serve files, such as movies or images or other static content.
This document describes how to use the Google Cloud Client Library in your app to store data in and retrieve data from Cloud Storage.
Before you begin
Follow the instructions in "Hello, World!" for Node.js on App Engine to set up your environment and project, and to understand how Node.js apps are structured in App Engine. Write down and save your project ID, because you will need it to run the sample application described in this document.
Make sure you create a Cloud Storage bucket for your application by invoking the following command:
gsutil mb gs://[YOUR_BUCKET_NAME]
Make the bucket publicly readable so it can serve files:
gsutil defacl set public-read gs://[YOUR_BUCKET_NAME]
Download the sample
To clone the repository:
git clone https://github.com/GoogleCloudPlatform/nodejs-docs-samples/
cd nodejs-docs-samples/appengine/storage/flexible
Edit project configuration and install dependencies
In app.yaml
, add your project ID to the GOOGLE_CLOUD_PROJECT
environment
value. Then set the GCLOUD_STORAGE_BUCKET
environment value to the name of
the Cloud Storage bucket you created previously.
In package.json
, add @google-cloud/storage
as a dependency, which
provides the functions to use Cloud Storage.
See the README.md
file
for instructions on running and testing locally.
Application code
The sample application presents a web page prompting the user to supply a file to be stored in Cloud Storage. When the user selects a file and clicks submit, the upload handler loads the file content into a blob and writes it to Cloud Storage.
Notice that after the file is uploaded to Cloud Storage, the public URL to this file is returned, which you can use to serve the file directly from Cloud Storage. You should store this value in your app for future use.
For more information
For complete information on Cloud Storage, see the Cloud Storage documentation.