尝试将对象放入 s3 存储桶时配置中缺少凭据

Missing credentials in config when trying to put object to s3 bucket

我有一个快速服务正在尝试将对象写入 s3 存储桶,但我收到以下错误:

Missing credentials in config

我在本地担任我的 AWS 角色,在 .aws/credentials 中设置我的凭据,然后在我的 Dockerfile 中将它们复制到我的容器中。

RUN mkdir "/home/node/.aws" && touch "/home/node/.aws/config" && touch "/home/node/.aws/credentials"
RUN echo "${AWS_CREDENTIALS}" > "/home/node/.aws/credentials"

文档说:

The SDK automatically detects AWS credentials set as variables in your environment and uses them for SDK requests, eliminating the need to manage credentials in your application. The environment variables that you set to provide your credentials are:

AWS_ACCESS_KEY_ID

AWS_SECRET_ACCESS_KEY

AWS_SESSION_TOKEN (optional)

所以我在使用 sdk 时不必手动管理我的凭据?

目前写入s3 bucket的代码:

import s3 from 'aws-sdk/clients/s3';

const s3Client = new s3({region: process.env['region']});

async upload() {
   const params = {
      Bucket: process.env['bucket'],
      Key: 'test.json',
      Body: somejsonfile
   }

   const res = s3Client.upload(params).promise();
   return results;
}

node 是执行此代码的正确用户吗?

文件 /home/node/.aws/credentials 看起来像这样吗:

[default]
aws_access_key_id = <AAK>
aws_secret_access_key = <ASK>

要调试,您可以尝试以下操作: 您可以尝试直接在代码中添加您的 AWS 凭据,如下所示:

AWS.config.update({
    accessKeyId: "<AAK>",
    secretAccessKey: "<ASK>",
    "region": "<REGION>"
});

或者,您可以尝试删除 /home/node/.aws/credentials 文件并将凭据移至 /home/node/.aws/config 文件:

[default]
region=<REGION>
output=json
aws_access_key_id = <AAK>
aws_secret_access_key = <ASK>

或者您可以尝试在 Dockerfile 中添加凭据和区域作为环境变量:

ENV AWS_ACCESS_KEY_ID=<AAK>
ENV AWS_SECRET_ACCESS_KEY=<ASK>
ENV AWS_DEFAULT_REGION=<REGION>