大佬教程收集整理的这篇文章主要介绍了node.js – 如何从Lambda函数解析AWS S3文件,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我想使用通过在S3存储桶中创建新对象而触发的Lambda函数来读取文件并提取一些数据并将其写入放置在另一个S3存储桶中的文件.
到目前为止,我的功能正常,将文件从一个S3存储桶复制到另一个存储桶,但我无法弄清楚如何添加一个函数来处理文件并将结果写入最终的S3目的地.
这些文件是简单的文本文件,我需要从文件中的每一行中提取数据.
如果我正在使用的Node.js代码添加了一个额外的函数来处理文件 – 请参阅注释?我在哪里寻求帮助.
// dependencies var async = require('async'); var AWS = require('aws-sdk'); var util = require('util'); // get reference to S3 client var s3 = new AWs.S3(); exports.handler = function(event,context) { // Read options from the event. console.log("Reading options from event:\n",util.inspect(event,{depth: 5})); var srcBucket = event.Records[0].s3.bucket.name; // Object key may have spaces or unicode non-ASCII characters. var srcKey = decodeURIComponent(event.Records[0].s3.object.key.replace(/\+/g," ")); var dstBucket = "inputBucket"; var dstKey = srcKey + ".txt"; // Sanity check: validate that source and desTination are different buckets. if (srcBucket == dstBucket) { console.error("DesTination bucket must not match source bucket."); return; } // Infer the file type. var typeMatch = srcKey.match(/\.([^.]*)$/); if (!typeMatch) { console.error('unable to infer file type for key ' + srcKey); return; } var imageType = typeMatch[1]; if (imageType != "txt") { console.log('skipping non-image ' + srcKey); return; } // Download the image from S3,transform,and upload to a different S3 bucket. async.waterfall([ function download(next) { // Download the file from S3 into a buffer. s3.getObject({ Bucket: srcBucket,Key: srcKey },next); },function transform(response,next) { // Read the file we have just downloaded // ? response.body ? var rl = require('readline').createInterface({ input: require('fs').createReadStream('file.in') }); // Process each line here wriTing the result to an output buffer? rl.on('line',function (linE) { console.log('Line from file:',linE); //Do something with the line... //Create some output String 'outputline' //Write 'outputline' to an output buffer 'outbuff' // ?? }); // Now pass the output buffer to the next function // so it can be uploaded to another S3 bucket // ?? next; } function upload(response,next) { // Stream the file to a different S3 bucket. s3.putObject({ Bucket: dstBucket,Key: dstKey,Body: response.body,ContentType: response.contentType },next); } ],function (err) { if (err) { console.error( 'Unable to process ' + srcBucket + '/' + srcKey + ' and upload to ' + dstBucket + '/' + dstKey + ' due to an error: ' + err ); } else { console.log( 'successfully processed ' + srcBucket + '/' + srcKey + ' and uploaded to ' + dstBucket + '/' + dstKey ); } context.done(); } ); };
以上是大佬教程为你收集整理的node.js – 如何从Lambda函数解析AWS S3文件全部内容,希望文章能够帮你解决node.js – 如何从Lambda函数解析AWS S3文件所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。