要清楚,一切都是客户端JavaScript,我希望一切都无服务器,所以我没有自己的API,也没有在我的最终实现自定义访问提供程序。我只想防止在UI中暴露我的访问权限和密钥。
/**
* Handle file upload with Amazon S3 bucket
* @param id - record ID in local DB
* @param doc - file to be uploaded, taken from event.target.files
* @param done - callback to call after upload
*/
public sendFileToAws(id: number, doc: File, done: Function) {
// @Todo : Move to config
let pointer = this;
aws.config.region = pointer.awsRegion;
aws.config.credentials = new aws.CognitoIdentityCredentials({
IdentityPoolId: 'us-east-1:e48af67b-c315-47ca-b816-000000000000',
RoleArn: 'arn:aws:iam::000000000000:role/GognitoSuperUserRole',
AccountId: '000000000000'
});
//aws.config.update({
// region: pointer.awsRegion,
// accessKeyId: pointer.awsAccessKey,
// secretAccessKey: pointer.awsSecretKey
//});
let server = new aws.S3({ params: { Bucket: pointer.awsStorageName } });
let directory = pointer.getDocumentDirectory() + '/' + id + '-' + doc.name;
let params = {
Key: directory,
ContentType: doc.type,
Body: doc,
Bucket: pointer.awsStorageName,
ACL: pointer.awsPermission
};
server.upload(params, (e, data) => {
done(e, data);
});
}
它返回以下错误:
<ErrorResponse xmlns="https://sts.amazonaws.com/doc/2011-06-15/">
<Error>
<Type>Sender</Type>
<Code>AccessDenied</Code>
<Message>Not authorized to perform sts:AssumeRoleWithWebIdentity</Message>
</Error>
<RequestId>28b768a5-8f30-11e7-a7bf-4b5038235cb8</RequestId>
</ErrorResponse>
答案 0 :(得分:3)
我还在开发一个前端的Typescript应用程序,该应用程序使用Cognito中的Authenticated和Unauthenticated身份。
对于未经身份验证的身份,我的流程如下:
CognitoIdentity.getId()
在身份池中创建新身份。该代码如下所示:
var cognitoidentity = new AWS.CognitoIdentity();
var params = {
IdentityPoolId: 'us-east-1:bxxxxxx-cxxx-4xxx-8xxx-9xxxxxxxxxxx'
};
// tslint:disable-next-line:no-any
cognitoidentity.getId(params, function(err: any, data: any) {
if (err) {
console.log(err, err.stack); // an error occurred
} else {
AWS.config.credentials = new AWS.CognitoIdentityCredentials({
IdentityPoolId: 'us-east-1:bxxxxxxx-cxxx-4xxx-8xxx-9xxxxxxxxxxx',
IdentityId: data.IdentityId
});
// access AWS resources
}
});
这将导致您的应用获得用于访问后端资源的临时IAM凭据(访问密钥,密钥,会话令牌)。
使用这些密钥时假定的角色将是您在身份池设置中配置的角色:
这样您也不必将IAM角色名称公开给浏览器。 AWS将根据IAM密钥假定正确的角色。
快乐黑客!