Referenz: http://docs.aws.amazon.com/cognito/latest/developerguide/using-amazon-cognito-user-identity-pools-javascript-examples.html
Sie werden diese drei Pakete müssen:
<script src="js/aws-cognito-sdk.min.js"></script>
<script src="js/amazon-cognito-identity.min.js"></script>
<script src="js/aws-sdk.min.js"></script>
Sobald Sie Cognito-Login verwenden, können Sie Lambda-Funktion wie folgt aufrufen:
function invokeMyLambda()
{
if(!objCognitoUser) syncAwsFromCognito();
var lambda = new AWS.Lambda({region: 'us-east-1', apiVersion: '2015-03-31'});
// create JSON object for service call parameters
var pullParams = {
FunctionName : 'myLambFunctionName',
InvocationType : 'RequestResponse', // Event | RequestResponse | DryRun
LogType : 'None',
Payload : JSON.stringify({ "yourKeyName": "Key Value to pass to the function in Event Object"}),
};
// invoke Lambda function, passing JSON object
lambda.invoke(pullParams, function(err, data) {
if (err) {
console.log(err);
} else {
console.log(data);
alert("Success: " + JSON.stringify(data));
}
});
lambda = null;
}
function syncAwsFromCognito() {
// objCognitoUser = new AWSCognito.CognitoIdentityServiceProvider.CognitoUser(userData);
if(!objCognitoUser) {
objCognitoUser = objUserPool.getCurrentUser();
}
if (objCognitoUser) {
objCognitoUser.getSession(function(err, result) {
if (result) {
if(AWS.config.credentials == null) // Refresh AWS Config credentials
AWS.config.credentials = new AWS.CognitoIdentityCredentials(jsonUserCreds);
AWS.config.credentials.params.Logins[strConfUserPoolID] = result.idToken.jwtToken;
}
});
//call refresh method in order to authenticate user and get new temp credentials
AWS.config.credentials.refresh(function (error) {
if (error) {
console.log('syncAwsFromCognito', error);
}
});
}
else
alert("Session expired. Login again");
}
Sie S3 machen Rufen Sie auch direkt nach der Cognito-Authentifizierung in Javascript auf. Ich bevorzuge die Verwendung der REST-API mit API-Gateway anstelle des direkten Aufrufs der Lambda-Funktion über den Browser. Dies liegt daran, dass der Aufruf der Funktion Lambda von der TokenID abhängt, die auch dann für eine Stunde gültig ist, wenn Sie sich mit dem Cognito SDK abmelden.