Store data in collection from s3.getObject


#1

Hey guys!
I am trying to get some data from s3.getObject and then store it in a collection.
I am having issues with async functions and Meteor needing to run in Fibers.

I’ve gone over some of the meteor docs and the “Why Fibers Make Sense For Meteor” talk (http://benjamn.github.io/goto2015-talk/#/1) to no avail.

s3.getObject has two flavors, and I have tried some implementations of both:
s3.getObject(params, function(err, data) { if (err) console.log(err, err.stack); // an error occurred else console.log(data); // successful response });
or
request = s3.getObject({Bucket: 'bucket', Key: 'key'}); request.on('complete', function(response) { ... }); // register a callback request.send();

I just can’t seem to figure out the async nature of these functions with the combination of Meteor Collection.insert needing to run in a Fiber. I logically understand it but I don’t know how to code it!

Thanks guys.


#2

So, the first method can be done with Meteor.wrapAsync:

const getS3Object = Meteor.wrapAsync(s3.getObject, s3);
try {
  data = getS3Object(params);
  console.log(data); // successful response
} catch (err) {
  console.log(err, err.stack); // an error occurred
}