I'm trying to download .jpg & .pdf images from S3 which has following settings
<?xml version="1.0" encoding="UTF-8"?>
<CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
<CORSRule>
<AllowedOrigin>*</AllowedOrigin>
<AllowedMethod>GET</AllowedMethod>
<AllowedHeader>*</AllowedHeader>
</CORSRule>
</CORSConfiguration>
Following the approach similar to - (How do I download a file with Angular2), everything works fine in Firefox. In chrome, PDF download works perfectly but when I try to download an image, always getting typical CORS Error for the GET request.
downloadFile(url, type, fileName) {
this.http.getFile(url, { responseType: 'blob' }).
subscribe(data => this.downloadFileComplete(data, type, fileName));
}
downloadFileComplete(data: any, type: string, fileName: string) {
let fileType = (type === 'IMAGE') ? 'image/jpeg' : 'application/pdf';
var blob = new Blob([data], { type: fileType.toString() });
if (type === 'IMAGE') {
saveAs(blob, fileName + ".jpg");
}
else {
saveAs(blob, fileName + ".pdf");
}
}
Access to XMLHttpRequest at 'https://xxxxxxxxxxxxxx.s3.ap-south-1.amazonaws.com/certifications/3c35754f-b3c4-42f2-953a-8af52b5ed19bf2bc5370-9481-492a-8174-dfcc63d5a9bd.jpg' from origin 'http://localhost:4200' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
Upon some research, I found this work-around (https://zyst.io/how-to-fix-aws-s3-chrome-and-safari-cors-on-images) where they suggest to use HTTP URL for S3. When I tried this, images download started working again. Can someone explain to me
Why GET call to fetch image throws CORS errors for image and not for PDF in Chrome?
Is there a proper solution for this problem? My site runs on HTTPS, therefore an HTTP based solution is not good.