簡體   English   中英

S3 文件的 PDF.js CORS 問題

[英]PDF.js CORS issue for S3 file

我正在嘗試使用 PDF.js 1.0.571 查看托管在 S3 Bucket 上的 PDF。 但是,我收到以下錯誤:

CORS 錯誤

類似問題工作 JSBin: http : //jsbin.com/pdfjs-helloworld-v2/7086/edit

不工作 JSBin: http : //jsbin.com/pdfjs-helloworld-v2/10562/edit

我的 S3 CORS 設置:

CORS 設置

用於測試的 S3 PDF: http : //sheethub.s3.amazonaws.com/sheets/0b26b42aa027c6b73855feb68b8c089e893f8114/original/another_guldove.pdf?1407157315

我的 CORS 設置錯誤嗎? 我在這里缺少什么? http://sheethub.s3.amazonaws.comhttp://s3.amazonaws.com/sheethub之間有區別嗎?

對於仍然面臨這個問題並登陸這里的人:

在您的 S3 CORS 配置上使用此配置。 按照此鏈接上的說明更新您的 CORS 配置。 不要忘記保存。

<!-- Sample policy -->
<CORSConfiguration>
    <CORSRule>
        <AllowedOrigin>*</AllowedOrigin>
        <AllowedMethod>GET</AllowedMethod>
        <AllowedMethod>HEAD</AllowedMethod>
        <MaxAgeSeconds>3000</MaxAgeSeconds>
        <AllowedHeader>Authorization</AllowedHeader>
    </CORSRule>
</CORSConfiguration>

<AllowedMethod>HEAD</AllowedMethod>有所不同。

就像我一樣,如果有人花了幾天的時間。

即使在 s3 上設置 cors 之后

<CORSConfiguration>
    <CORSRule>
        <AllowedOrigin>*</AllowedOrigin>
        <AllowedMethod>GET</AllowedMethod>
        <AllowedMethod>HEAD</AllowedMethod>
        <MaxAgeSeconds>3000</MaxAgeSeconds>
        <AllowedHeader>Authorization</AllowedHeader>
    </CORSRule>
</CORSConfiguration>

現在是數組格式:

[
   {
      "AllowedHeaders":[
         "*"
      ],
      "AllowedMethods":[
         "HEAD",
         "GET"
      ],
      "AllowedOrigins":[
         "*"
      ]
   }
]

an 不適用於 pdf.js 或任何 javascript 代碼。

只需添加一個帶有 url 的額外參數,例如。

Case-1:
    var url = "https://xxxx.xxx.xxxxx?token=yyy";
    url = url + '&origin='+ window.location.host;

Case-2:
    var url = "https://xxxx.xxx.xxxxx";
    url = url + '?origin='+ window.location.host;

您需要在您的服務器上啟用 CORS。 它基本上是通過將以下標頭添加到 Web 服務器的輸出中來完成的:

  • Access-Control-Allow-Credentials:true
  • 訪問控制允許方法:GET
  • 訪問控制允許來源: http : //www.yourdomain.com

您可以在此處閱讀有關類似案例的更多信息: http : //flexpaper.devaldi.com/questions-and-answers.jsp?qa=1560/remote-hosted-pdfs-not-being-displayed-stuck-loading

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM