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.
Install dependancy
npm install --save-dev @types/node
Need to add "types": ["node"] to the tsconfig.app.json
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;
});
}