使用 aws-amplify for 节点的存储 (S3) 时出现“无凭据”错误

J. *_*ake 6 javascript amazon-s3 amazon-web-services node.js reactjs

当尝试从 S3 中的存储桶获取图像时,I\xc2\xb4m 收到错误“无凭据”:

\n\n

\r\n
\r\n
import React, { Component } from \'react\'\r\nimport { Auth, Storage } from \'aws-amplify\'\r\n\r\n\r\ncomponentDidMount(){\r\n\r\nStorage.get(\'random-image.jpg\')\r\n                .then(data => {\r\n                    console.log(data)\r\n                }).catch(err => console.log(err))\r\n         \r\n}
Run Code Online (Sandbox Code Playgroud)\r\n
\r\n
\r\n

\n\n

对于我\xc2\xb4听说的我需要添加一个身份联合池,我\xc2\xb4m努力理解aws-amplify和aws-sdk之间的区别,特别是我必须在我的应用程序中放置身份的确切位置联合池 url 才能使其正常工作。

\n\n

Here\xc2\xb4s是我到目前为止的代码,我\xc2\xb4m使用cognito服务来允许用户注册和登录,并使用S3让用户能够上传图像。

\n\n

\r\n
\r\n
Amplify.configure({\r\n    Auth: {\r\n        mandatorySignId: true,\r\n        region: config.cognito.REGION,\r\n        userPoolId: config.cognito.USER_POOL_ID,\r\n        userPoolWebClientId: config.cognito.APP_CLIENT_ID\r\n    },\r\n    Storage: {\r\n        AWSS3: {\r\n            bucket: config.s3.bucket,\r\n            region: config.s3.region, \r\n        }\r\n    }\r\n})
Run Code Online (Sandbox Code Playgroud)\r\n
\r\n
\r\n

\n\n

但是现在我需要实现必要的代码,以便能够对 S3 存储桶执行 GET 请求,并且从 i\xc2\xb4ve 读取的内容中,我需要打开一个与我的认知用户池相关的联合身份池,然后将此代码粘贴到应用程序中:

\n\n

\r\n
\r\n
AWS.config.region = \'eu-west-2\'; // Region\r\nAWS.config.credentials = new AWS.CognitoIdentityCredentials({\r\n    IdentityPoolId: \'eu-west-2:70614759-5f98-48*******\',\r\n});
Run Code Online (Sandbox Code Playgroud)\r\n
\r\n
\r\n

\n\n

然而那不是aws-amplify但是aws-sdk。我\xc2\xb4ve 尝试导入 aws-sdk 并粘贴该代码,但不起作用。\n我可以将该信息放入语句中吗Amplify.configure?在这个页面https://aws-amplify.github.io/docs/js/storage之后,它说我可以,所以我这样做了:

\n\n

\r\n
\r\n
Auth: {\r\n        mandatorySignId: true,\r\n        region: config.cognito.REGION,\r\n        userPoolId: config.cognito.USER_POOL_ID,\r\n        userPoolWebClientId: config.cognito.APP_CLIENT_ID\r\n        // add the identity federated:\r\n        identityPoolId: config.cognito.IDENTITY_POOL\r\n    },
Run Code Online (Sandbox Code Playgroud)\r\n
\r\n
\r\n

\n\n

现在它给了我Missing credentials in config

\n