Я исправил эту проблему, обновив функцию fileEvent (), как показано ниже, и она прекрасно работала.Вклад Гектора помог мне копаться внутри.Спасибо, Гектор.
fileEvent(fileInput: any) {
const region = <aws region>;
const file = fileInput.target.files[0];
const bucketName = <bucketName>;
const accessKey = <accessKey>;
const secretKey = <secretKey>;
console.log(file);
AWS.config.credentials = new AWS.CognitoIdentityCredentials({
// either IdentityPoolId or IdentityId is required
// See the IdentityPoolId param for AWS.CognitoIdentity.getID (linked below)
// See the IdentityId param for AWS.CognitoIdentity.getCredentialsForIdentity
// or AWS.CognitoIdentity.getOpenIdToken (linked below)
IdentityPoolId: <IdentityPoolId>,
// IdentityId: <IdentityId>,
// optional, only necessary when the identity pool is not configured
// to use IAM roles in the Amazon Cognito Console
// See the RoleArn param for AWS.STS.assumeRoleWithWebIdentity (linked below)
// RoleArn: 'arn:aws:iam::1234567890:role/MYAPP-CognitoIdentity',
// optional tokens, used for authenticated login
// See the Logins param for AWS.CognitoIdentity.getID (linked below)
Logins: {
'graph.facebook.com': 'FBTOKEN',
'www.amazon.com': 'AMAZONTOKEN',
'accounts.google.com': 'GOOGLETOKEN',
'api.twitter.com': 'TWITTERTOKEN',
'www.digits.com': 'DIGITSTOKEN'
},
// optional name, defaults to web-identity
// See the RoleSessionName param for AWS.STS.assumeRoleWithWebIdentity (linked below)
RoleSessionName: 'web',
// optional, only necessary when application runs in a browser
// and multiple users are signed in at once, used for caching
// LoginId: 'example@gmail.com'
}, {
// optionally provide configuration to apply to the underlying service clients
// if configuration is not provided, then configuration will be pulled from AWS.config
// region should match the region your identity pool is located in
region: region,
// specify timeout options
httpOptions: {
timeout: 100
}
});
const bucket = new AWS.S3({
region: region,
credentials: new AWS.Credentials(accessKey, secretKey)
});
const params = {
Bucket: bucketName,
Key: file.name,
ContentType: file.type,
Body: file,
ServerSideEncryption: 'AES256'
};
bucket.putObject(params, function(err, data) {
if (err) {
console.log(err.message);
return false;
} else {
// Upload Successfully Finished
console.log('File Uploaded Successfully');
}
});
}