Skip to content

Commit

Permalink
Make samples compatible with Node 6 (pulumi#73)
Browse files Browse the repository at this point in the history
* Make samples compatible with Node 6
  • Loading branch information
lindydonna committed Jun 11, 2018
1 parent 1907e59 commit 7707585
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 17 deletions.
4 changes: 4 additions & 0 deletions cloud-js-httpendpoint/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

A simple REST API that counts the number of times a route has been hit. For a detailed walkthrough of this example, see the article [Create a Serverless REST API](https://docs.pulumi.com/quickstart/aws-rest-api.html).

> Note: this example uses JavaScript promises, but if you're using Node 8, you can change the code to use `async` and `await`.
## Deploying and running the program

1. Create a new stack:
Expand Down Expand Up @@ -62,6 +64,8 @@ A simple REST API that counts the number of times a route has been hit. For a de

$ curl $(pulumi stack output endpoint)/hello
{"route":"hello","count":1}
$ curl $(pulumi stack output endpoint)/hello
{"route":"hello","count":2}
$ curl $(pulumi stack output endpoint)/woohoo
{"route":"woohoo","count":1}
```
Expand Down
18 changes: 10 additions & 8 deletions cloud-js-httpendpoint/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,19 @@ let counterTable = new cloud.Table("counterTable", "route");
// Create an API endpoint
let endpoint = new cloud.HttpEndpoint("hello-world");

endpoint.get("/{route+}", async (req, res) => {
endpoint.get("/{route+}", (req, res) => {
let route = req.params["route"];
console.log(`Getting count for '${route}'`);

// get previous value and increment
let value = await counterTable.get({route}); // reference outer `counterTable` object
let count = (value && value.count) || 0;
await counterTable.insert( { route, count: ++count });

res.status(200).json({ route, count});
console.log(`Got count ${count} for '${route}'`);
// reference outer `counterTable` object
counterTable.get({ route }).then(value => {
let count = (value && value.count) || 0;
counterTable.insert({ route, count: ++count }).then(() => {
res.status(200).json({ route, count });
console.log(`Got count ${count} for '${route}'`);
});
});
});

module.exports.endpoint = endpoint.publish().url;
exports.endpoint = endpoint.publish().url;
13 changes: 8 additions & 5 deletions cloud-js-thumbnailer-machine-learning/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,32 +19,35 @@ const ffmpegThumbnailTask = new cloud.Task("ffmpegThumbTask", {
const videoProcessor = new video.VideoLabelProcessor();

// When a new video is uploaded, start Rekognition label detection
bucket.onPut("onNewVideo", async (bucketArgs) => {
bucket.onPut("onNewVideo", bucketArgs => {
console.log(`*** New video: file ${bucketArgs.key} was uploaded at ${bucketArgs.eventTime}.`);
videoProcessor.startRekognitionJob(bucketName.get(), bucketArgs.key);
return Promise.resolve();
}, { keySuffix: ".mp4" }); // run this Lambda only on .mp4 files

// When Rekognition processing is complete, run the FFMPEG task on the video file
// Use the timestamp with the highest confidence for the label "cat"
videoProcessor.onLabelResult("cat", async (file, framePos) => {
videoProcessor.onLabelResult("cat", (file, framePos) => {
console.log(`*** Rekognition processing complete for ${bucketName.get()}/${file} at timestamp ${framePos}`);
const thumbnailFile = file.substring(0, file.lastIndexOf('.')) + '.jpg';

// launch ffmpeg in a container, use environment variables to connect resources together
await ffmpegThumbnailTask.run({
ffmpegThumbnailTask.run({
environment: {
"S3_BUCKET": bucketName.get(),
"INPUT_VIDEO": file,
"TIME_OFFSET": framePos,
"OUTPUT_FILE": thumbnailFile,
},
}).then(() => {
console.log(`*** Launched thumbnailer task.`);
});
console.log("*** Launched thumbnailer task.");
});

// When a new thumbnail is created, log a message.
bucket.onPut("onNewThumbnail", async (bucketArgs) => {
bucket.onPut("onNewThumbnail", bucketArgs => {
console.log(`*** New thumbnail: file ${bucketArgs.key} was saved at ${bucketArgs.eventTime}.`);
return Promise.resolve();
}, { keySuffix: ".jpg" });

// Export the bucket name.
Expand Down
11 changes: 7 additions & 4 deletions cloud-js-thumbnailer/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright 2016-2018, Pulumi Corporation. All rights reserved.

const cloud = require("@pulumi/cloud-aws");
const aws = require("@pulumi/aws");

// A bucket to store videos and thumbnails.
const bucket = new cloud.Bucket("bucket");
Expand All @@ -14,27 +15,29 @@ const ffmpegThumbnailTask = new cloud.Task("ffmpegThumbTask", {

// When a new video is uploaded, run the FFMPEG task on the video file.
// Use the time index specified in the filename (e.g. cat_00-01.mp4 uses timestamp 00:01)
bucket.onPut("onNewVideo", async (bucketArgs) => {
bucket.onPut("onNewVideo", bucketArgs => {
console.log(`*** New video: file ${bucketArgs.key} was uploaded at ${bucketArgs.eventTime}.`);
const file = bucketArgs.key;

const thumbnailFile = file.substring(0, file.indexOf('_')) + '.jpg';
const framePos = file.substring(file.indexOf('_')+1, file.indexOf('.')).replace('-',':');

await ffmpegThumbnailTask.run({
ffmpegThumbnailTask.run({
environment: {
"S3_BUCKET": bucketName.get(),
"INPUT_VIDEO": file,
"TIME_OFFSET": framePos,
"OUTPUT_FILE": thumbnailFile,
},
}).then(() => {
console.log(`Running thumbnailer task.`);
});
console.log(`Running thumbnailer task.`);
}, { keySuffix: ".mp4" });

// When a new thumbnail is created, log a message.
bucket.onPut("onNewThumbnail", async (bucketArgs) => {
bucket.onPut("onNewThumbnail", bucketArgs => {
console.log(`*** New thumbnail: file ${bucketArgs.key} was saved at ${bucketArgs.eventTime}.`);
return Promise.resolve();
}, { keySuffix: ".jpg" });

// Export the bucket name.
Expand Down

0 comments on commit 7707585

Please sign in to comment.