Home > Enterprise >  configuring app.yaml handler for static image files
configuring app.yaml handler for static image files

Time:07-25

I have been trying to configure my app.yaml in the Google Cloud Platform.

I would like to achieve two things:

  1. redirect all URLs (except images) to the main index.html in the root folder. For example, example.com/abcd should be redirected to example.com.
  2. load any image requests from the folder named "mypics".

Here is my existing app.yaml file.

    runtime: python27
    api_version: 1
    threadsafe: true

    handlers:
    # image files
    - url: /(.*\.(bmp|gif|ico|jpeg|jpg|png))
      static_files: mypics/\1
      upload: mypics/(.*\.(bmp|gif|ico|jpeg|jpg|png))

    # URL redirect
    - url: /(.*)
      static_files: index.html
      upload: index.html

I have managed to get the redirects working. However, the images are not being loaded. I have reversed the order of the handlers, which does not seem to make a difference.

If I try to load the image directly, I get the following error:

Error: Not Found The requested URL /mypics/Diamond.png was not found on this server.

Thanks for your help.

CodePudding user response:

I suspect it has to do with how you specify the path to your images in your template files (i.e. html files). Let me explain with the result of the test I did on my machine

Previously,

Folder structure

  • Project Root

    • static

      • images

        • image_1.png

        • image_2.png

    • css

app.yaml

- url: /static
  static_dir: static

Code in index.html file was < img src="/static/images/image_1.png" alt="Image 1" >.

The above worked (i.e. images were properly displayed)

Then I changed to your code

 - url: /(.*\.(bmp|gif|ico|jpeg|jpg|png))
  static_files: mypics/\1
  upload: mypics/(.*\.(bmp|gif|ico|jpeg|jpg|png)) 

The images were no longer displayed. I also tried

- url: /static/(.*\.(bmp|gif|ico|jpeg|jpg|png))
  static_files: mypics/\1
  upload: mypics/(.*\.(bmp|gif|ico|jpeg|jpg|png))

but the images were still not displayed.

I then went back and modified the routing in app.yaml to

- url: /static/images/(.*\.(bmp|gif|ico|jpeg|jpg|png))
  static_files: mypics/\1
  upload: mypics/(.*\.(bmp|gif|ico|jpeg|jpg|png))

The change here is that I specified the full path I used in my index.html i.e. static/images/ and the images got displayed again

  • Related