简体   繁体   English

Django 中的 Static 文件与 AWS S3

[英]Static files in Django with AWS S3

I just moved the static files of my Django project into AWS S3.我刚刚将我的 Django 项目的 static 文件移动到 AWS S3 中。 It works so far.到目前为止它有效。 But my.css files contain relative paths, for example to fonts, for example:但是 my.css 文件包含相对路径,例如到 fonts,例如:

@import "variables";

@font-face {
  font-family: '#{$font-family}';
  src:  url('#{$font-path}/#{$font-family}.eot?4zay6m');
  format('svg');
  font-weight: normal;
  font-style: normal;
}

My browser console shows that the path to these file is set correctly, for example我的浏览器控制台显示这些文件的路径设置正确,例如

https://mybucket.s3.amazonaws.com/static/plugins/fonts/myfont.ttf?4zay6m

but I get a permission denied, because unlike the other (from django set paths), these paths do not contain the "Amazon stuff" after the question mark like但我得到一个权限被拒绝,因为与其他路径不同(来自 django 设置路径),这些路径在问号之后不包含“亚马逊的东西”

https://mybucket.s3.amazonaws.com/static/js/jquery.tablednd.js?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=XXXXXXX

Is there a simple workaroud?有简单的解决方法吗?

Thanks in advance.提前致谢。

You can do solve your problem using:您可以使用以下方法解决您的问题:

  1. Set S3 bucket as static web hosting: Bucket -> Properties -> Static website hosting -> Set root file and error file as index.html Set S3 bucket as static web hosting: Bucket -> Properties -> Static website hosting -> Set root file and error file as index.html
  2. Use public end point as URL to access your content like css, js or images使用公共端点作为 URL 来访问您的内容,例如 css、js 或图像

Your S3 is not public static hosting to access without Access token of AWS account.您的 S3 不是公共的 static 托管,无需 AWS 帐户的访问令牌即可访问。

For example how to do S3 bucket as hosting see this blog: https://www.internetkatta.com/host-angular-2-or-4-or-5-version-in-aws-s3-using-cloudfront例如如何将 S3 存储桶作为托管,请参阅此博客: https://www.internetkatta.com/host-angular-2-or-4-or-5-version-in-aws-s3-using-cloudfront

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

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