TylerW
TylerW

Reputation: 1553

Resize images served directly from GCS in GAE using get_serving_url?

Before I switched to GCS, I used to be able to use the resize feature for images on the fly (=s200). Is this possible with images stored on GCS? I can't seem to get it to work.

I upload the image to GCS, save the blobkey and then use get_serving_url to get the image url. Using that URL, the image shows up on my site. But if I append the resize on the fly feature (=s200), I get a 404 error.

Here's the code I'm using in Flask:

def gcs_upload_image (image, filename):

  write_retry_params = gcs.RetryParams(backoff_factor=1.1)

  with gcs.open(filename,
              mode='w',
              content_type=image.mimetype,
              options={'x-goog-meta-foo': 'foo',
                             'x-goog-meta-bar': 'bar'},
              retry_params=write_retry_params) as imageFile:
    image.save(imageFile)

  blobstore_filename = '/gs' + filename
  return blobstore.create_gs_key(blobstore_filename)

@mod.route("/account", methods=["GET", "POST"])
@login_required
def account():
    if request.method == "GET":
        return render_template('users/account.html')
    else:
        image = request.files.get('file', None)
        bucket = current_app.config['BUCKET']
        filename = "/".join(['', bucket, g.user.key.id()])
        if image:
            if g.user.avatar:
                blobstore.delete(g.user.avatar)

            gcs_key = gcs_upload_image(image, filename)
            blobkey = blobstore.BlobKey(gcs_key)
            g.user.avatar = blobkey
            g.user.put()
        return redirect(url_for("users.account"))

@mod.route('/u/<userid>')
def profile(userid):
    user = User.get_by_id(userid)
    url = get_serving_url(user.avatar)
    return render_template('users/profile.html', user=user, url=url)

Upvotes: 1

Views: 441

Answers (1)

Mars
Mars

Reputation: 1422

You could just use the size and crop arguments to resize the image on the fly when calling get_serving_url().

Upvotes: 1

Related Questions