Skip to content

Instantly share code, notes, and snippets.

@odbol
Created April 28, 2022 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save odbol/2dca19186346c43f40da343acfc405b2 to your computer and use it in GitHub Desktop.
Save odbol/2dca19186346c43f40da343acfc405b2 to your computer and use it in GitHub Desktop.
Node.js script to read AWS Credentials from the CVS file that the AWS Console provides you. Baffling that they don't offer this by default...
import {promises as fs} from 'fs';
import os from 'os';
import { parse } from 'csv-parse/sync';
/**
* Creates a credential provider to pass to S3Client which reads a credentials
* CSV downloaded from the AWS console.
*
* @param csvFile string path of the CSV file from AWS console.
* @returns a Provider<Credentials> to give to S3Client as the credentials option.
*/
export async function createProvider(csvFile) {
const content = await fs.readFile(csvFile);
// Parse the CSV content
const records = parse(content, {
columns: true
});
//console.log('cols', records)
return {
accessKeyId: records[records.length - 1]['Access key ID'],
secretAccessKey: records[records.length - 1]['Secret access key']
}
}
import {createProvider} from './CsvCredentialsProvider.mjs';
// Create an Amazon S3 service client object.
const s3Client = new SDK.S3Client({
region: REGION,
credentials: createProvider("new_user_credentials.csv")
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment