Uploading images along with Google App Engine - google-app-engine

I'm working on a Google App Engine project.
My app is working and looking correct locally, but when I try to upload images in an image directory, they're not being displayed at appspot.
As a little troubleshoot, I put a HTML page in "/images/page2.html" and I can load that page at the appspot, but my pages don't display my images. So, it's not a problem with my path.
As another sanity check, I'm also uploading a style sheet directory with .css code in it, and that's being read properly.
I have a suspicion that the problem lies in my app.yaml file.
Any ideas?
I don't want to paste all the code here, but here are some of the key lines. The first two work fine. The third does not work:
<link type="text/css" rel="stylesheet" href="/stylesheets/style.css" />
Page 2
<img src="/images/img.gif">
This is my app.yaml file
application: myApp
version: 1
runtime: python
api_version: 1
handlers:
- url: /stylesheets
static_dir: stylesheets
- url: /images
static_dir: images
- url: /.*
script: helloworld.py

You have to configure app.yaml for static content such as images and css files
Example:
url: /(.*\.(gif|png|jpg))
static_files: static/\1
upload: static/(.*\.(gif|png|jpg))
For more info check out:
http://code.google.com/appengine/docs/configuringanapp.html

I'll bet your problem is that you're using Windows.
If that's the case, I believe you need a preceding slash for your static_dir value.

I am using the Java version of App engine, and I faced a similar issues with the server not able to serve static images.
What worked ultimately was to change the AppEngine config file "appengine-web.xml" in my case to contain
<static-files>
<include path="**.*"/>
<include path="/images/**.*" />
</static-files>
My images are in the /images directory and HTML and CSS are in . directory which is at the WEB-INF level

#jamtoday The preceding slash didn't make a difference, but it did get me started figuring out what each app needs to be told what about my directory structure.
So, I have nothing very conclusive to add, but I wanted to follow up, because I got it working, but I didn't explore all the issues after I got it working.
One change that helped was to stop working from a HwlloWorld/src/ directory and start working in the HelloWorld/ directory. It seems like the dev_appserver picked up all the dependencies, but the remote server didn't. Essentially, the relative path of my local links didn't match the relative path of the links after uploading.
I also realized that the dev-appserver relies on the .yaml file, as well as the appcfg script. That is. . .if you add a directory to your project, and then try to link to files in that directory, you need to add the directory to the .yaml file, and then restart the dev-appserver to pick up on this.
So, there are probably ways to handle what I was originally trying to do if you give the .yaml file the right info, but changing to a different directory structure locally handled it for me.

<img src="/images/img.gif">
this line can't show you the image.
Try this one:
1-Create a class to handle "image request"
class GetImage(webapp.RequestHandler):
def get(self):
self.response.headers['Content-Type'] = 'image/jpg'
self.response.out.write(image_object)
2-In your page.html:
<img src="/image"
3-At the main function in your code.py:
application = webapp.WSGIApplication(('/image', GetImage), debug=True)
have fun

Related

React Static Website Problem Deploying on GCP (can't find main.js or main.css)

I followed this tutorial: https://cloud.google.com/storage/docs/hosting-static-website to host my React webapp. Here is what I have so far:
// app.yaml
runtime: nodejs10
handlers:
- url: /static
static_dir: build/static
- url: /(.*\.(json|ico|js))$
static_files: build/\1
upload: build/.*\.(json|ico|js)$
- url: /.*
static_files: build/index.html
upload: build/index.html
(I've tried MANY configurations here)
I run npm run build and put the build directory in GCP Cloud Storage along with app.yaml. When I go to my website I get this:
This happens when I hit the root url.
It looks like it is looking at the static directory which should then look at build/static according to my app.yaml.
If I go directly to the webpage such as https://www.drinkingbuddy.io/build/static/js/main.39aaab31.js, the browser displays the file as expected. So it is there being hosted but something is messed up with pathing? I'm not sure.
Testing locally, everything works. Even when serving the build files.
Any guidance would be greatly appreciated! Thank you.
EDIT:
Another thing is that when I go to the base url, it just shows the document tree.
EDIT 2:
When accessing the files directly from the bucket such as https://www.storage.googleapis.com/drinkingbuddy-static/build/index.html, I then get this error:
This XML file does not appear to have any style information associated with it. The document tree is shown below.
<Error>
<Code>UserProjectAccountProblem</Code>
<Message>User project billing account not in good standing.</Message>
<Details>The billing account for the owning project is disabled in state absent</Details>
</Error>
But looking through my account I don't see any billing issues.
EDIT 3:
Now I am able to get to .../build/index.html and it displays my site. but the root / is still throwing errors saying it can't find main.js. I added homepage: "." to my package.json.
I was able to get it to work by doing homepage:"/build". Now I can access the root url and get expected behavior.

Google App Engine serves static CSS files with wrong mime-type

I've been working on a new site to be hosted on Google App Engine. I've been deploying my app to a development project and it is hosted here:
Development: https://herdboss-dev.appspot.com/
It works fine.
But when I take the exact same code and deploy it to my other project which going to be the real, production website, it is serving some of the css as application/octet-stream instead of text/css and so those files aren't being parsed by the browser, so almost all of my css is not working on the production site:
Production: https://herdboss-prod.appspot.com/
Even weirder is that SOME of the css is being served correctly. /css/normalize.css is being served as text/css but /css/site.css is being served as application/octet-stream.
https://herdboss-prod.appspot.com/css/normalize.css
https://herdboss-prod.appspot.com/css/site.css
https://herdboss-prod.appspot.com/js/jquery-ui-1.12.1.custom-theme/jquery-ui.min.css
My app.yaml has a static handler for css files:
- url: /css
static_dir: css
secure: always
I tried adding a mime_type as well but that didn't change anything:
- url: /css
static_dir: css
mime_type: 'text/css'
secure: always
EDIT:
While experimenting, I cut my site.css in half, deployed, and then it started serving correctly. Then I reinstated the full size site.css, deployed... and it's still serving properly now.
But my jquery-ui.min.css is still serving as octet-stream. This is crazy.
EDIT2:
And it's serving my svg's with the wrong mime type as well.
Is mime-typing just utterly broken in the GAE? If so, why is it working on my dev gae?
My solution was:
duplicate the offending file and rename it
change one byte (otherwise it will not be uploaded/updated)
verify that it downloads as stylesheet
use the renamed, changed css file in your html
Wait for the original to get served correctly: after about 24h the problem fixed itself
There is an issue on the GAE issue tracker: https://issuetracker.google.com/issues/186012458
Initially, I tested a workaround but this workaround broke after a day: I modified my HTML to load the stylesheets from a version server basically cross-site as (hxxps://VERSION-dot-SERVER.appspot.com/static/stylesheet.css). The version server served the correct mime-type ... but only for some time.
I suspect the problem is a wrong mime-type due to an extremely rare race condition (cache access/refresh during upload?). It is simply not possible to trick the Google Frontend to refresh the file. Even appending .../styles.css?x=1 or switching back to an older code version will not trigger a refresh.
Files and their mime-types seem to be stored based on their hashed-values, so re-upload of a file with the same content has no effect even if it is simply renamed. You will see an "uploaded 0 files" message if you deploy a version with a duplicate file. It has to be changed and renamed.
I did check both of your sites: https://herdboss-prod.appspot.com/, https://herdboss-prod.appspot.com/
I can see that you have several CSS files: normalize.css, jquery-ui.min.css, base-min.css, grids-min.css, grids-responsive-custom.css, css?family=Open+Sans,
site.css, ie-is-terrible.css, css_compiled.css.
All of them served correctly as Type: stylesheet
If you encounter this problem I recommend performing following test:
Check content type in Developer Console > Network with the option Disable Cache > And reload page. If it helps: clean cache of the browser.
Check content of the page in different browser, as content can be interpreted differently depending on the rendering engine.
If 1 and 2 does not work, and you are using nginx, it is most probably configuration problem. Change the file /etc/nginx/conf.d/default.conf. Put /etc/nginx/mime.types in the 'http' section:
http / {
include /etc/nginx/mime.types;
default_type application/octet-stream;
....
}
if it is still not working, move it to location section
location / {
include /etc/nginx/mime.types;
...
}

Google App Engine: Only serve existing files in static directory

Is there a way to only serve existing files in a static directory? For example in my app.yaml file I have
- url: /img
static_dir: img
Which works fine if you go to /img/exists.jpg. However if you go to /img/doesnt-exist.jpg you get an ugly Error: Not Found message. Is there a way to make it skip this file and go to this script so that I can add a custom 404 message?
- url: /.*
script: index.php
According to the current documentation, you can configure a custom error response.

How to Map Static Handlers in App Engine?

I just starting to learn how to develop for App Engine and I am trying to organize my file structure and I have my static files under this hierarchy:
root/themes/default/assets/styles
for my css files and my js is within:
root/themes/default/assets/scripts
I am having trouble getting AE to serve the files when I put them in these folders. If I put the css and js files in:
root/themes/default/assets
I have no issues of them being served to the browser if I use the following in my app.yaml:
- url: /themes/(.+)/assets/(.+)
static_files: themes/\1/assets/\2
upload: themes/(.+)/assets/(.+)
I have tried several ways by trying to follow the docs but I am just not getting it. My question is what would be the proper way of writing the handlers to get to theses files with my organizational structure?
Your help is appreciated. Thnx
It might be your regexes are too greedy - try something like
- url: /themes/(.+)/assets/(styles|scripts)/(.+)
static_files: themes/\1/assets/\2/\3
upload: themes/(.+)/assets/(styles|scripts)/(.+)
?
Unless you are trying to catch some very particular 404s I would use something like this:
- url: /themes
static_dir: themes
Turned out that all the methods I was trying would have worked (including the above answers - so I upped them). The reason why they were not was because there was an issue where the folders were not being deployed by Git which was bad timing making me think it was me. Once the folders were deployed everything worked as it should.
Thnx again for your help.

app.yaml working differently on Local and on the Cloud

I've been playing around with GAE for PHP. Here is my Github code and it is running at http://shining-weft-626.appspot.com/
~/form gives me a 404 online but works perfectly on my local machine. Someone help.
/form is (bizarrely) a reserved URL on appengine, you'll have to pick something else: docs
The URL /form is working fine.
You are getting 404's for the URLs /form/ and /forms, because they do not match anything in your app.yaml.
- url: /form
script: form.php
- url: /
script: index.php
You code looks correct, if you committed and pushed it as shown. I think that you may be getting an error when loading the app.yaml, because you point to directories that do not exist. Try adding /js and /images to your /assets directory. They are missing now. Commit, and push again.
(Or, delete those mappings from your app.yaml)

Resources