TimothyAURA
TimothyAURA

Reputation: 1359

App Engine - app.yaml handler for images

I'm trying to understand app.yaml a little better.

GCP docs recommend creating a handler for your images like this:

# Serve images as static resources.
- url: /(.+\.(gif|png|jpg))$
  static_files: \1
  upload: .+\.(gif|png|jpg)$
  application_readable: true

However I use the much simpler:

- url: /assets/images
  static_dir: assets/images

I would like to understand the extra parameters and regular expressions Google has used.

What are the differences between Google's and my handler?

PS at the moment my handler is working, but when I use the Google handler my images do not load.

Upvotes: 0

Views: 1091

Answers (1)

Dan Cornilescu
Dan Cornilescu

Reputation: 39824

The explanation is in the static_files row from the Handlers element table:

static_files

Optional. A static file pattern handler associates a URL pattern with paths to static files uploaded with the application. The URL pattern regular expression can define regular expression groupings to be used in the construction of the file path. You can use this instead of static_dir to map to specific files in a directory structure without mapping the entire directory.

Example:

handlers:
# All URLs ending in .gif .png or .jpg are treated as paths to
# static files in the static/ directory. The URL pattern is a
# regular expression, with a grouping that is inserted into the
# path to the file.
- url: /(.*\.(gif|png|jpg))$
  static_files: static/\1
  upload: static/.*\.(gif|png|jpg)$

The url is the requested path while static_files and upload are real file paths relative to your app/service source directory, with \1 and .*\.(gif|png|jpg)$ respectively replaced by the url regex matching grouping value - whatever is inside the outer round paranthesis.

So a request to /a_file.gif will match the url regex, producing a a_file.gif grouping. Which will be replaced into static_files and upload as static/a_file.gif - the actual path of the file in your app source code.

With your static_dir config any file that exists under assets/images would be served if a matching request to /assets/images/<the_file> is made, regardles of what that filename is.

With a static_files config you can select only specific filenames to be served (that match the regex pattern) and you can make them appear with a different name and/or in a different path than the one where they really are relative to the app dir.

Your static_files config should be working if you make requests to the right path, like /assets/images/<some_file>.png for example (I assume that's where your image files exist).

But if you want, for example, to serve a file located under that assets/images dir but requested simply as /<some_file>.png (i.e. without that path prefix) you'd need to configure it differently:

# Serve images as static resources.
- url: /(.+\.(gif|png|jpg))$
  static_files: assets/images/\1
  upload: assets/images/.+\.(gif|png|jpg)$

Also check that you don't have overlapping static_dir and/or static_files paths configured - that can cause obscure problems, see Static files are missing

Upvotes: 3

Related Questions