How to upload file in AWS S3 from Angular 8 How to upload file in AWS S3 from Angular 8 angular angular

How to upload file in AWS S3 from Angular 8


I finally come with solution after spending couple of hours on it.solutions steps are as below for Angular 8 Project.

  1. Install dependancy

    npm install --save-dev @types/node

  2. Need to add "types": ["node"] to the tsconfig.app.json

  3. Add below lines in polyfills.js

    if (typeof (window as any).global === 'undefined') { (window as any).global = window; }

Reference : Last answer by @AWS PS (Step 1)
Reference : https://github.com/aws/aws-sdk-js/issues/1271 (Step 2)
Reference : https://github.com/bevacqua/dragula/issues/602 (Step 3)


Finally I have solved the issue by below Steps:

Step 1 :

npm install --save-dev @types/node

Step 2 :

Use Reference : https://github.com/aws/aws-sdk-js/issues/1271 (Step 2)

Step 3 :

Use Reference : https://github.com/bevacqua/dragula/issues/602 (Step 3)

public uploadFileToAws(file, folderName, newFileName) {    var aws_cognito_identity_pool_id = environment.pool_id;    var aws_cognito_region = environment.aws_cognito_region;    var aws_project_region = environment.aws_project_region;    AWS.config.region = aws_project_region;    AWS.config.credentials = new AWS.CognitoIdentityCredentials({      IdentityPoolId: aws_cognito_identity_pool_id    }, {        region: aws_cognito_region      });    AWS.config.update({ customUserAgent: 'MobileHub v0.1' });    const s3 = new S3({      apiVersion: '2006-03-01',      params: { Bucket: environment.bucket }    });    s3.upload({        Key: folderName+'/'+newFileName,        Bucket: environment.bucket,        Body: file,        ACL: 'private'      },function (err, data) {        this.fileuploading = false;        if (err) {          console.log(err, 'there was an error uploading your file');        } else {          console.log(data.Key+ ' uploaded successfully');                  }                return true;      });  }


TypeScript is complaining because some node environment types are needed. This is a limitation for now that we might be able to get around by stubbing those interfaces in the future.

can you try installing the environment typings to see if it gets around your issue?

npm install --save-dev @types/node