Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix serverless_example.md #455

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 50 additions & 40 deletions v2/serverless_example.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,14 +143,12 @@ mkdir resources
Create the following JavaScript file, `widgets.js`, in the `resources` directory\.

```js
import {
const {
S3Client,
ListObjectsV2Command,
} from '@aws-sdk/client-s3';

// In the following code we are using AWS JS SDK v3
// See https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/index.html
ListObjectsV2Command
} = require('@aws-sdk/client-s3');
const S3 = new S3Client({});

const bucketName = process.env.BUCKET;

exports.main = async function(event, context) {
Expand All @@ -160,8 +158,8 @@ exports.main = async function(event, context) {
if (method === "GET") {
if (event.path === "/") {
const data = await S3.send(new ListObjectsV2Command({ Bucket: bucketName }));
const body = {
widgets: data.Contents.map(function(e) { return e.Key })
const body = {
widgets: data.KeyCount > 0 ? data.Contents.map(function(e) { return e.Key }) : []
};
return {
statusCode: 200,
Expand Down Expand Up @@ -576,20 +574,18 @@ The next step is to create Lambda functions to create, show, and delete individu
Replace the code in `widgets.js` \(in `resources`\) with the following\.

```js
import {
const {
S3Client,
ListObjectsV2Command,
GetObjectCommand,
PutObjectCommand,
DeleteObjectCommand
} from '@aws-sdk/client-s3';

// In the following code we are using AWS JS SDK v3
// See https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/index.html
DeleteObjectCommand,
} = require("@aws-sdk/client-s3");
const S3 = new S3Client({});

const bucketName = process.env.BUCKET;

exports.main = async function(event, context) {
exports.main = async function (event, context) {
try {
const method = event.httpMethod;
// Get name, if present
Expand All @@ -598,26 +594,35 @@ exports.main = async function(event, context) {
if (method === "GET") {
// GET / to get the names of all widgets
if (event.path === "/") {
const data = await S3.send(new ListObjectsV2Command({ Bucket: bucketName }));
const data = await S3.send(
new ListObjectsV2Command({ Bucket: bucketName })
);
const body = {
widgets: data.Contents.map(function(e) { return e.Key })
widgets:
data.KeyCount > 0
? data.Contents.map(function (e) {
return e.Key;
})
: [],
};
return {
statusCode: 200,
headers: {},
body: JSON.stringify(body)
body: JSON.stringify(body),
};
}

if (widgetName) {
// GET /name to get info on widget name
const data = await S3.send(new GetObjectCommand({ Bucket: bucketName, Key: widgetName}));
const body = data.Body.toString('utf-8');
const data = await S3.send(
new GetObjectCommand({ Bucket: bucketName, Key: widgetName })
);
const body = data.Body.toString("utf-8");

return {
statusCode: 200,
headers: {},
body: JSON.stringify(body)
body: JSON.stringify(body),
};
}
}
Expand All @@ -629,27 +634,29 @@ exports.main = async function(event, context) {
return {
statusCode: 400,
headers: {},
body: "Widget name missing"
body: "Widget name missing",
};
}

// Create some dummy data to populate object
const now = new Date();
const data = widgetName + " created: " + now;

const base64data = Buffer.from(data, 'binary');
const base64data = Buffer.from(data, "binary");

await S3.send(new PutObjectCommand({
Bucket: bucketName,
Key: widgetName,
Body: base64data,
ContentType: 'application/json'
}));
await S3.send(
new PutObjectCommand({
Bucket: bucketName,
Key: widgetName,
Body: base64data,
ContentType: "application/json",
})
);

return {
statusCode: 200,
headers: {},
body: data
body: data,
};
}

Expand All @@ -660,36 +667,39 @@ exports.main = async function(event, context) {
return {
statusCode: 400,
headers: {},
body: "Widget name missing"
body: "Widget name missing",
};
}

await S3.send(new DeleteObjectCommand({
Bucket: bucketName, Key: widgetName
}));
await S3.send(
new DeleteObjectCommand({
Bucket: bucketName,
Key: widgetName,
})
);

return {
statusCode: 200,
headers: {},
body: "Successfully deleted widget " + widgetName
body: "Successfully deleted widget " + widgetName,
};
}

// We got something besides a GET, POST, or DELETE
return {
statusCode: 400,
headers: {},
body: "We only accept GET, POST, and DELETE, not " + method
body: "We only accept GET, POST, and DELETE, not " + method,
};
} catch(error) {
} catch (error) {
var body = error.stack || JSON.stringify(error, null, 2);
return {
statusCode: 400,
headers: {},
body: body
}
body: body,
};
}
}
};
```

Wire up these functions to your API Gateway code at the end of the `WidgetService` constructor\.
Expand Down