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

jja*_*gga 5 amazon-s3 amazon-web-services amazon-cognito

在遵循此处的简单官方教程(从浏览器将照片上传到 Amazon S3(对未经身份验证的用户使用 Cognito))时,我收到错误“配置中缺少凭据”。如下所示,代码非常简单,但不知道为什么会发生错误,即使我认为我完全遵循了教程的先决条件。我很好奇何时出现错误消息“配置中缺少凭据”,以便我猜测原因。这是因为 AWS 发生了变化还是我做错了什么?\n如果您让我知道正确的答案或任何提示,我将非常感激。

\n\n
<!DOCTYPE html>\n<html lang="en">\n\n<head>\n    <meta charset="UTF-8">\n    <meta name="viewport" content="width=device-width, initial-scale=1.0">\n    <meta http-equiv="X-UA-Compatible" content="ie=edge">\n    <title>Document</title>\n    <script src="https://sdk.amazonaws.com/js/aws-sdk-2.255.1.min.js"></script>\n    <script>\n        var bucketName = \'mama-swedenhouse-resources\';\n        var bucketRegion = \'ap-northeast-2\';\n        var identityPoolId = \'ap-northeast-2:f4250b2e-5d77-4413-bc3a-34c4xxxxxxxx\'; //just hiding last 8 letters on this github issue \n\n        AWS.config.update({\n            region: bucketRegion,\n            credentials: new AWS.CognitoIdentityCredentials({\n                identityPoolId: identityPoolId\n            })\n        });\n\n        var s3 = new AWS.S3({\n            apiVersion: \'2006-03-01\',\n            params: {\n                Bucket: bucketName\n            }\n        });\n\n        function uploadPhoto() {\n\n            var files = document.getElementById(\'test-file-input\').files;\n            if (!files.length) {\n                return alert(\'Please choose a file to upload first.\');\n            }\n            var file = files[0];\n            var fileName = file.name;\n\n            s3.upload({\n                Key: fileName + \' \' + Math.floor(Date.now()),\n                Body: file,\n                ACL: \'public-read\'\n            }, function (err, data) {\n                if (err) {\n                    console.log(err);\n                    return console.log(\'There was an error uploading your photo: \', err.message);\n                }\n                console.log(\'Successfully uploaded photo.\');\n            });\n        };\n    </script>\n</head>\n\n<body>\n<section>\n    <input type="file" name="test-file-input" id="test-file-input" accept="image/*">\n    <button id="uplaod-btn" onclick="uploadPhoto();">\xec\x97\x85\xeb\xa1\x9c\xeb\x93\x9c</button>\n</section>\n</body>\n\n</html>\n
Run Code Online (Sandbox Code Playgroud)\n\n

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

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

小智 2

让它正常工作,设置凭据如下:

AWS.config.region = 'REGION';
AWS.config.credentials = new AWS.CognitoIdentityCredentials({
    IdentityPoolId: 'IDENTITY_POOL_ID',
});
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助!