AWS JavaScript SDK V3是否会自动从本地配置读取凭据?

发布于 2025-02-06 01:30:06 字数 1255 浏览 1 评论 0原文

我正在查看此页面 https://docs.aws.amazon.com/sdk-for-javascript ... ,这似乎暗示SDK将查看〜/.aws/.aws/recertentials和如果没有AWS_PROFILE环境VAR,请使用[默认]配置文件。

我正在我的本地计算机上运行一个nextJS应用程序,试图列出S3存储桶,获取错误:凭证缺少

我真的很想不必在Env Vars中指定信用,因为稍后我将将该应用程序部署到ECS,在那里它将使用IAM角色进行访问。

这是我的代码:

import { ListBucketsCommand } from '@aws-sdk/client-s3';
import React, { useEffect } from 'react';
import { s3Client } from '../lib/s3Client';

const S3Buckets = () => {
    useEffect(() => {
        async function getS3Buckets() {
            const input = {};
            const command = new ListBucketsCommand(input);
            const res = await s3Client.send(command);
            console.log(res);
        }
        getS3Buckets();
    }, []);

    return <div>{/* S3 Buckets eventually listed here */}</div>;
};

export default S3Buckets; 

带有S3Client助手如下:

import { S3Client } from '@aws-sdk/client-s3';

export const s3Client = new S3Client({ region: process.env.AWS_REGION });

I'm looking at this page https://docs.aws.amazon.com/sdk-for-javascript... and it seems to imply that the SDK will look at ~/.aws/credentials and take the [default] profile if there is no AWS_PROFILE environment var.

I'm running a NextJS app on my local machine trying to list S3 buckets, getting Error: Credential is missing.

I would really love not to have to specify the creds in env vars as I'll be deploying the app to ECS later where it will use an IAM Role for access.

Here's my code:

import { ListBucketsCommand } from '@aws-sdk/client-s3';
import React, { useEffect } from 'react';
import { s3Client } from '../lib/s3Client';

const S3Buckets = () => {
    useEffect(() => {
        async function getS3Buckets() {
            const input = {};
            const command = new ListBucketsCommand(input);
            const res = await s3Client.send(command);
            console.log(res);
        }
        getS3Buckets();
    }, []);

    return <div>{/* S3 Buckets eventually listed here */}</div>;
};

export default S3Buckets; 

with the s3Client helper as below:

import { S3Client } from '@aws-sdk/client-s3';

export const s3Client = new S3Client({ region: process.env.AWS_REGION });

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文