Notice: Over the next few months, we're reorganizing the App Engine documentation site to make it easier to find content and better align with the rest of Google Cloud products. The same content will be available, but the navigation will now match the rest of the Cloud products.

Using Cloud Storage

Stay organized with collections Save and categorize content based on your preferences.

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 Python on App Engine to set up your environment and project, and to understand how Python 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
cd python-docs-samples/appengine/flexible/storage

Edit project configuration and install dependencies

In app.yaml, set GOOGLE_STORAGE_BUCKET; this value is the name of the Cloud Storage bucket you created previously.

    CLOUD_STORAGE_BUCKET: your-bucket-name

In requirements.txt, notice that you must include the google-cloud-storage library, because this provides Cloud Storage functions.

Flask==2.1.0; python_version > '3.6'
Flask==2.0.3; python_version < '3.7'
werkzeug==2.1.2; python_version > '3.6'
werkzeug==2.0.3; python_version < '3.7'
google-cloud-storage==2.0.0; python_version < '3.7'
google-cloud-storage==2.1.0; python_version > '3.6'
gunicorn==20.1.0; python_version > '3.0'
gunicorn==19.10.0; python_version < '3.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 Cloud Storage blob and writes it to the Cloud Storage bucket.

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.

import logging
import os
from typing import Union

from flask import Flask, request
from import storage

app = Flask(__name__)

# Configure this environment variable via app.yaml

def index() -> str:
    return """
<form method="POST" action="/upload" enctype="multipart/form-data">
    <input type="file" name="file">
    <input type="submit">

@app.route('/upload', methods=['POST'])
def upload() -> str:
    """Process the uploaded file and upload it to Google Cloud Storage."""
    uploaded_file = request.files.get('file')

    if not uploaded_file:
        return 'No file uploaded.', 400

    # Create a Cloud Storage client.
    gcs = storage.Client()

    # Get the bucket that the file will be uploaded to.
    bucket = gcs.get_bucket(CLOUD_STORAGE_BUCKET)

    # Create a new blob and upload the file's content.
    blob = bucket.blob(uploaded_file.filename)


    # Make the blob public. This is not necessary if the
    # entire bucket is public.
    # See

    # The public URL can be used to directly access the uploaded file via HTTP.
    return blob.public_url

def server_error(e: Union[Exception, int]) -> str:
    logging.exception('An error occurred during a request.')
    return """
    An internal error occurred: <pre>{}</pre>
    See logs for full stacktrace.
    """.format(e), 500

if __name__ == '__main__':
    # This is used when running locally. Gunicorn is used to run the
    # application on Google App Engine. See entrypoint in app.yaml.'', port=8080, debug=True)

For more information

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