Javascript Aws文件上载回调函数在退出父函数后是否运行?
我不清楚aws回调函数是如何工作的,在实现它时,我面临一个问题,即它在退出其父函数后上载文件:Javascript Aws文件上载回调函数在退出父函数后是否运行?,javascript,amazon-web-services,amazon-s3,Javascript,Amazon Web Services,Amazon S3,我不清楚aws回调函数是如何工作的,在实现它时,我面临一个问题,即它在退出其父函数后上载文件: uploadfile(file) { const params = { Bucket: 'test', Key: 'test/' + this.id, Body: file, ContentDisposition: 'attachment;filename="' + file.name + '"', Content
uploadfile(file) {
const params = {
Bucket: 'test',
Key: 'test/' + this.id,
Body: file,
ContentDisposition: 'attachment;filename="' + file.name + '"',
ContentType: file.type
};
const bucket = new S3({
accessKeyId: '****',
secretAccessKey: '******'
});
bucket.upload(params, function(err, data) {
if (err) {
console.log('There was an error uploading your file: ', err);
return false;
}
console.log('Successfully uploaded file.', data);
localStorage.setItem('fileUpload', 'true');
return true;
});
}
I have also uploaded images through aws cognito. Refer below code this will help you :
import * as aws from 'aws-sdk';
/**
* function to initialize AWS
**/
initializeAWS() {
this.bucket = new aws.S3({params: {Bucket: 'bucketName'} });
}
/**
* function to upload image through cognito
**/
public uploadFileAWSCognito(file: any, referenceId : any, callBack: any){
//get cognito credentials
this.userService.getCognitoId().subscribe(
res => {
this.cognitoCredentials = res.data;
this.amazonCognitoId = this.cognitoCredentials.IdentityId;
this.amazonCognitoToken = this.cognitoCredentials.Token;
//upload image to cognito
return new Promise((resolve, reject) => {
aws.config.update({
region: 'us-east-1',
credentials: new aws.CognitoIdentityCredentials({
IdentityPoolId: 'pool_Id',
IdentityId : this.amazonCognitoId,
Logins: {
'cognito-identity.amazonaws.com': this.amazonCognitoToken
}
})
});
let keyString;
if(referenceId=='Panel') {
keyString = 'panel/'+referenceId +"_"+ new Date().getTime() +'.jpeg'
} else {
keyString = 'profile/user/'+referenceId +'.jpeg?' + new Date().getTime()
}
this.s3 = new AWS.S3({ apiVersion: '2006-03-01', params: { Bucket: 'bucketName ' } });
this.s3.upload({
Key: keyString,
Body: file,
StorageClass: 'STANDARD',
ACL: 'public-read'
}, (err: any, data: any) => {
if (err) {
console.log('err', err);
//return;
reject({ errorMSG: 'errorm message' });
}
//return uploaded image data
callBack(data);
});
});
},
error => {
this.commonService.hideLoading();
console.log(error);
});
}
}