James King
James King

Reputation: 1614

Serving images directly from GCS in GAE using Blobstore API and Images API

Many questions and answers on Blobstore and Google Cloud Storage(GCS) are two or three years old, while things change dramatically these years. GCS is no longer a standalone service. It is integrated into Google App Engine (GAE) now.

Google seems to push GCS so hard that Blobstore is deprecated, for example,

The Files API feature used here to write files to Blobstore has been deprecated and is going to be removed at some time in the future, in favor of writing files to Google Cloud Storage and using Blobstore to serve them.

I believe it is high time to switch to GCS.

For example, www.example.com is a site built on GAE, while example.jpg is an image stored on GCS, I want to serve the image using the url http://www.example.com/images/example.jpg

This used to be impossible, but now it is possible thanks to the integration.

I found this:

https://developers.google.com/appengine/docs/python/googlecloudstorageclient/ says:

When the Blobstore API is used together with the Images API, you get a powerful way to serve images, because you can serve images directly from GCS, bypassing the App Engine app, which saves on instance hour costs.

I do know how to 'bypassing the App Engine app'. Is there any example on how to bypass GAE while serving the images using Blobstore API and Images API?

Upvotes: 4

Views: 3020

Answers (3)

Spain Train
Spain Train

Reputation: 6006

You actually do not need to use BlobStore at all now. The following will work to get the images API URL for serving images stored in GCS:

from google.appengine.api import images

images.get_serving_url(None, filename='/gs/<bucket>/<object>'))

Upvotes: 2

Vijay Parashar
Vijay Parashar

Reputation: 50

Serving images from 'www' is not a good idea if you are using www as you GAE Cname, to serve images you can create a new sub-domain.In our case we are using cdn.example.com, and we serve our images like http://cdn.example.com/images/example.jpg

How Do we do it.

create a GCS bucket with name cdn.example.com and place your images under /images path.

Specify your index and 404 pages and it will be good to go.

more on this https://cloud.google.com/storage/docs/website-configuration?hl=en

Upvotes: 0

Brandon Yarbrough
Brandon Yarbrough

Reputation: 38399

Instructions are here: https://developers.google.com/appengine/docs/python/images/functions#Image_get_serving_url

Start with an image hosted in Google Cloud Storage.

First, use the Blobstore API's create_gs_key() function to generate a blob key for your GCS image object. Then, pass that blob key into the Image API's get_serving_url() function.

The Image API will give you a special URL that skips over your app engine app and serves the image directly.

Upvotes: 3

Related Questions