0

I would like to implement a workflow to upload a csv file to AWS S3 using api gateway and a nodejs lambda function. Is there any blueprint out there, where I can start from.

Thank you in advance

1
  • Why was this question given a neg? Commented Jun 28, 2018 at 23:02

1 Answer 1

1

You can upload file to s3 bucket without back-end implemention ( aws-lambda) you can do it using client app.if you needed you can do it both way

  1. Upload csv to s3 using client app

I. Configure bucket (java script)

 var albumBucketName = 'BUCKET_NAME';
    var bucketRegion = 'REGION';
    var IdentityPoolId = 'IDENTITY_POOL_ID';

    AWS.config.update({
      region: bucketRegion,
      credentials: new AWS.CognitoIdentityCredentials({
        IdentityPoolId: IdentityPoolId
      })
    });

    var s3 = new AWS.S3({
      apiVersion: '2006-03-01',
      params: {Bucket: albumBucketName}
    });

II. Upload CSV

function addCSV(csvName) {
  var files = document.getElementById('csv_file').files;
  if (!files.length) {
    return alert('Please choose a file to upload first.');
  }
  var file = files[0];
  var fileName = file.name;
  var csvKey = encodeURIComponent(csvName) + '//';


  s3.upload({
    Key: csvKey,
    Body: file,
    ACL: 'public-read'
  }, function(err, data) {
    if (err) {
      return alert('There was an error uploading your csv: ', err.message);
    }
    alert('Successfully uploaded CSV.');

  });
}

if you not clear this one. you can use this docucument.

  1. Upload file using aws-lambda ( node.js )

I. configure sdk

var AWS = require('aws-sdk');
// Set the region 
AWS.config.update({region: 'REGION'});

II. upload CSV

// call S3 to retrieve upload file to specified bucket
var uploadParams = {Bucket: process.argv[2], Key: '', Body: ''};
var file = process.argv[3];

var fs = require('fs');
var fileStream = fs.createReadStream(file);
fileStream.on('error', function(err) {
  console.log('File Error', err);
});
uploadParams.Body = fileStream;

var path = require('path');
uploadParams.Key = path.basename(file);

// call S3 to retrieve upload file to specified bucket
s3.upload (uploadParams, function (err, data) {
  if (err) {
    console.log("Error", err);
  } if (data) {
    console.log("Upload Success", data.Location);
  }
});

More details read this article.

If you have any more question about this please comment here

Sign up to request clarification or add additional context in comments.

1 Comment

Thx for your helpful input. Do you know any other possibility to get an event body from api gateway and process it accordingly?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.