使用Amazon Cognito Login上传到Amazon S3 [英] Upload to Amazon S3 with Amazon Cognito Login

查看:164
本文介绍了使用Amazon Cognito Login上传到Amazon S3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码向Amazon Cognito注册用户。我想在用户注册时将文件上传到Amazon S3 Bucket。

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());

});


推荐答案

您需要在Cognito中创建一个标识池联合身份。让您的用户池成为该特定身份池的身份提供者,以获取经过身份验证的身份。

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.

使用上面的代码注册用户后,您需要使用以下代码确认并登录并获取AWS凭据(使用您自己的信息替换占位符) ):

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!');
    }
    });

在该代码块的末尾,您将获得可以与main一起使用的AWS凭据适用于javascript(s3客户端)的AWS SDK,用于将文件上传到S3。

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.

这篇关于使用Amazon Cognito Login上传到Amazon S3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆