尝试执行putObject时出现“配置中缺少凭据,如果使用AWS_CONFIG_FILE,则设置AWS_SDK_LOAD_CONFIG = 1”错误

时间:2020-05-19 07:18:27

标签: javascript amazon-web-services amazon-s3 file-upload browser

我对使用AWS服务非常陌生,对JS整体而言还相对较新。我正在尝试使用putObject将用户数据从我的网站发送到s3存储桶。我希望任何进入此站点的用户将其数据发送到存储桶。在我网站的HTML中,我具有以下JavaScript代码,该代码创建了一个AWS.S3对象,如下所示:

                AWS.config.update({
                    region: 'us-east-1', 
                    accessKeyID: "MYACCESSKEYID",
                    secretAccessKey: "MYSECRETACCESSKEY"

                });
                var s3 = new AWS.S3();

                var params = {
                    Bucket: "my.test.bucket", 
                    Key: <JSON object containing user info>
                };

                s3.putObject(params, function(err, data) {
                    if (err) console.log(err, err.stack); // an error occurred
                    else     console.log(data);           // successful response
                    /*
                    data = {
                        ETag: "\"6805f2cfc46c0f04559748bb039d69ae\"", 
                        VersionId: "tpf3zF08nBplQK1XLOefGskR7mGDwcDk"
                    }
                    */
                });

访问密钥ID和相应的秘密访问密钥与IAM用户相关联,该IAM用户具有执行putObject以便将对象发送到存储桶的必要权限。我知道这不是一种安全的处理方式,但目前尚无此问题。当我尝试在浏览器中打开HTML文件时,标题中的错误消息出现在浏览器控制台中。这是浏览器控制台中显示的完整错误消息:

CredentialsError: Missing credentials in config, if using AWS_CONFIG_FILE, set AWS_SDK_LOAD_CONFIG=1
    at r (https://sdk.amazonaws.com/js/aws-sdk-2.674.0.min.js:70:20163)
    at constructor.getCredentials (https://sdk.amazonaws.com/js/aws-sdk-2.674.0.min.js:70:20691)
    at constructor.<anonymous> (https://sdk.amazonaws.com/js/aws-sdk-2.674.0.min.js:71:21119)
    at constructor.callListeners (https://sdk.amazonaws.com/js/aws-sdk-2.674.0.min.js:73:23679)
    at constructor.emit (https://sdk.amazonaws.com/js/aws-sdk-2.674.0.min.js:73:23419)
    at constructor.emitEvent (https://sdk.amazonaws.com/js/aws-sdk-2.674.0.min.js:73:9203)
    at constructor.e (https://sdk.amazonaws.com/js/aws-sdk-2.674.0.min.js:73:4845)
    at i.runTo (https://sdk.amazonaws.com/js/aws-sdk-2.674.0.min.js:75:12863)
    at constructor.runTo (https://sdk.amazonaws.com/js/aws-sdk-2.674.0.min.js:73:6501)
    at constructor.send (https://sdk.amazonaws.com/js/aws-sdk-2.674.0.min.js:73:6394) "CredentialsError: Missing credentials in config, if using AWS_CONFIG_FILE, set AWS_SDK_LOAD_CONFIG=1
    at r (https://sdk.amazonaws.com/js/aws-sdk-2.674.0.min.js:70:20163)
    at constructor.getCredentials (https://sdk.amazonaws.com/js/aws-sdk-2.674.0.min.js:70:20691)
    at constructor.<anonymous> (https://sdk.amazonaws.com/js/aws-sdk-2.674.0.min.js:71:21119)
    at constructor.callListeners (https://sdk.amazonaws.com/js/aws-sdk-2.674.0.min.js:73:23679)
    at constructor.emit (https://sdk.amazonaws.com/js/aws-sdk-2.674.0.min.js:73:23419)
    at constructor.emitEvent (https://sdk.amazonaws.com/js/aws-sdk-2.674.0.min.js:73:9203)
    at constructor.e (https://sdk.amazonaws.com/js/aws-sdk-2.674.0.min.js:73:4845)
    at i.runTo (https://sdk.amazonaws.com/js/aws-sdk-2.674.0.min.js:75:12863)
    at constructor.runTo (https://sdk.amazonaws.com/js/aws-sdk-2.674.0.min.js:73:6501)
    at constructor.send (https://sdk.amazonaws.com/js/aws-sdk-2.674.0.min.js:73:6394)"

请告知我是否还有其他信息。

edit:我已经尝试使用AWS CLI来配置〜/ .aws / config文件,使用accesskeyID,秘密访问密钥以及我在上面的代码中使用的区域

0 个答案:

没有答案