Skip to content

Commit 8e7475b

Browse files
committed
binary content example
1 parent d6675f3 commit 8e7475b

File tree

7 files changed

+161
-0
lines changed

7 files changed

+161
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
* [Using AWS Mobile Analytics for server-side events](aws-mobile-analytics) – log events into AWS Mobile Analytics for easy internal telemetry
3535
* [Custom Authorizers](custom-authorizers) – a simple example of how to set up custom authorizers in API Gateway
3636
* [Geo location](api-gw-geolocation) – shows how to retrieve the country where your users originated the request
37+
* [Binary Content](binary-content) – shows how to configure API Gateway to send or receive binary data
3738

3839
## Event processing
3940

binary-content/README.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Handling binary content with API Gateway
2+
3+
This project demonstrates how to configure API Gateway using Claudia API Builder to support binary content handling. There are three endpoints:
4+
5+
* `/img`: shows how to receive a text request and respond with binary content
6+
* `/info` endpoint shows how to receive binary content and respond with text
7+
* `/thumb`: endpoint shows how to receive and respond with binary content
8+
9+
## Try it out
10+
11+
* run `npm install` to download the dependencies
12+
* run `npm run create` to deploy the project to Lambda. Grab the API ID from the result and replace `<API-ID>` in the example commands below
13+
14+
### retrieve a picture (text request, binary result)
15+
16+
```
17+
curl https://<API-ID>.execute-api.us-east-1.amazonaws.com/latest/img -H "Accept: image/png" > 1.png
18+
```
19+
20+
### get info about a pic (binary request, text result)
21+
22+
```
23+
curl --request POST -H "Content-Type: image/png" --data-binary "@img.png" https://<API-ID>.execute-api.us-east-1.amazonaws.com/latest/info
24+
```
25+
26+
### make a thumbnail (binary post, binary result)
27+
28+
29+
```
30+
curl --request POST -H "Content-Type: image/png" -H "Accept: image/png" --data-binary "@img.png" https://<API-ID>.execute-api.us-east-1.amazonaws.com/latest/thumb > thumb.png
31+
```
32+
33+
## More information
34+
35+
Check out the [Handling Binary Content Tutorial](https://claudiajs.com/tutorials/binary-content.html) for more information on these features, and see the [Binary Data Type support for API Gateway](https://aws.amazon.com/about-aws/whats-new/2016/11/binary-data-now-supported-by-api-gateway/) for more information on how things are configured under the hood.
36+
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*global module, require, console, Promise */
2+
const childProcess = require('child_process'),
3+
execPromise = function (command) {
4+
'use strict';
5+
return new Promise(function (resolve, reject) {
6+
childProcess.exec(command, function (err) {
7+
if (err) {
8+
reject(err);
9+
} else {
10+
resolve();
11+
}
12+
});
13+
});
14+
},
15+
spawnPromise = function (command, options) {
16+
'use strict';
17+
return new Promise(function (resolve, reject) {
18+
const process = childProcess.spawn(command, options),
19+
result = [];
20+
process.stdout.on('data', function (buffer) {
21+
console.log(buffer.toString());
22+
result.push(buffer.toString());
23+
});
24+
process.stderr.on('data', function (buffer) {
25+
console.error(buffer.toString());
26+
});
27+
process.on('close', function (code) {
28+
if (code !== 0) {
29+
reject(code);
30+
} else {
31+
resolve(result.join(''));
32+
}
33+
});
34+
});
35+
};
36+
module.exports = {
37+
exec: execPromise,
38+
spawn: spawnPromise
39+
};

binary-content/fs-promise.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
const fs = require('fs'),
2+
promisify = function (target, methodName) {
3+
'use strict';
4+
target[methodName + 'Promise'] = function () {
5+
const originalArgs = Array.prototype.slice.call(arguments);
6+
return new Promise((resolve, reject) => {
7+
const cb = function (err, data) {
8+
if (err) {
9+
reject(err);
10+
} else {
11+
resolve(data);
12+
}
13+
};
14+
originalArgs.push(cb);
15+
target[methodName].apply(target, originalArgs);
16+
});
17+
};
18+
};
19+
20+
promisify(fs, 'writeFile');
21+
promisify(fs, 'readFile');
22+
promisify(fs, 'unlink');
23+
promisify(fs, 'rename');
24+
25+
module.exports = fs;

binary-content/img.png

155 KB
Loading

binary-content/main.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*global exports*/
2+
const path = require('path'),
3+
os = require('os'),
4+
ApiBuilder = require('claudia-api-builder'),
5+
fs = require('./fs-promise'),
6+
childProcess = require('./child-process-promise'),
7+
api = new ApiBuilder();
8+
9+
module.exports = api;
10+
11+
//api.setBinaryMediaTypes(['image/*']);
12+
13+
api.get('/img', () => {
14+
'use strict';
15+
return fs.readFilePromise(path.join(__dirname, 'img.png'));
16+
}, { success: { contentType: 'image/png', contentHandling: 'CONVERT_TO_BINARY'}});
17+
18+
19+
api.post('/info', (request) => {
20+
'use strict';
21+
const tempFileName = path.join(os.tmpdir(), request.lambdaContext.awsRequestId);
22+
let result;
23+
return fs.writeFilePromise(tempFileName, request.body)
24+
.then(() => childProcess.spawn('/usr/bin/identify', [tempFileName]))
25+
.then(picInfo => result = picInfo.replace(/[^\s]*\s/, ''))
26+
.then(() => fs.unlinkPromise(tempFileName))
27+
.then(() => result);
28+
}, { success: { contentType: 'text/plain' } });
29+
30+
api.post('/thumb', (request) => {
31+
'use strict';
32+
const tempFileName = path.join(os.tmpdir(), request.lambdaContext.awsRequestId),
33+
thumbFileName = tempFileName + '-thumb.png';
34+
let result;
35+
return fs.writeFilePromise(tempFileName, request.body)
36+
.then(() => childProcess.spawn('/usr/bin/convert', ['-resize', '150x', tempFileName, thumbFileName]))
37+
.then(() => fs.readFilePromise(thumbFileName))
38+
.then(fileContents => result = fileContents)
39+
.then(() => fs.unlinkPromise(tempFileName))
40+
.then(() => fs.unlinkPromise(thumbFileName))
41+
.then(() => result);
42+
}, { success: { contentType: 'image/png', contentHandling: 'CONVERT_TO_BINARY' } });

binary-content/package.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"name": "binary-content",
3+
"version": "1.0.0",
4+
"description": "",
5+
"scripts": {
6+
"create": "claudia create --region us-east-1 --api-module main",
7+
"update": "claudia update"
8+
},
9+
"keywords": [],
10+
"author": "Gojko Adzic <gojko@neuri.co.uk>",
11+
"license": "MIT",
12+
"devDependencies": {
13+
"claudia": "^2.6.0"
14+
},
15+
"dependencies": {
16+
"claudia-api-builder": "^2.4.0"
17+
}
18+
}

0 commit comments

Comments
 (0)