Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PDF.js CORS issue for S3 file

I'm trying to view a PDF hosted on an S3 Bucket using PDF.js 1.0.571. However, I'm getting the following error:

CORS error

Working JSBin from a similar issue: http://jsbin.com/pdfjs-helloworld-v2/7086/edit

Not Working JSBin: http://jsbin.com/pdfjs-helloworld-v2/10562/edit

My S3 CORS Setup:

CORS setup

S3 PDF for testing: http://sheethub.s3.amazonaws.com/sheets/0b26b42aa027c6b73855feb68b8c089e893f8114/original/another_guldove.pdf?1407157315

Is my CORS setup wrong? What am I missing here? Is there a difference between http://sheethub.s3.amazonaws.com and http://s3.amazonaws.com/sheethub ?

like image 666
Yos Riady Avatar asked Aug 03 '14 12:08

Yos Riady


People also ask

How do I update my AWS S3 CORS?

In the Amazon S3 console, choose the bucket you want to edit. Select the Permissions tab, and scoll down to the Cross-origin resource sharing (CORS) panel. Choose Edit, and type your CORS configuration in the CORS Configuration Editor, then choose Save.

What is the use of CORS in AWS?

Cross-origin resource sharing (CORS) defines a way for client web applications that are loaded in one domain to interact with resources in a different domain. With CORS support, you can build rich client-side web applications with Amazon S3 and selectively allow cross-origin access to your Amazon S3 resources.


2 Answers

For people still facing this issue and landed here :

Use this config on your S3 CORS configuration. Follow instruction on this link to update your CORS config. Don't forget to save.

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

<AllowedMethod>HEAD</AllowedMethod> makes the difference.

like image 111
Gautier Avatar answered Oct 15 '22 10:10

Gautier


Just like me, if someone is spending days on it.

Even after setting cors on s3

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

which is now in array format:

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

an doesn't works with pdf.js or any javascript code.

Just add one extra parameter with url, eg.

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;
like image 5
Bilal Siddiqui Avatar answered Oct 15 '22 10:10

Bilal Siddiqui