Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/amazon-web-services/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用AmazonCognito登录上传到AmazonS3_Javascript_Amazon Web Services_Amazon S3_Amazon Cognito - Fatal编程技术网

Javascript 使用AmazonCognito登录上传到AmazonS3

Javascript 使用AmazonCognito登录上传到AmazonS3,javascript,amazon-web-services,amazon-s3,amazon-cognito,Javascript,Amazon Web Services,Amazon S3,Amazon Cognito,我正在使用下面的代码向Amazon Cognito注册一个用户。然后,当用户注册时,我想将一个文件上传到AmazonS3存储桶中 一旦用户注册,我需要做什么来配置bucket以准备上传? 多谢各位 var roleArn = 'arn:aws:iam::123456:role/Cognito_Auth_Role'; var bucketName = 'MY_BUCKET'; AWS.config.region = 'eu-west-1'; var pool

我正在使用下面的代码向Amazon Cognito注册一个用户。然后,当用户注册时,我想将一个文件上传到AmazonS3存储桶中

一旦用户注册,我需要做什么来配置bucket以准备上传? 多谢各位

    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
};


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

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

var cognitoUser=userPool.getCurrentUser();
if(cognitoUser!=null){
getSession(函数(err,result){
如果(结果){
log('您现在已登录');
//将用户的Id令牌添加到Cognito凭据登录映射中。
AWS.config.credentials=新的AWS.CognitoIdentityCredentials({
IdentityPoolId:'您的_IDENTITY _POOL _ID',
登录:{
'cognito idp..amazonaws.com/':result.getIdToken().getJwtToken()
}
});
}
});
}
//调用刷新方法以验证用户身份并获取新的临时凭据
AWS.config.credentials.refresh((错误)=>{
如果(错误){
控制台错误(error);
}否则{
console.log('Successfully logged!');
}
});

在这段代码的末尾,您将获得AWS凭据,可以与主AWS SDK for javascript(s3客户端)一起使用,将文件上载到s3

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

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

var cognitoUser=userPool.getCurrentUser();
if(cognitoUser!=null){
getSession(函数(err,result){
如果(结果){
log('您现在已登录');
//将用户的Id令牌添加到Cognito凭据登录映射中。
AWS.config.credentials=新的AWS.CognitoIdentityCredentials({
IdentityPoolId:'您的_IDENTITY _POOL _ID',
登录:{
'cognito idp..amazonaws.com/':result.getIdToken().getJwtToken()
}
});
}
});
}
//调用刷新方法以验证用户身份并获取新的临时凭据
AWS.config.credentials.refresh((错误)=>{
如果(错误){
控制台错误(error);
}否则{
console.log('Successfully logged!');
}
});

在这段代码的末尾,您将获得AWS凭据,可以与主AWS SDK for javascript(s3客户端)一起使用,将文件上载到s3

你好。我从Chrome控制台收到此错误:未捕获错误:无法读取Null的属性“刷新”。感谢一百万,拯救了我的一天!尝试使用cognito将图像上传到S3时非常沮丧,然后遇到了您的代码…谢谢。我说得太快了!原来是使用未经身份验证的身份登录的,一旦我禁用该功能,就会出现NotAuthorizedException异常!终于成功了!我想我有一个复制/粘贴错误,但它现在工作!类型“Credentials | CredentialsOptions”上不存在属性“refresh”。您好。我从Chrome控制台收到此错误:未捕获错误:无法读取Null的属性“刷新”。感谢一百万,拯救了我的一天!尝试使用cognito将图像上传到S3时非常沮丧,然后遇到了您的代码…谢谢。我说得太快了!原来是使用未经身份验证的身份登录的,一旦我禁用该功能,就会出现NotAuthorizedException异常!终于成功了!我想我有一个复制/粘贴错误,但它现在工作!类型“Credentials | CredentialsOptions”上不存在属性“refresh”。
    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());

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