Skip to content

Commit

Permalink
Merge pull request GoogleCloudPlatform#1166 from andrewsg/images-blob…
Browse files Browse the repository at this point in the history
…store

Add get_serving_url sample for images/blobstore
  • Loading branch information
andrewsg authored Oct 19, 2017
2 parents d45749d + bd12a13 commit 338042a
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
23 changes: 22 additions & 1 deletion appengine/standard/images/api/blobstore.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,26 @@ def get(self):
# [END thumbnailer]


app = webapp2.WSGIApplication([('/img', Thumbnailer)], debug=True)
class ServingUrlRedirect(webapp2.RequestHandler):
def get(self):
blob_key = self.request.get("blob_key")

if blob_key:
blob_info = blobstore.get(blob_key)

if blob_info:
# [START get_serving_url]
url = images.get_serving_url(
blob_key, size=150, crop=True, secure_url=True)
# [END get_serving_url]
return webapp2.redirect(url)

# Either "blob_key" wasn't provided, or there was no value with that ID
# in the Blobstore.
self.error(404)


app = webapp2.WSGIApplication(
[('/img', Thumbnailer),
('/redirect', ServingUrlRedirect)], debug=True)
# [END all]
21 changes: 21 additions & 0 deletions appengine/standard/images/api/blobstore_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,24 @@ def test_img_missing(app):
def test_no_img_id(app):
# No blob_key, should get error
app.get('/img', status=404)


def test_url_redirect(app):
with mock.patch('blobstore.images') as mock_images:
with mock.patch('blobstore.blobstore') as mock_blobstore:
mock_blobstore.get.return_value = b'123'
mock_images.get_serving_url.return_value = 'http://lh3.ggpht.com/X'

response = app.get('/redirect?blob_key=123')

assert response.status_int == 302


def test_url_redirect_missing(app):
# Bogus blob_key, should get error
app.get('/redirect?blob_key=123', status=404)


def test_url_redirect_no_key(app):
# No blob_key, should get error
app.get('/redirect', status=404)

0 comments on commit 338042a

Please sign in to comment.