简体   繁体   中英

How to upload a csv file to AWS S3 using API gateway and lambda

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM