Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using AWS SDK with web workers

Total noob on ionic/cordova/angular here. Started last week, so I am struggling here. I am trying to upload files from an app (on iOS), which was created using Ionic and Cordova. The files are images and thus are very big. I want to upload these images in a background worker thread. Thus comes the need for web workers.

The images have to be uploaded to Amazon S3. I have the following code in my worker javascript file.

onmessage = function(e) {
  importScripts("aws-sdk.min.js");
  console.log("Doing work");
  self.AWS.config.region = 'us-east-1';  //Error here. config is not defined :(
  self.AWS.config.update({accessKeyId: 'XXX', secretAccessKey: 'ABCD'});
  //More AWS stuff
  postMessage("DONE");
}

My main javascript file is all fine, because I tried it out with non-AWS configurations (plain old console.log("stuff here")) and it works all well. It starts failing as soon as I try to do anything with the AWS SDK. Also, the aws-sdk.min.js is being imported correctly (atleast Chrome shows no error on the console).

like image 637
Satadru Biswas Avatar asked Nov 09 '22 23:11

Satadru Biswas


1 Answers

Aha, this seems to be solving my problems http://www.jefferydurand.com/amazon/web/services/worker/2015/05/08/amazon-javascript-sdk-web-worker.html

Interestingly, it didn't work with aws-sdk-2.2.3 but worked with the one shown in the example.

From the website:

  // this was the trick I needed to get the aws sdk to load.
  // web workers don't have a 'window' object but the library assumes 
  // there is a window object
  window = {};
  importScripts('https://sdk.amazonaws.com/js/aws-sdk-2.1.27.min.js');

  // initialize our dynamodb table and get it ready to accept values
  window.AWS.config.update({accessKeyId: 'XXXXXXXXXX', secretAccessKey: 'XXXXXXXJJJJXXXXX'});
  window.AWS.config.region = 'us-east-1';
  var table = new window.AWS.DynamoDB({params: {TableName: 'song_player_metrics'}});
like image 197
Satadru Biswas Avatar answered Nov 14 '22 21:11

Satadru Biswas