Using Cloud Storage

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 to store and retrieve data using Cloud Storage in an App Engine app.


Prerequisites and setup

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]

And make it publically readable so it can serve files:

gsutil defacl set public-read gs://[your-bucket-name]

Clone the repository

Download the sample:

git clone
cd nodejs-docs-samples/appengine/storage

Edit project configuration and install dependencies

In app.yaml, a GCLOUD_PROJECT for your project. This value is your project ID. Then set GCLOUD_STORAGE_BUCKET; this value is the name of the Cloud Storage bucket you created previously.

# Copyright 2015-2016, Google, Inc.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# See the License for the specific language governing permissions and
# limitations under the License.

# [START app_yaml]
runtime: nodejs
vm: true

# [START env]
  GCLOUD_PROJECT: <your-project-id>
  GCLOUD_STORAGE_BUCKET: <your-bucket-name>
# [END env]
# [END app_yaml]

In package.json, set gcloud as a dependency, which provides the functions to use Cloud Storage.

  "name": "appengine-storage",
  "description": "Node.js Google Cloud Storage sample for Google App Engine",
  "scripts": {
    "start": "node app.js"
  "dependencies": {
    "@google-cloud/storage": "^0.1.1",
    "body-parser": "^1.14.2",
    "express": "^4.13.4",
    "jade": "^1.11.0",
    "multer": "^1.2.0"

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.

'use strict';

var format = require('util').format;
var express = require('express');

// By default, the client will authenticate using the service account file
// specified by the GOOGLE_APPLICATION_CREDENTIALS environment variable and use
// the project specified by the GCLOUD_PROJECT environment variable. See
// These environment variables are set automatically on Google App Engine
var Storage = require('@google-cloud/storage');

// Instantiate a storage client
var storage = Storage();

var app = express();
app.set('view engine', 'jade');

// Multer is required to process file uploads and make them available via
// req.files.
var multer = require('multer')({
  inMemory: true,
  fileSize: 5 * 1024 * 1024 // no larger than 5mb, you can change as needed.

// A bucket is a container for objects (files).
var bucket = storage.bucket(process.env.GCLOUD_STORAGE_BUCKET);

// Display a form for uploading files.
app.get('/', function (req, res) {

// Process the file upload and upload to Google Cloud Storage.'/upload', multer.single('file'), function (req, res, next) {
  if (!req.file) {
    return res.status(400).send('No file uploaded.');

  // Create a new blob in the bucket and upload the file data.
  var blob = bucket.file(req.file.originalname);
  var blobStream = blob.createWriteStream();

  blobStream.on('error', function (err) {
    return next(err);

  blobStream.on('finish', function () {
    // The public URL can be used to directly access the file via HTTP.
    var publicUrl = format(


var server = app.listen(process.env.PORT || '8080', function () {
  console.log('App listening on port %s', server.address().port);
  console.log('Press Ctrl+C to quit.');

For more information

For complete information on Cloud Storage, see the Cloud Storage documentation.

Send feedback about...

App Engine flexible environment for Node.js docs