对于aws回调函数的工作方式,我还不清楚,但在实现该函数时,我遇到了一个问题,即退出其父函数后会上传文件:
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;
});
}
答案 0 :(得分:0)
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);
});
}
}