如何在不创建 IAM 用户的情况下将文件从 EKS 上传到 S3 存储桶?

How to upload files from EKS to S3 bucket without creating IAM user?

我在 Kubernetes 环境中部署了一个 pod,并创建了服务帐户以使用完全访问权限访问我的 S3 存储桶。我想将我的日志上传到 s3 存储桶。

module.exports.uploadFile = () => {
const s3 = new AWS.S3();
const fileContent = fs.readFileSync(path.resolve(__dirname,'../logger/MyLogFile.log'))
const params = {
    Bucket: 'MYBUCKETNAME',
    Key: 'MyLogFile.log',
    Body: fileContent
};

s3.putObject(params, function(err, data) {
    if (err) {
        console.log(err)
        logger.error('file upload error')
        throw err;
    }
    logger.info(`File uploaded successfully. ${data.Location}`);
})}

这是我遇到的错误...

Error [CredentialsError]: Missing credentials in config, if using AWS_CONFIG_FILE, set AWS_SDK_LOAD_CONFIG=1 at Timeout.connectTimeout [as _onTimeout] (/usr/src/app/node_modules/aws-sdk/lib/http/node.js:69:15) at listOnTimeout (internal/timers.js:557:17) at processTimers (internal/timers.js:500:7) { code: 'CredentialsError', time: 2021-12-09T10:43:29.712Z, retryable: true, originalError: { message: 'Could not load credentials from any providers', code: 'CredentialsError', time: 2021-12-09T10:43:29.705Z, retryable: true, originalError: { message: 'EC2 Metadata roleName request returned error', code: 'TimeoutError', time: 2021-12-09T10:43:29.705Z, retryable: true, originalError: { message: 'Socket timed out without establishing a connection', code: 'TimeoutError', time: 2021-12-09T10:43:29.705Z, retryable: true } } } }

从 EKS 端到我的应用程序创建服务帐户解决了问题。

创建服务帐户后,我的文件更改将是

1.) aws.yml

serviceAccount:
enabled: true
name: MY_SERVICE_ACC_NAME

2.) 我的 AWS.S3 对象创建不会有任何变化

  const s3 = new AWS.S3();

现在 AWS.S3 对象将自动填充强制参数。