Programatically import users to Cognito user pool from csv

0

I am trying to programtically import users into Cognito user pool from csv file.Invoked CognitoUserImportJobCommand from NodeJS SDK which returns a presinged url. (https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/clients/client-cognito-identity-provider/classes/createuserimportjobcommand.html)

While trying to upload csv to presigned url from lambda using form-data and axios we are getting 403 ERR_BAD_REQUEST. I also tried from client side using Form data. Bit it is throwing CORS error. Did anyone faced similar issues or have a solution for this.

2 Answers
1

Got the solution from AWS support team.

import { CognitoIdentityProviderClient, CreateUserImportJobCommand, StartUserImportJobCommand } from "@aws-sdk/client-cognito-identity-provider"; import axios from 'axios'; import fs from 'fs';

// Read the CSV file into a Buffer. const filePath = 'template.csv'; const fileBuffer = fs.readFileSync(filePath); const client = new CognitoIdentityProviderClient({ region: "us-east-1" });

console.log('Creating Import job\n'); const params = { "CloudWatchLogsRoleArn": "<IAMRoleARN>", "JobName": "TestNode", "UserPoolId": "<UserPoolId>" }; const command = new CreateUserImportJobCommand(params); const response = await client.send(command); const url = response.UserImportJob.PreSignedUrl; const job_id = response.UserImportJob.JobId; console.log('Presigned Url: ', url);

// Make an HTTP POST request to the presigned URL.

await axios.put(url, fileBuffer, { headers: { 'Content-Type': 'text/csv', 'Access-Control-Allow-Origin': '*', // Required for CORS support to work 'Access-Control-Allow-Credentials': true, // Required for cookies, authorization headers with HTTPS 'x-amz-server-side-encryption': 'aws:kms' }, }) .then(response => { console.log(File uploaded successfully with status ${response.status}); }) .catch(error => { console.error('Error uploading file:', error); });

console.log('Starting Import job'); const params2 = { "JobId": job_id, "UserPoolId": "<UserPoolId>" }

const command2 = new StartUserImportJobCommand(params2); const response2 = await client.send(command2); console.log(response2)

Kiran
answered a year ago
0

The first consideration is whether each of the guidance steps located in the documentation link below are applicable and have been followed. Cognito user pool import

AWS
AWSdave
answered a year ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions