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

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 Java on App Engine to set up your environment and project, and to understand how Java 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 java-docs-samples/flexible/cloudstorage

Edit project configuration and install dependencies

In app.yaml, set the BUCKET_NAME to the Cloud Storage you previously created for your project.

runtime: java
env: flex

- url: /.*
  script: this field is required, but ignored


In pom.xml, set as a dependency, and specify google-cloud-storage as the artifactID for that dependency; this provides the functions to use Cloud Storage.

<!--  Using libraries-bom to manage versions.
See -->


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 doPost request handler writes the file to the Cloud Storage bucket using Storage.create

Notice that to retrieve this file from Cloud Storage, you will need to specify the bucket name and the filename. You should store these values in your app for future use.

@WebServlet(name = "upload", value = "/upload")
public class UploadServlet extends HttpServlet {

  private static final String BUCKET_NAME = System.getenv("BUCKET_NAME");
  private static Storage storage = null;

  public void init() {
    storage = StorageOptions.getDefaultInstance().getService();

  public void doPost(HttpServletRequest req, HttpServletResponse resp)
      throws IOException, ServletException {
    final Part filePart = req.getPart("file");
    final String fileName = filePart.getSubmittedFileName();

    // Modify access list to allow all users with link to read file
    List<Acl> acls = new ArrayList<>();
    acls.add(Acl.of(Acl.User.ofAllUsers(), Acl.Role.READER));
    // the inputstream is closed by default, so we don't need to close it here
    Blob blob =
            BlobInfo.newBuilder(BUCKET_NAME, fileName).setAcl(acls).build(),

    // return the public download link

For more information

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