Using App Engine APIs for Cloud Storage

This solution is not recommended: This page describes how to use the
superseded App Engine APIs for Cloud Storage. Apps that use these APIs can only
run in App Engine and will need to upgrade to the
recommended APIs before migrating to newer
App Engine runtimes.

The recommended APIs for Cloud Storage are in the Google Cloud Client Library and
enable your app to run in App Engine as well as other environments that support
Java 8.

Note: If you use Maven or Ivy, the dependencies are automatically retrieved
for you.

ACLs and the App Engine client library

An app using the client library cannot change the bucket ACL, but it can specify
an ACL that controls access to the objects it creates. The available ACL settings
are described under documentation for the
FcsFileOptions
class.

Cloud Storage and subdirectories

The App Engine client library for Cloud Storage lets you supply
subdirectory delimiters when you create an object, but there are no true
subdirectories in Cloud Storage. Instead, a subdirectory in Cloud Storage is a
part of the object filename.

For example, you might assume that creating an object
somewhere/over/the/rainbow.mp3 would store the file rainbow.mp3 in the
subdirectory somewhere/over/the/. Instead, the object name is set to
somewhere/over/the/rainbow.mp3.

Note: The Cloud Storage client library provides a configurable mechanism for
automatic request retries in event of timeout failures when accessing
Cloud Storage. The same mechanism also provides
exponential backoff to determine an
optimal processing rate.

To change the default values for retries and
backoff, use the
RetryParams class.

Using the App Engine client library with the development app server

You can use the client library with the development server. However because
there is no local emulation of Cloud Storage, all requests to read and write
files must be sent over the Internet to an actual Cloud Storage bucket.

To use the client library with the development app server, run dev_appserver.py
with the flag --default_gcs_bucket_name [BUCKET_NAME], replacing
[BUCKET_NAME] with the name of the Cloud Storage bucket you are using.

This flag controls the bucket that will be returned when your application
calls file.DefaultBucketName(ctx).

Reading and Writing to Google Cloud Storage

Required imports

The following snippet shows the imports you need for Cloud Storage access via
the client library:

This sample writes a new file to Cloud Storage, or, if a file with the same name
already exists, it overwrites it. This is useful because once you write a file
to Cloud Storage, you cannot modify it. To change the file, you must make your
modifications to a copy of the file, then overwrite the old file.

The code above decodes the Base64
encoded image ID and retrieves the filename of an image identified by its
image_id from the images table. The filename is converted to a valid Cloud
Storage filename using GcsFilename.

The file is deleted from the bucket using gcsService.delete.
Finally, it removes the record of its usage in the blogpostImage table.