【发布时间】: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