【问题标题】:AWS S3Client does not load credentials properlyAWS S3Client 未正确加载凭证
【发布时间】:2023-02-03 04:35:51
【问题描述】:

我对来自 aws sdk v3 的 S3 客户端有疑问:

如果我使用文档中指定的 S3Client 以及使用环境变量提供的凭据,我会收到错误 The AWS Access Key Id you provided does not exist in our records.

起初我以为是因为我没有使用正确的AWS_ACCESS_KEY_ID,但在客户端初始化后添加这一行解决了问题,并记录了正确的值:

s3.config.credentials().then(console.log)

最让我困扰的是,如果我在其他任何地方调用此行(即:在异步函数中),它并不能解决问题。

  • 为什么这个异步函数调用修复了其余的执行?
  • 它是否只是暂时修复客户端? (客户端为多个函数调用保持实例化)
  • 承诺是否可以延迟结束:在客户的第一通电话之后?
  • 为什么它在 s3 调用之前调用时不起作用(有或没有 await)?

这是我的代码:

const s3Config: S3ClientConfig = {}
s3Config.endpoint = new HttpRequest({...} as Endpoint) // used with a local s3 server
const s3 = new S3Client(s3Config);

// this is the hack
s3.config.credentials().then(console.log)

export const upload = async (...) => {
    // here it does not work
    // await s3.config.credentials().then(console.log)

    const streamUpload = new Upload({client: s3,...})
    return await streamUpload.done()
}


export const getTempLink = async (...) => {
    // here it does not work
    // await s3.config.credentials().then(console.log)

    //* Get the pre-signed url
    const command = new GetObjectCommand({Bucket,Key})
    return await getSignedUrl(s3 as any, command as any, { expiresIn })
}

谢谢你的帮助 !

【问题讨论】:

    标签: javascript typescript amazon-s3 aws-sdk


    【解决方案1】:

    我遇到了类似的问题

    首先是 aws-sdk 与 aws-sdk/client-s3 之间的区别

    我的问题是 .env 文件中的键名是这样的

    ACCESS_KEY_ID,秘密访问密钥,AWS_SESSION_TOKEN

    那行不通,所以我像下面这样更改名称并准备开始。它拿走了凭据

    AWS_ACCESS_KEY_ID,AWS_SECRET_ACCESS_KEY,AWS_SESSION_TOKEN,

    请记住,aws 有一个 chain of procedence 作为凭据

    【讨论】:

      猜你喜欢
      • 2022-09-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-07-26
      • 1970-01-01
      • 1970-01-01
      • 2021-01-18
      • 2021-06-28
      相关资源
      最近更新 更多