如何处理 javascript 文件具有“相对”文件的情况它使用的图像的路径?
当我将媒体目录上传到 S3 时,这些路径会中断。
我的流程如下:
我有一个脚本,可将整个媒体目录上传到 Amazon S3 CDN。 所以我的网站从该 CDN 下载图像/css/js。
但是,我的许多 JQuery 插件都具有“相对”定义的图像。 所以当它放到 S3 时,这条路径就不再有意义了。
在这种情况下我该怎么办? 我真的很想将我的媒体文件夹放在 S3 中,但我只是希望可以保留路径。
示例:
/media/js/fancybox/fancybox.js
/media/js/fancybox/fancybox-image.png
当我在 Apache/Django 服务器中运行它时,它可以工作,因为 fancybox.js 调用 fancybox-image.js,并且它会检测到它。
但是,当我在 Amazon S3 上运行它时:
s3.amazon.com/my_bucket/js/fancybox/fancybox.js
s3.amazon.com/my_bucket/js/fancybox/fancybox-image.png
javascript 改为调用此图像:
s3.amazon.com/my_bucket/fancybox-image.png
These paths break when I upload my media directory to S3.
Here's my process:
I have a script that uploads my entire media directory to Amazon S3 CDN.
So my website downloads images/css/js from that CDN.
But, a lot of my plugins from JQuery have images that are "relatively" defined.
So when it puts to S3, that path doesn't make sense anymore.
What do I do in this case?
I really want to put my media folder in S3, but I just wish the paths can be kept.
Example:
/media/js/fancybox/fancybox.js
/media/js/fancybox/fancybox-image.png
When I run this in my Apache/Django server, it works, since fancybox.js calls the fancybox-image.js, and it detects it.
However, when I run it on my Amazon S3:
s3.amazon.com/my_bucket/js/fancybox/fancybox.js
s3.amazon.com/my_bucket/js/fancybox/fancybox-image.png
The javascript calls this image instead:
s3.amazon.com/my_bucket/fancybox-image.png
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 JS 中定义一个 URLbase 变量,以便在开发环境中它类似于
URLbase = "http://localhost"
,在生产环境中它类似于URLbase = "http://s3.amazon.cn"。 com/my_bucket"
然后,当您定义 URL 时,您可以将 URLbase 添加到该位置,或者定义一个实用程序函数,将 URLbase 添加到路径(如果您有一些,这将使其更加灵活)您想要立即混合的基本 URL。)
Define a URLbase variable in your JS so that in your development environment it's something like
URLbase = "http://localhost"
and on production it'sURLbase = "http://s3.amazon.com/my_bucket"
Then when you're defining URLs you can prepend URLbase to the location, or define a utility function that will prepend the URLbase to the path (this would make it more flexible if you've got a few base URLs you want to mix in at once.)