开发者问题收集

@aws-sdk/client-s3 S3Client 返回错误 TypeError:无法读取未定义的属性(读取“发送”)

2022-09-16
2726

按照 Gatsby 应用中的 AWS-SDK 示例/文档,我设置了以下工作流程,以便直接从客户端将文件上传到我的 S3 存储桶,但是, const data = await client.send(...) 返回 Error TypeError: Cannot read properties of undefined (reading 'send') ,即使 client 确实存在。

AWS 是一个新的领域,如果我的错误非常明显,请原谅我。

完整错误:

Error TypeError: Cannot read properties of undefined (reading 'send')
at eval (fromCognitoIdentityPool.js:26:1)
at step (tslib.es6.js:102:1)
at Object.eval [as next] (tslib.es6.js:83:1)
at fulfilled (tslib.es6.js:73:1)
import { PutObjectCommand, S3Client } from "@aws-sdk/client-s3";
import { fromCognitoIdentityPool } from "@aws-sdk/credential-provider-cognito-identity";

const client = new S3Client({
  region: process.env.GATSBY_AWS_S3_REGION,
  credentials: fromCognitoIdentityPool({
    clientConfig: { region: process.env.GATSBY_AWS_S3_REGION },
    identityPoolId: process.env.GATSBY_AWS_USER_POOL_ID,
  })
})

// Upload file to specified bucket.
export const run = async ({ email, photo }) => {
  const uploadParams = {
    Bucket: process.env.GATSBY_AWS_BUCKET_NAME,
    Key: email + photo.name,
    Body: photo,
  };

  try {
    const data = await client.send(new PutObjectCommand(uploadParams));
    console.log("Success", data);
    return data; 
  } catch (err) {
    console.log("Error", err);
  }
};
1个回答

错误在于导入:

import { fromCognitoIdentityPool } from "@aws-sdk/credential-provider-cognito-identity";

而应该是:

import { fromCognitoIdentityPool } from "@aws-sdk/credential-providers";
Kirill
2022-09-19