配置中缺少凭据:Cognito(对于未经身份验证的)和S3

时间:2018-06-15 17:35:36

标签: amazon-web-services amazon-s3 amazon-cognito

在遵循简单的官方教程here(Uploading Photos to Amazon S3 from a Browser(using Cognito for unauthenticated users))时,我收到错误“配置中缺少凭据”。如下所示,代码非常简单,但即使我认为我完全遵循本教程的要求,也不知道为什么会出现错误。我很好奇当出现错误消息“配置中缺少凭据”时,我可能会猜到原因。这是因为AWS已经改变了,还是我做错了什么? 如果您让我知道正确的答案或任何提示,那将是非常感恩的。

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script src="https://sdk.amazonaws.com/js/aws-sdk-2.255.1.min.js"></script>
    <script>
        var bucketName = 'mama-swedenhouse-resources';
        var bucketRegion = 'ap-northeast-2';
        var identityPoolId = 'ap-northeast-2:f4250b2e-5d77-4413-bc3a-34c4xxxxxxxx'; //just hiding last 8 letters on this github issue 

        AWS.config.update({
            region: bucketRegion,
            credentials: new AWS.CognitoIdentityCredentials({
                identityPoolId: identityPoolId
            })
        });

        var s3 = new AWS.S3({
            apiVersion: '2006-03-01',
            params: {
                Bucket: bucketName
            }
        });

        function uploadPhoto() {

            var files = document.getElementById('test-file-input').files;
            if (!files.length) {
                return alert('Please choose a file to upload first.');
            }
            var file = files[0];
            var fileName = file.name;

            s3.upload({
                Key: fileName + ' ' + Math.floor(Date.now()),
                Body: file,
                ACL: 'public-read'
            }, function (err, data) {
                if (err) {
                    console.log(err);
                    return console.log('There was an error uploading your photo: ', err.message);
                }
                console.log('Successfully uploaded photo.');
            });
        };
    </script>
</head>

<body>
<section>
    <input type="file" name="test-file-input" id="test-file-input" accept="image/*">
    <button id="uplaod-btn" onclick="uploadPhoto();">업로드</button>
</section>
</body>

</html>

错误消息(在Chrome控制台上)

Error: Missing credentials in config
    at constructor.fail (aws-sdk-2.255.1.min.js:50)
    at constructor.validateStructure (aws-sdk-2.255.1.min.js:50)
    at constructor.validateMember (aws-sdk-2.255.1.min.js:50)
    at constructor.validate (aws-sdk-2.255.1.min.js:50)
    at constructor.<anonymous> (aws-sdk-2.255.1.min.js:49)
    at constructor.callListeners (aws-sdk-2.255.1.min.js:51)
    at constructor.emit (aws-sdk-2.255.1.min.js:51)
    at constructor.emitEvent (aws-sdk-2.255.1.min.js:50)
    at constructor.e (aws-sdk-2.255.1.min.js:50)
    at a.runTo (aws-sdk-2.255.1.min.js:52)
aws-s3-upload.html:48 There was an error uploading your photo:  Missing credentials in config

1 个答案:

答案 0 :(得分:1)

可以像这样设置凭据:

AWS.config.region = 'REGION';
AWS.config.credentials = new AWS.CognitoIdentityCredentials({
    IdentityPoolId: 'IDENTITY_POOL_ID',
});

希望这会有所帮助!