Using the AWS SDK with Web Workers

Total noob on ionic / cordova / angular here. It started last week, so I'm scared here. I am trying to download files from an application (on iOS) that was created using Ionic and Cordova. Files are images and therefore very large. I want to load these images into a background workflow. Thus, there is a need for web workers.

Images must be uploaded to Amazon S3. I have the following code in a javascript working 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 okay because I tried it with non-AWS configurations (plain old console.log("stuff here"))and it works fine. It starts crashing as soon as I try to do something using the AWS SDK. It is aws-sdk.min.jsimported correctly (at least , Chrome does not show errors on the console).

+4
source share
2 answers

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

Interestingly, it did not work with aws-sdk-2.2.3, but it worked with the one shown in this 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'}});
+1
source

-. , , , . , -, . , .

.

var accessKeyId = "public key here"
var secretAccessKey =  "secret key here" // secret key. This should be hidden. Maybe on a server or lambda instance. 
var bucketName = "my-new-bucket"
var region = "us-west-2"
//make bucket
AWS.config.update({accessKeyId: accessKeyId, secretAccessKey: secretAccessKey})
AWS.config.region = region
var bucket = new AWS.S3({
  params: {
    Bucket: bucketName
  }
})
// upload something into bucket
//    note: you need to define a callbackFunction
bucket.putObject({
  Key: "test.txt,
  Body: "hello world. this is just a test."
}, callbackFunction)

, Blob .

0

Source: https://habr.com/ru/post/1620262/


All Articles