Search code examples
amazon-web-servicesamazon-s3

Accessing AWS S3 bucket with permanent credentials


I want to have full control of my S3 buckets through a node.js appplication so I am using aws-sdk (

@aws-sdk/client-s3 v3).

I want sdk APIs to pick credential (AccessKey and SecretToken) of the IAM user (with full bucket control policy attached) which I give it during S3Client instantiation.

something like this

const s3Config = {
  region: 'ap-south-1',
  accessKeyId: 'accessKeyId',
  secretAccessKey: 'secretAccessKey'
};

const s3 = new S3Client(s3Config); 
s3.send() //Create, Read etc operations

but as it turns out that sdk picks credentails specially session token from .aws/credentials file.

I have already created an IAM admin user and using the admin user a new user with fill AmazonS3BucetPolicy attached and yes I am using those credential above but I get session token expire error.

I want my node application to ask user for AccessKeyId and SecretKey to send req to aws and fetch appropriate resposne and not be dependent on session tokens.

P.S forgive me if I am not able to put my point across very well

I tried passing credentials to S3Client constructor.And response was expired token asking me to aquire a new one use aws sso login command


Solution

  • Modify how you're providing the IAM user credentials as follows:

    const { S3Client } = require("@aws-sdk/client-s3");
    
    const s3Config = {
        region: 'ap-south-1',
        credentials: {
            accessKeyId: 'xxx',
            secretAccessKey: 'yyy'
        }
    }
    
    const client = new S3Client(s3Config);