Reputation: 1595
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).
Upvotes: 6
Views: 2263
Reputation: 1595
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'}});
Upvotes: 1
Reputation: 565
I have done this before from a web page. That being said, I hear it is a really bad practice to expose your secret key to the public. I think most people have a server or lambda instance that signs the upload. However, it is fine for prototyping.
Anyway this code worked for me.
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)
This example uploaded a text file but you can also upload a Blob for an image.
Upvotes: 0