AWS CredentialsError S3:无法从CognitoIdentityCredentials加载凭据
我正在使用记录的AWS Cognito。进行日志记录后,我尝试将图像上传到s3存储桶。我不想在代码中传递我的accessKey和accessId。我在做什么-
我正在使用Angular 8 +
import * as AWS from 'aws-sdk';
AWS.config.update({
region: Config.settings.userPool.region,
credentials: new AWS.CognitoIdentityCredentials({
IdentityPoolId: Config.settings.userPool.identityPoolId,
}),
});
const params = {
Bucket: Config.settings.buckets3.imageS3,
Key: this.FOLDER + file.name,
Body: file,
ContentEncoding: Config.settings.buckets3.contentEncoding,
ContentDisposition: Config.settings.buckets3.contentDisposition,
ContentType: Config.settings.buckets3.contentType,
};
const s3Bucket = new AWS.S3({});
return new Promise((resolve, reject) => {
s3Bucket.upload(params, function (err, data) {
if (err) {
const print = console.log;
print('There was an error uploading your file: ', err);
reject(err);
} else {
const print = console.log;
print('Successfully uploaded file.', data);
data.Location = data.Location.replace('s3.ap-south-1.amazonaws.com/', '');
resolve(data);
}
});
测试目的-用于检查凭据是否正确
AWS.config.getCredentials(function (err) {
if (!err) {
console.log(AWS.config.credentials.accessKeyId);
} else {
console.log(err);
}
});