This document describes how to store and retrieve data using Cloud Storage in an App Engine app using the App Engine client library for Cloud Storage. It assumes that you completed the tasks described in Setting Up for Google Cloud Storage to activate a Cloud Storage bucket and download the client libraries. It also assumes that you know how to build an App Engine application, as described in the Quickstart for Python 2 App Engine standard environment.
Required imports
The main.py
file contains the typical imports used for accessing Cloud Storage
via the client library:
You need the os
module and the app_identity
API to get the
default bucket
name at runtime. Note that if you don't use the default bucket, you'll need some
other way to supply the bucket name.
Specifying the Cloud Storage bucket
Before doing any operations in Cloud Storage, you need to supply the bucket name. The easiest way to do this is to use the default bucket for your project, which can be obtained as follows:The call to get_default_gcs_bucket_name
succeeds only if you have created
the default bucket
for your project.
Writing to Cloud Storage
The following sample shows how to write to the bucket:
Notice that in the call to open
the file for write, the sample specifies
certain Cloud Storage headers that write custom metadata for the file; this
metadata can be retrieved using cloudstorage.stat
. You can find the list of
supported headers in the cloudstorage.open
reference.
Notice also that the x-goog-acl
header is not set. That means the default
Cloud Storage ACL of public read is
going to be applied to the object when it is written to the bucket.
Finally, notice the call to close
the file after you finish the write. If you
don't do this, the file is not written to Cloud Storage. Be aware that after
you call close
, you cannot append to the file. If you need to modify a file,
you'll have to open the file again in write mode, which does an overwrite, not
an append.
Reading from Cloud Storage
The following sample shows how to read a file from the bucket:
The filename
argument is specified in the format of
YOUR_BUCKET_NAME/PATH_IN_GCS
. The sample
shows how to display selected lines from the file being read, in this
case, the opening line and the last 1K lines, using seek
.
Notice that no mode is specified in the code above when the file is opened for
read. The default for open
is read-only mode.
Listing bucket contents
The sample code shows how to page through a bucket with a large number of files,
using the marker
, and max_keys
parameters to page through
a list of the contents of the bucket:
Note that the complete file name is displayed as one string without directory
delimiters. If you want to display the file with its more recognizable directory
hierarchy, set the delimiter
parameter to the directory delimiter you want to
use.
Deleting files in Cloud Storage
The code below demonstrates how to delete a file from Cloud Storage using the
cloudstorage.delete()
method (imported as gcs
).
This example cleans up the files that were written to the bucket in the Writing to Cloud Storage section.
What's next
- Visit the API Reference documentation.
- Learn more about using Cloud Storage as well as view additional samples and tutorials in the Cloud Storage documentation.