Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in AWS by (5.6k points)

I'm getting an error when I try to upload files to S3 from my Angular8, but I am not able to use the S3 library in the service file.

import * as AWS from 'aws-sdk/global';

import * as S3 from 'aws-sdk/clients/s3';

I'm getting errors with these lines not sure where I'm missing.

Can anyone help!

1 Answer

0 votes
by (12.4k points)
edited by

To resolve your issue you can try the below steps:

First, install dependency:

npm install --save-dev @types/node

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

Add these lines in polyfills.js:

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

You can use the below reference:

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;

      });

  }

Interested in learning AWS? Check out: AWS Certification

If you wish to know Angular in-depth and get certified in it? Then have a look at the Angular Course from Intellipaat!

Related questions

0 votes
0 answers
Want to get 50% Hike on your Salary?

Learn how we helped 50,000+ professionals like you !

0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...