1

I would like to load the image from the AWS S3 bucket using the pre-signed URL for image cropping purpose. I'm using the ngx-image-copper and here's my code:

app-cropper.component.ts:

getBase64FromImageUrl(url: string) {
this.httpClient
  .get(url, { responseType: 'blob' } })
  .subscribe((blob) => {
    const reader = new FileReader();
    reader.readAsDataURL(blob);
    reader.addEventListener('load', () => {
      this.imageBase64 = reader.result;
    });
  })}

app-cropper.component.html:

<image-cropper
  class="ly-cropper"
  id="cropper"
  *ngIf="imageBase64; else spinner"
  [imageFile]="imageBase64"
  [maintainAspectRatio]="isMaintainAspectRatio$ | async"
  [aspectRatio]="ratio"
  [autoCrop]="true"
  format="{{ asset?.fileType }}"
  (imageCropped)="onImageCropped($event)"
  (imageLoaded)="onImageLoaded()"
  (cropperReady)="onCropperReady()"
  (loadImageFailed)="onImageLoadFailed()"
  [cropper]="cropper"
  [canvasRotation]="canvasRotation"
></image-cropper>

<ng-template #spinner>
  <ly-spinner mode="indeterminate" color="gray"> </ly-spinner>
</ng-template>
<div *ngIf="isLoading$ | async" class="loading-overlay">
  <ly-spinner mode="indeterminate" color="gray" [diameter]="200" [showLabel]="false"></ly-spinner>
</div>

With most browsers, the image loading works perfectly fine. However, only in the Chrome browser, I keep getting the error saying that

Access to XMLHttpRequest at 'xxx' from origin 'http://localhost:4200' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested 

I checked most of the posts which are related to the CORS issue with the S3 bucket, and the S3 configuration has been configured as follows. But the CORS issue specifically happening in Chrome still couldn't be fixed. Does anybody know how can I fix this?

[
    {
        "AllowedHeaders": [
            "*"
        ],
        "AllowedMethods": [
            "GET"
        ],
        "AllowedOrigins": [
            "*"
        ],
        "ExposeHeaders": []
    }
]
3
  • Do you see OPTIONS request in the network tab in the browser? Commented Jan 19, 2023 at 14:23
  • Nope. There's no OPTIONS request. Commented Jan 19, 2023 at 14:28
  • OPTION requests are for post requests, but here we have a get request, so that should not be an issue... maybe you need to add the withCredentials = true option like described here stackoverflow.com/questions/38615205/… Commented Jan 19, 2023 at 15:10

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.