问题描述
在Rails应用中,使用pdfjs_viewer-rails
gem( https://github.com/senny/pdfjs_viewer-rails ),我一直看到此错误:
In a Rails app, using the pdfjs_viewer-rails
gem (https://github.com/senny/pdfjs_viewer-rails), I keep seeing this error:
PDF.js v1.3.91 (build: d1e83b5)
Message: file origin does not match viewer's
我认为这是因为PDF托管在Amazon S3上,并且我已在此处阅读了有关CORS的讨论( https://github.com/mozilla/pdf.js/wiki/Frequently-Asked-Questions#faq-xhr ).但是,我无法组装有效的COSR配置.
I presume this is because the PDF is hosted on Amazon S3, and I have read through the discussion of CORS here (https://github.com/mozilla/pdf.js/wiki/Frequently-Asked-Questions#faq-xhr). However, I have been unable to assemble a COSR configuration that works.
我当前的存储桶配置是:
My current bucket configuration is:
<?xml version="1.0" encoding="UTF-8"?>
<CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
<CORSRule>
<AllowedOrigin>*</AllowedOrigin>
<AllowedMethod>GET</AllowedMethod>
<AllowedHeader>Range</AllowedHeader>
<MaxAgeSeconds>3000</MaxAgeSeconds>
<ExposeHeader>Accept-Ranges</ExposeHeader>
<ExposeHeader>Content-Range</ExposeHeader>
<ExposeHeader>Content-Encoding</ExposeHeader>
<ExposeHeader>Content-Length</ExposeHeader>
<AllowedHeader>Authorization</AllowedHeader>
</CORSRule>
</CORSConfiguration>
为了安全起见,在我的rails应用程序中,我有:
and just to be safe, in my rails app I have:
config.action_dispatch.default_headers = {
'Access-Control-Allow-Origin' => '*',
'Access-Control-Request-Method' => %w{GET}.join(",")
}
还有其他人能够克服这个问题吗?有谁知道与pdf.js和Amazon s3兼容的CORS政策?
Has anyone else been able to overcome this? Does anyone know of a CORS policy that works with pdf.js and amazon s3?
推荐答案
就我而言,问题与CORS无关.我不得不覆盖/pdfjs_viewer/viewer.js
并将我的开发和生产url包含在7000行附近的HOSTED_VIEWER_ORIGINS
声明中.
In my case the problem was not CORS related. I had to overwrite /pdfjs_viewer/viewer.js
and include my development and production urls in the HOSTED_VIEWER_ORIGINS
declaration around line 7000.
这篇关于PDF.js消息:文件来源与查看者的来源不匹配(Amazon S3)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!