Reputation: 21
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
Upvotes: 0
Views: 4764
Reputation: 3047
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
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.
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
Upvotes: 1