(DEV306) Building Cross-Platform Applications Using the AWS SDK for JavaScript | AWS re:Invent 2014

52
November 13, 2014 Las Vegas, NV Aditya Manohar, Amazon Web Services

Transcript of (DEV306) Building Cross-Platform Applications Using the AWS SDK for JavaScript | AWS re:Invent 2014

Page 1: (DEV306) Building Cross-Platform Applications Using the AWS SDK for JavaScript | AWS re:Invent 2014

November 13, 2014 Las Vegas, NV

Aditya Manohar, Amazon Web Services

Page 2: (DEV306) Building Cross-Platform Applications Using the AWS SDK for JavaScript | AWS re:Invent 2014

AWS SDK for JavaScript

December2012

AWS SDK for Node.jsDeveloperPreview

Page 3: (DEV306) Building Cross-Platform Applications Using the AWS SDK for JavaScript | AWS re:Invent 2014

AWS SDK for JavaScript

December2012

AWS SDK for Node.jsDeveloperPreview

October 2013

AWS SDK for JavaScriptDeveloper Preview

Page 4: (DEV306) Building Cross-Platform Applications Using the AWS SDK for JavaScript | AWS re:Invent 2014

AWS SDK for JavaScript

December2012

October 2013

AWS SDK for JavaScriptDeveloper Preview

AWS SDK for

JavaScript

June2014

AWS SDK for Node.jsDeveloperPreview

Page 5: (DEV306) Building Cross-Platform Applications Using the AWS SDK for JavaScript | AWS re:Invent 2014

AWS SDK for JavaScript

December2012

October 2013

AWS SDK for JavaScriptDeveloper Preview

AWS SDK for

JavaScript

June2014

re:Invent 2014

AWS SDK for Node.jsDeveloperPreview

Page 6: (DEV306) Building Cross-Platform Applications Using the AWS SDK for JavaScript | AWS re:Invent 2014

ubiquitous

• Not just browsers and Node.js anymore

• Android and iOS (e.g., PhoneGap)

• WinRT: Windows Desktop & Windows Phone

• Embedded devices: Cylon.js and Johnny-Five

Page 7: (DEV306) Building Cross-Platform Applications Using the AWS SDK for JavaScript | AWS re:Invent 2014

Goals

Overview of application

SDK features

Multiple environments & operational differences

Page 8: (DEV306) Building Cross-Platform Applications Using the AWS SDK for JavaScript | AWS re:Invent 2014

Video Transcoder

Page 9: (DEV306) Building Cross-Platform Applications Using the AWS SDK for JavaScript | AWS re:Invent 2014

Upload, encode, and download videos

AmazonS3

AmazonElastic

Transcoder

Upload

Download

Page 10: (DEV306) Building Cross-Platform Applications Using the AWS SDK for JavaScript | AWS re:Invent 2014

Demo

Page 11: (DEV306) Building Cross-Platform Applications Using the AWS SDK for JavaScript | AWS re:Invent 2014

github.com/awslabs/

aws-sdk-js-sample-video-transcoder

Page 12: (DEV306) Building Cross-Platform Applications Using the AWS SDK for JavaScript | AWS re:Invent 2014

ArchitectureAWS SDK for JavaScript

app.js

Page 13: (DEV306) Building Cross-Platform Applications Using the AWS SDK for JavaScript | AWS re:Invent 2014
Page 14: (DEV306) Building Cross-Platform Applications Using the AWS SDK for JavaScript | AWS re:Invent 2014

var AWS = require('aws-sdk');

// Construct a service object

var s3 = new AWS.S3();

// Making requests

s3.listObjects({Bucket: 'bucket'}, function(err,data){

if (err) console.log(err);

else console.log(data);

});

Page 15: (DEV306) Building Cross-Platform Applications Using the AWS SDK for JavaScript | AWS re:Invent 2014

var s3 = new AWS.S3();

var req = s3.listObjects({Bucket: 'bucket'});

// Attaching life cycle callbacks

req.on('success', function(response){

console.log(response.data);

});

req.send();

Page 16: (DEV306) Building Cross-Platform Applications Using the AWS SDK for JavaScript | AWS re:Invent 2014

httpUploadProgress & httpDownloadProgress

req.on('httpUploadProgress', function(progress) {

console.log(progress.loaded + ' of ' + progress.total);

});

Page 17: (DEV306) Building Cross-Platform Applications Using the AWS SDK for JavaScript | AWS re:Invent 2014

var params = {id: 'jobId'};

transcoder.waitFor('jobComplete', params, function(err, data) {

if (err) console.log(err);

else console.log(data);

});

Page 18: (DEV306) Building Cross-Platform Applications Using the AWS SDK for JavaScript | AWS re:Invent 2014

Video Transcoder (contd.)

Page 19: (DEV306) Building Cross-Platform Applications Using the AWS SDK for JavaScript | AWS re:Invent 2014

Application workflow

Start Create Pipeline

1

Upload Files

2

Create Job

3

Attach Waiter

4 5

PollJob

6

DownloadFiles

Page 20: (DEV306) Building Cross-Platform Applications Using the AWS SDK for JavaScript | AWS re:Invent 2014

Node.js

console.js

github.com/awslabs/

aws-sdk-js-sample-video-transcoder

AWS SDK for JavaScript

app.js

Page 21: (DEV306) Building Cross-Platform Applications Using the AWS SDK for JavaScript | AWS re:Invent 2014

$> npm install aws-sdk

Page 22: (DEV306) Building Cross-Platform Applications Using the AWS SDK for JavaScript | AWS re:Invent 2014

credentials

# Shared credentials file

# ~/.aws/credentials

[default]

aws_access_key_id = foo

aws_secret_access_key = bar

[development]

aws_access_key_id = baz

aws_secret_access_key = quux

Page 23: (DEV306) Building Cross-Platform Applications Using the AWS SDK for JavaScript | AWS re:Invent 2014

app.jsApp.prototype.jobCreated = function(response) {

self.publish('app:request-success', data, message);

self.publish('app:job-complete', data);

};

Page 24: (DEV306) Building Cross-Platform Applications Using the AWS SDK for JavaScript | AWS re:Invent 2014

console.js

Application.subscribe('app:request-success', printNotification);

Application.subscribe('app:job-complete', downloadObject);

Page 25: (DEV306) Building Cross-Platform Applications Using the AWS SDK for JavaScript | AWS re:Invent 2014

console.js

var printNotification = function(response, message) {

process.stdout.write(message.text + '\n');

};

Page 26: (DEV306) Building Cross-Platform Applications Using the AWS SDK for JavaScript | AWS re:Invent 2014

console.js

var downloadObject = function(responseData){

// . . .

var req = s3.getObject({Bucket: Application.contentBucket, Key:key});

var stream = req.createReadStream();

// . . .

stream.on('data', function(data){

progress.loaded += data.length;

}).pipe(writeStream);

// . . .

};

Page 27: (DEV306) Building Cross-Platform Applications Using the AWS SDK for JavaScript | AWS re:Invent 2014

Why?

Page 28: (DEV306) Building Cross-Platform Applications Using the AWS SDK for JavaScript | AWS re:Invent 2014

Single

Page 29: (DEV306) Building Cross-Platform Applications Using the AWS SDK for JavaScript | AWS re:Invent 2014

Reduced maintenance cost

Page 30: (DEV306) Building Cross-Platform Applications Using the AWS SDK for JavaScript | AWS re:Invent 2014

Easy

Page 31: (DEV306) Building Cross-Platform Applications Using the AWS SDK for JavaScript | AWS re:Invent 2014

Easy

Page 32: (DEV306) Building Cross-Platform Applications Using the AWS SDK for JavaScript | AWS re:Invent 2014

Any application that can be written in JavaScript will eventually be written in JavaScript

- Jeff Atwood, 2007

Page 33: (DEV306) Building Cross-Platform Applications Using the AWS SDK for JavaScript | AWS re:Invent 2014
Page 34: (DEV306) Building Cross-Platform Applications Using the AWS SDK for JavaScript | AWS re:Invent 2014

Web Application

view.js

github.com/awslabs/

aws-sdk-js-sample-video-transcoder

AWS SDK for JavaScript

app.js

Page 35: (DEV306) Building Cross-Platform Applications Using the AWS SDK for JavaScript | AWS re:Invent 2014

$> bower install aws-sdk

Page 36: (DEV306) Building Cross-Platform Applications Using the AWS SDK for JavaScript | AWS re:Invent 2014

Use Amazon Cognito

No localStorage

Page 37: (DEV306) Building Cross-Platform Applications Using the AWS SDK for JavaScript | AWS re:Invent 2014

Configure CORS on Amazon S3

Page 38: (DEV306) Building Cross-Platform Applications Using the AWS SDK for JavaScript | AWS re:Invent 2014

Application.subscribe('app:request-progress', showProgress);

<progress max="100" data-message-id></progress>

jQuery UI

Page 39: (DEV306) Building Cross-Platform Applications Using the AWS SDK for JavaScript | AWS re:Invent 2014

Application.subscribe('app:request-success', notify);

Application.subscribe('app:request-failure', notify);

(noty, Notify.js, MiniJS, etc.)

Page 40: (DEV306) Building Cross-Platform Applications Using the AWS SDK for JavaScript | AWS re:Invent 2014

AWS SDK for JavaScript

app.js

Google Chrome Extension

ui.js options.js

github.com/awslabs/

aws-sdk-js-sample-video-transcoder

Page 41: (DEV306) Building Cross-Platform Applications Using the AWS SDK for JavaScript | AWS re:Invent 2014

No CORS Configuration: manifest.json

{

"manifest_version": 2,

"name": "Transcoder",

"permissions": [

"notifications",

"https://*.amazonaws.com/"

]

}

Page 42: (DEV306) Building Cross-Platform Applications Using the AWS SDK for JavaScript | AWS re:Invent 2014

options.js

var saveConfiguration = function (evt) {

localStorage.config = JSON.stringify(config);

}

Page 43: (DEV306) Building Cross-Platform Applications Using the AWS SDK for JavaScript | AWS re:Invent 2014

ui.js: N

var notify = function(response, message) {

var options = {

type: 'basic',

iconUrl: assetUrls[message.sender],

title: 'Status',

message: message.text

};

chrome.notifications.create('', options, noop);

};

Page 44: (DEV306) Building Cross-Platform Applications Using the AWS SDK for JavaScript | AWS re:Invent 2014

AWS SDK for JavaScriptApps for

Windows RT

github.com/awslabs/

aws-sdk-js-sample-video-transcoder

home.js progress.js

transcoder.js

app.js

Page 45: (DEV306) Building Cross-Platform Applications Using the AWS SDK for JavaScript | AWS re:Invent 2014

But first…

Page 46: (DEV306) Building Cross-Platform Applications Using the AWS SDK for JavaScript | AWS re:Invent 2014

Windows Runtime Windows API surface

- msdn.microsoft.com

Page 47: (DEV306) Building Cross-Platform Applications Using the AWS SDK for JavaScript | AWS re:Invent 2014

transcoder.js

setCredentials: function (resource, username, password) {

var vault = new Windows.Security.Credentials.PasswordVault();

var credential = new Windows.Security.Credentials

.PasswordCredential(resource, username, password);

vault.add(credential);

}

Page 48: (DEV306) Building Cross-Platform Applications Using the AWS SDK for JavaScript | AWS re:Invent 2014

transcoder.js

saveConfiguration: function (config) {

localSettings.values.config = composite;

}

Page 49: (DEV306) Building Cross-Platform Applications Using the AWS SDK for JavaScript | AWS re:Invent 2014

progress.js

<progress max="100" data-message-id></progress>

(+custom progress manager code)

Page 50: (DEV306) Building Cross-Platform Applications Using the AWS SDK for JavaScript | AWS re:Invent 2014

home.jsnotify: function (response, message) {

toast = new notifications.ToastNotification(toastXml);

toastNotifier = notifications.ToastNotificationManager

.createToastNotifier();

toastNotifier.show(toast);

}

Page 51: (DEV306) Building Cross-Platform Applications Using the AWS SDK for JavaScript | AWS re:Invent 2014

Summary

Page 52: (DEV306) Building Cross-Platform Applications Using the AWS SDK for JavaScript | AWS re:Invent 2014

http://bit.ly/awsevals

@awsforjs

github.com/

aws/aws-sdk-js

blogs.aws.amazon.com/

javascript/