Using Google Cloud Storage
You can use a Cloud Storage bucket to store and serve files, such as movies or images or other static content.
This document describes how to store and retrieve data using Cloud Storage in an App Engine app using the Google Cloud Storage client library. This client library provides error handling, retries, read buffering with prefetch, and uses HTTPS so your data is encrypted during transfer.
For more information about Cloud Storage, see the Cloud Storage documentation.
Contents
Prerequisites and setup
Activating a Cloud Storage bucket
Setting bucket and object permissions
Downloading the client library
Reading and writing to a bucket
Using the client library with the development app server
Pricing, quotas, and limits
Alternatives ways to access Cloud Storage
Prerequisites and setup
You need to complete the following:
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 for use with your application.
Activate a Cloud Storage bucket for your app to use, and set permissions if neccessary .
Download the client library.
Activating a Cloud Storage bucket
To use Cloud Storage, you'll need to activate at least one bucket. Typically, you might want to use the 5GB free default bucket, and upgrade this into a paid bucket if you need more storage. You can always activate and use other paid bucket if you want, but there is only one free default bucket per project.
To activate the free, default Cloud Storage bucket for your app:
Click Create under Default Cloud Storage Bucket in the App Engine settings page for your project. Notice the name of this bucket: it is in the form
<project-id>.appspot.com.If you need more storage than the 5GB limit, you can increase this by enabling billing for your project, making this a paid bucket. You will be charged for storage over the 5GB limit.
If you want to activate one or more paid buckets, follow the instructions under Creating a bucket to activate them.
Setting bucket and object permissions
By default, when you create a bucket for your project, your app has all the permissions required to read and write to it.
If you want to set permissions to allow other users to access the bucket and its contents, see Setting bucket permissions and Setting object permissions.
Downloading the client library
You can download the library using popular tools like Apache Maven, Apache Ivy, or Git, or you can download the library manually from the Maven repository. Choose your preferred method:
Maven
Maven users should include the following in their application's pom.xml file:
Note that RELEASE is a literal value that results in the download of the latest client library for your project.
Ivy
Ivy users should include the following in their application's ivy.xml file:
Git
If you have Git installed, you can clone the Google Cloud Storage client library's GitHub repository as follows:
Manual download
Visit the library's Maven repository and download the latest class, source, and JavaDoc JAR files:
In addition, you will need to download the following dependencies and include them in your application:
Note: If you use Maven or Ivy, the dependencies are automatically retrieved for you.
Reading and writing to a bucket
The following sample shows how to write a file to a Cloud Storage bucket, and how to retrieve a file:
java/example/src/com/google/appengine/demos/GcsExampleServlet.java
\[This section requires a browser that supports JavaScript and iframes.\]
Notice the sample shows the optional use of the Blobstore API for serving files stored in Cloud Storage. This can be a more cost effective and faster way to return files to the caller because it returns the file to the caller without your app having to read the file. For more information, see Using the Blobstore API with Google Cloud Storage.
Also, notice that the code above shows how to set the retry parameters, which control the retries that occur in case of network or resource issues. Normally, you should not have to set the retry parameters, because the default values will work for the majority of cases.
Using the client library with the development app server
You can use the client library with the development server. This provides Cloud Storage emulation using the local disk.
Note: Files saved locally are subject to the file size and naming conventions imposed by the local filesystem.
Pricing, quotas, and limits
There are no bandwidth charges associated with making Google Cloud Storage client library calls to Cloud Storage. However, there are operations charges; because the calls count against your URL fetch quota, as the library uses the URL Fetch service to interact with Cloud Storage. And There are operations and storage charges, however,
Notice that Google Cloud Storage is a pay-to-use service; you will be charged according to the Cloud Storage price sheet.
Alternatives ways to access Cloud Storage
Instead of using the client library, you could use the following:
Cloud Storage REST API. This could be a good choice if you need a specific feature not provided in the client library. However, this isn't compatible with the development server.
Cloud Storage Browser in the Google Cloud Platform Console. Useful for uploading objects quickly.
gsutil. A command line tool.
