Using Cloud Storage with Java for the App Engine Standard Environment

This part of the Bookshelf tutorial for Java shows how to store images in Google Cloud Storage.

This page is part of a multi-page tutorial. To start from the beginning and see instructions for setting up, go to Java Bookshelf App.

Creating a Cloud Storage bucket

The following instructions show how to create a Cloud Storage bucket. Buckets are the basic containers that hold your data in Cloud Storage.

To create a bucket:

  1. Invoke the following command in a terminal window:

    gsutil mb gs://[YOUR-BUCKET-NAME]

  2. Set the bucket's default ACL to public-read, which enables users to see their uploaded images:

    gsutil defacl set public-read gs://[YOUR-BUCKET-NAME]

Running the app on your local machine

To run the app locally:

  1. In the getting-started-java/bookshelf-standard/3-binary-data directory, enter this command to start a local web server:

    mvn -Plocal clean appengine:devserver -Dbookshelf.bucket=[YOUR-BUCKET]
    Replace [YOUR-BUCKET] with your bucket name.
  2. In your web browser, navigate to http://localhost:8080

Now you can browse the app's web pages, add books with cover images, edit books, and delete books.

Deploying the app to the App Engine standard environment

To deploy to the App Engine standard environment:

  1. Make sure you have fully exercised the Bookshelf application locally, especially creating at least one book and then clicking My Books. This creates a required Cloud Datastore index that is uploaded with Bookshelf. Note that building the application using clean deletes that local index, so be sure to do this on the build of the application that you are going to deploy.
  2. In the getting-started-java/bookshelf-standard/3-binary-data directory, enter this command to deploy the app:
    mvn appengine:update -Dappengine.appId=[YOUR-PROJECT-ID] -Dappengine.version=[YOUR-VERSION] -Dbookshelf.bucket=[YOUR-BUCKET].appspot.com
    Replace [YOUR-PROJECT-ID] with your project ID and [YOUR-VERSION] with your version, for example, 1, or 2, or some other string value you want to use.
  3. In your web browser, enter this address:
    https://[YOUR-PROJECT-ID].appspot.com
    Replace [YOUR-PROJECT-ID] with your project ID.

After you update your app, you can redeploy the updated version by entering the same command you used to deploy the app the first time, specifying the same project ID and version: this overwrites the currently deployed app. If you specify a different version string in the update command line, the new deployment creates a new version of your app and promotes it to be the currently serving version.

You can reduce costs by deleting the non-serving versions of your app.

To delete an app version:

  1. In the GCP Console, go to the App Engine Versions page.

    Go to the Versions page

  2. Click the checkbox next to the non-default app version you want to delete.
  3. Click the Delete button at the top of the page to delete the app version.

For complete information about cleaning up billable resources, see the Cleaning up section in the final step of this tutorial.

Application structure

Binary data sample structure

The application uses Cloud Storage to store binary data, pictures in this case. The app continues to use Cloud Datastore for book information.

Understanding the code

This section walks you through the application code and explains how it works.

Handling user uploads

It is simple to use the Google Cloud Storage API for Java. In most cases, a single line is all you need to authenticate locally.

static {
  storage = StorageOptions.getDefaultInstance().getService();
}

Uploading blobs to Cloud Storage

Verify the file matches your requirements before uploading it.

/**
 * Checks that the file extension is supported.
 */
private void checkFileExtension(String fileName) throws ServletException {
  if (fileName != null && !fileName.isEmpty() && fileName.contains(".")) {
    String[] allowedExt = {".jpg", ".jpeg", ".png", ".gif"};
    for (String ext : allowedExt) {
      if (fileName.endsWith(ext)) {
        return;
      }
    }
    throw new ServletException("file must be an image");
  }
}

Then, make the filename unique by appending a timestamp to it. Using storage.create, pass a BlobInfo created with bucketName and fileName, and set the access control list so that all users can read and pass the file's InputStream. The result is the public URL for the object.

/**
 * Uploads a file to Google Cloud Storage to the bucket specified in the BUCKET_NAME
 * environment variable, appending a timestamp to end of the uploaded filename.
 */
@SuppressWarnings("deprecation")
public String uploadFile(FileItemStream fileStream, final String bucketName)
    throws IOException, ServletException {
  checkFileExtension(fileStream.getName());

  DateTimeFormatter dtf = DateTimeFormat.forPattern("-YYYY-MM-dd-HHmmssSSS");
  DateTime dt = DateTime.now(DateTimeZone.UTC);
  String dtString = dt.toString(dtf);
  final String fileName = fileStream.getName() + dtString;

  // the inputstream is closed by default, so we don't need to close it here
  BlobInfo blobInfo =
      storage.create(
          BlobInfo
              .newBuilder(bucketName, fileName)
              // Modify access list to allow all users with link to read file
              .setAcl(new ArrayList<>(Arrays.asList(Acl.of(User.ofAllUsers(), Role.READER))))
              .build(),
          fileStream.openStream());
  // return the public download link
  return blobInfo.getMediaLink();
}
Was this page helpful? Let us know how we did:

Send feedback about...