Upload to Amazon S3 with Amazon Cognito Login

2019-06-10 05:53发布

问题:

I am using the code below to sign up a user with Amazon Cognito. I would then like to upload a file to an Amazon S3 Bucket when the user signs up.

What do I need to do to configure the bucket ready to upload, once the user has signed up? Thank you

    var roleArn = 'arn:aws:iam::123456:role/Cognito_Auth_Role';
    var bucketName = 'MY_BUCKET';
    AWS.config.region = 'eu-west-1';
        var poolData = {
            UserPoolId : 'POOL_ID', // your user pool id here
            ClientId : 'CLIENT_ID' // your app client id here
        };
        var userPool = new AWSCognito.CognitoIdentityServiceProvider.CognitoUserPool(poolData);
        var userData = {
            Username : 'username', // your username here
            Pool : userPool
        };
        var attributeList = [];
        var password
        //Create Bucket
        var bucket = new AWS.S3({
        params: {
            Bucket: bucketName
        }
    });

var dataEmail = {
    Name : 'email',
    Value : 'email@me.com' // your email here
};
var dataPhoneNumber = {
    Name : 'phone_number',
    Value : '+1234567890' // your phone number here with +country code and no delimiters in front
};

...

    var attributeEmail = new AWSCognito.CognitoIdentityServiceProvider.CognitoUserAttribute(dataEmail);
var attributePhoneNumber = new AWSCognito.CognitoIdentityServiceProvider.CognitoUserAttribute(dataPhoneNumber);
attributeList.push(attributeEmail);
attributeList.push(attributePhoneNumber); 
var cognitoUser;
userPool.signUp('username', 'password', attributeList, null, function(err, result){
    if (err) {
        alert(err);
        return;
    }
    cognitoUser = result.user;
    console.log('user name is ' + cognitoUser.getUsername());

});

回答1:

You would need to create an identity pool in the Cognito Federated Identities. Have your user pool be an identity provider for that particular identity pool for authenticated identities.

After signing up the user with your code above, you would need to confirm him and sign in and obtain AWS credentials using the code below (replace the placeholders with your own info):

var cognitoUser = userPool.getCurrentUser();

if (cognitoUser != null) {
    cognitoUser.getSession(function(err, result) {
        if (result) {
            console.log('You are now logged in.');

            // Add the User's Id Token to the Cognito credentials login map.
            AWS.config.credentials = new AWS.CognitoIdentityCredentials({
                IdentityPoolId: 'YOUR_IDENTITY_POOL_ID',
                Logins: {
                    'cognito-idp.<region>.amazonaws.com/<YOUR_USER_POOL_ID>': result.getIdToken().getJwtToken()
                }
            });
        }
    });
}
//call refresh method in order to authenticate user and get new temp credentials
AWS.config.credentials.refresh((error) => {
    if (error) {
        console.error(error);
    } else {
        console.log('Successfully logged!');
    }
    });

At the end of that block of code you would have obtained AWS credentials that you can use with the main AWS SDK for javascript (the s3 client) to upload files to S3.