NodeJS AWS createPresignedPost方法在文件上传期间返回凭据错误

olqngx59  于 2023-06-29  发布在  Node.js
关注(0)|答案(1)|浏览(108)

我的问题是NodeJS中aws createPresignedPost方法的凭据错误。下面是我的代码块:

export async function getCognitoCredentials() {
  const stsClient = new STSClient({ region: 'eu-west-1' })

  const assumeRoleCommand = new AssumeRoleCommand({
    RoleArn: `arn:aws:iam::${id}:role/new-iam-cross-account-role`,
    RoleSessionName: 'serviceName',
    DurationSeconds: 3600,
  })

  const assumeRoleResponse = await stsClient.send(assumeRoleCommand)

  return {
    accessKeyId: assumeRoleResponse.Credentials?.AccessKeyId!,
    secretAccessKey: assumeRoleResponse.Credentials?.SecretAccessKey!,
    sessionToken: assumeRoleResponse.Credentials?.SessionToken!,
  }
}

const s3Client = new S3Client({ region: 'eu-west-1', credentials: getCognitoCredentials })

console.log(s3Client)
const { url, fields } = await createPresignedPost(s3Client, {
    Bucket: 'bucketName',
    Key: path,
    Conditions: [
        ['content-length-range', min, max],
        ['starts-with', '$Content-Type', 'image/'],
    ],
    Expires: 600,
})
console.log('url >>>', url)
console.log('fields >>>', fields)

我得到以下错误:

Uncaught (in promise) Error: Credential is missing

我查了证件,应该是真的。我不明白为什么我得到的错误。任何帮助将不胜感激。谢谢

ki0zmccv

ki0zmccv1#

getCognitoCredentials是一个函数,但你没有调用它:

const s3Client = new S3Client({ region: 'eu-west-1', credentials: getCognitoCredentials })

尝试调用该函数,看看是否得到相同的错误。

const s3Client = new S3Client({ region: 'eu-west-1', credentials: getCognitoCredentials() })

相关问题