简体   繁体   English

在 AWS Amplify 部署后,static web 应用程序中的图像未显示

[英]Images in static web app not showing after AWS Amplify deployment

I'm having some trouble getting images to work after my app is deployed to AWS Amplify.在我的应用程序部署到 AWS Amplify 后,我无法让图像正常工作。 I created a pretty simple app that consists of just HTML, CSS and JS files (no angular/react/flutter etc.).我创建了一个非常简单的应用程序,它只包含 HTML、CSS 和 JS 文件(没有 angular/react/flutter 等)。 My folder structure is currently:我的文件夹结构目前是:

- .html files
- styles.css
--- scripts
   |___app.js
   |___config.js
   |___aws.js
--- images
   |___image1.PNG
   |___image2.PNG
   |___image3.PNG
   |___image4.PNG

In my app.js file, I've got a few dynamic image creations setup in the same way (just different variable names and image files):在我的app.js文件中,我以相同的方式设置了一些动态图像创建(只是变量名称和图像文件不同):

var image = document.createElement('img');
image.src = '../images/image1.png';
image.addEventListener('click', myFunction);
image.style.cursor = 'pointer';

Specifically the image.src line;特别是image.src行; I've tried different combinations such as:我尝试了不同的组合,例如:

  1. image.src = '../images/image1.png';
  2. image.src = './images/image1.png'; (one suggestion I found said to remove one of the periods (not sure why)) (我发现的一个建议是删除其中一个句号(不知道为什么))
  3. image.src = '/images/image1.png'; (placing the images folder inside the scripts folder) (将图像文件夹放在脚本文件夹中)
  4. image.src = 'image1.png'; (placing the actual images at the same level as the scripts) (将实际图像与脚本置于同一级别)
  5. I've even tried placing the images in an S3 bucket, and then using #3 add an image rewrite as described here: AWS Rewrite/Redirect Documentation .我什至尝试将图像放在 S3 存储桶中,然后使用 #3 添加图像重写,如下所述: AWS Rewrite/Redirect Documentation

No matter what I try, after pushing my code to GitHub and then after my AWS Amplify app finishes building the code, when I go to website, all the images are showing as if they couldn't be found and the dev console always shows something along the lines of Image could not be found. The server returned with: 404 Could not be found error无论我尝试什么,在将我的代码推送到 GitHub 之后,然后在我的 AWS Amplify 应用程序完成构建代码之后,当我将 go 发送到网站时,所有图像都显示为好像找不到它们并且开发控制台总是显示一些东西沿着Image could not be found. The server returned with: 404 Could not be found error Image could not be found. The server returned with: 404 Could not be found error . Image could not be found. The server returned with: 404 Could not be found error

The code works perfectly fine and the images show up on my local machine when I open the Live Preview using either Brackets.io or VS Code, just not when deployed to Amplify.当我使用 Brackets.io 或 VS 代码打开实时预览时,代码工作得非常好,图像显示在我的本地计算机上,只是在部署到 Amplify 时没有。

I've also tried setting up in the Amplify console the Rewrites and Redirects as it mentions in this Github forum: Github Forum but to no avail.我还尝试在 Amplify 控制台中设置重写和重定向,正如它在此 Github 论坛中提到的那样: Github 论坛但无济于事。 My current Rewrites and Redirects section of the Amplify console effectively look like what's shown in the first link.我当前的 Amplify 控制台的重写和重定向部分实际上看起来像第一个链接中显示的内容。

I've got to be missing some kind of setup to get this working but I'm not sure what it is.我必须缺少某种设置才能使其正常工作,但我不确定它是什么。 Hopefully it's a simple fix and I don't need to effectively re-do the project some other way.希望这是一个简单的修复,我不需要以其他方式有效地重新执行该项目。

Thank you in advance for any assistance.提前感谢您的任何帮助。

I figured it out.我想到了。 I decided to try and use GitHub Pages instead to see if the same problem occurs there and it does.我决定尝试使用 GitHub Pages 来查看是否存在相同的问题并且确实存在。 I did some research on why it wouldn't show up there and I found this article: GitHub Pages Stackoverflow which basically says that the filename, extension and folders are all case sensitive.我做了一些关于为什么它不会出现在那里的研究,我发现了这篇文章: GitHub Pages Stackoverflow基本上说文件名、扩展名和文件夹都是区分大小写的。 I didn't think that AWS Amplify would have this problem but it does.我不认为 AWS Amplify 会有这个问题,但确实有。

My files had .PNG as the file extension but in the code I used .png .我的文件有.PNG作为文件扩展名,但在我使用的代码中.png After changing it in the code and re-deploying, it worked.在代码中更改并重新部署后,它可以工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM