Search code examples
angularamazon-web-servicesamazon-s3clientpre-signed-url

S3 file upload with presigned url returns status (canceled)


I am trying to upload a file to Amazon S3 using the angular client. I have generated Presigned URL using the NodeJs application server. While uploading a file to presigned URL, but it fails to upload a file from the client, gets (canceled).

I tried to upload file in formats: buffer, base64, formData and raw File

This works if I try uploading with the postman to the generated URL on binary form

Generating Presigned URL using NodeJs

      const s3 = new AWS.S3({
         accessKeyId: AWS_ACCESS_KEY,
         secretAccessKey: AWS_SECRET_ACCESS_KEY,
         region: 'eu-central-1',
      });
      const signedUrlExpireSeconds = 60 * 5;
      const presignedS3Url = s3.getSignedUrl('putObject', {
         Bucket: process.env.bucket,
         Key: './test3Public.pdf',
         Expires: signedUrlExpireSeconds,
         ACL: 'public-read',
         ContentType: 'application/pdf',
      });

HTML

<input
      type="file"
      (change)="onFileSelected($event)"
      accept="application/pdf, .docx"
   />

Component.ts

onFileSelected(event: any) {
      this.fileToUpload = <File>event.target.files[0];
     
      this.fileUpload
         .generatePresignedURL(this.fileToUpload.name)
         .pipe(first())
         .subscribe(
            (data) => {
               this.fileUpload
                  .uploadfileAWSS3(this.fileToUpload, data.presignedS3Url)
                  .pipe(first())
                  .subscribe(
                     (data) => {
                        console.log('uploaded', data);
                     },
                     (error) => {
                        console.log('error', error);
                     }
                  );
            },
            (error) => {
               console.log('error', error);
            }
         );
   }

format that im sending a file:

enter image description here

Angular 11 Service

 uploadfileAWSS3(file, fileuploadurl) {
      const req = new HttpRequest('PUT', fileuploadurl, file);
      return this.http.request(req);
   }

enter image description here

Could you help me where is the problem that the upload gets canceled?


Solution

  • HTML:

        <input type="file" (change)="onFileSelected($event)" accept="application/pdf, .docx"
        />
    

    Angular Code:

      onFileSelected(event) {
        const fileToUpload = <File>event.target.files[0];
        const bucketParms = {
          Bucket: "sample-temp-bucket",
          Key: fileToUpload.name,
          ContentType: "application/pdf",
          ACL: "public-read",
          Expires: 3600,
        };
        s3.getSignedUrl("putObject", bucketParms, (error, url) => {
          if (error) console.log("error", error);
          if (url) {
            this.http.put(url, fileToUpload).subscribe((response) => {
              console.log("response receved is ", response);
            });
          }
        });
      }
    

    CORS:

    [
        {
            "AllowedHeaders": [
                "*"
            ],
            "AllowedMethods": [
                "PUT",
                "POST"
            ],
            "AllowedOrigins": [
                "http://localhost:4200"
            ],
            "ExposeHeaders": []
        }
    ]