Skip to content

Commit 6393a43

Browse files
committed
Fix serverless_example.md
Fixes the broken update from Node14 and AWS-SDK v2 to Node18 and AWS-SDK v3
1 parent e5021c5 commit 6393a43

File tree

1 file changed

+50
-40
lines changed

1 file changed

+50
-40
lines changed

v2/serverless_example.md

+50-40
Original file line numberDiff line numberDiff line change
@@ -143,14 +143,12 @@ mkdir resources
143143
Create the following JavaScript file, `widgets.js`, in the `resources` directory\.
144144

145145
```js
146-
import {
146+
const {
147147
S3Client,
148-
ListObjectsV2Command,
149-
} from '@aws-sdk/client-s3';
150-
151-
// In the following code we are using AWS JS SDK v3
152-
// See https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/index.html
148+
ListObjectsV2Command
149+
} = require('@aws-sdk/client-s3');
153150
const S3 = new S3Client({});
151+
154152
const bucketName = process.env.BUCKET;
155153

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

578576
```js
579-
import {
577+
const {
580578
S3Client,
581579
ListObjectsV2Command,
582580
GetObjectCommand,
583581
PutObjectCommand,
584-
DeleteObjectCommand
585-
} from '@aws-sdk/client-s3';
586-
587-
// In the following code we are using AWS JS SDK v3
588-
// See https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/index.html
582+
DeleteObjectCommand,
583+
} = require("@aws-sdk/client-s3");
589584
const S3 = new S3Client({});
585+
590586
const bucketName = process.env.BUCKET;
591587

592-
exports.main = async function(event, context) {
588+
exports.main = async function (event, context) {
593589
try {
594590
const method = event.httpMethod;
595591
// Get name, if present
@@ -598,26 +594,35 @@ exports.main = async function(event, context) {
598594
if (method === "GET") {
599595
// GET / to get the names of all widgets
600596
if (event.path === "/") {
601-
const data = await S3.send(new ListObjectsV2Command({ Bucket: bucketName }));
597+
const data = await S3.send(
598+
new ListObjectsV2Command({ Bucket: bucketName })
599+
);
602600
const body = {
603-
widgets: data.Contents.map(function(e) { return e.Key })
601+
widgets:
602+
data.KeyCount > 0
603+
? data.Contents.map(function (e) {
604+
return e.Key;
605+
})
606+
: [],
604607
};
605608
return {
606609
statusCode: 200,
607610
headers: {},
608-
body: JSON.stringify(body)
611+
body: JSON.stringify(body),
609612
};
610613
}
611614

612615
if (widgetName) {
613616
// GET /name to get info on widget name
614-
const data = await S3.send(new GetObjectCommand({ Bucket: bucketName, Key: widgetName}));
615-
const body = data.Body.toString('utf-8');
617+
const data = await S3.send(
618+
new GetObjectCommand({ Bucket: bucketName, Key: widgetName })
619+
);
620+
const body = data.Body.toString("utf-8");
616621

617622
return {
618623
statusCode: 200,
619624
headers: {},
620-
body: JSON.stringify(body)
625+
body: JSON.stringify(body),
621626
};
622627
}
623628
}
@@ -629,27 +634,29 @@ exports.main = async function(event, context) {
629634
return {
630635
statusCode: 400,
631636
headers: {},
632-
body: "Widget name missing"
637+
body: "Widget name missing",
633638
};
634639
}
635640

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

640-
const base64data = Buffer.from(data, 'binary');
645+
const base64data = Buffer.from(data, "binary");
641646

642-
await S3.send(new PutObjectCommand({
643-
Bucket: bucketName,
644-
Key: widgetName,
645-
Body: base64data,
646-
ContentType: 'application/json'
647-
}));
647+
await S3.send(
648+
new PutObjectCommand({
649+
Bucket: bucketName,
650+
Key: widgetName,
651+
Body: base64data,
652+
ContentType: "application/json",
653+
})
654+
);
648655

649656
return {
650657
statusCode: 200,
651658
headers: {},
652-
body: data
659+
body: data,
653660
};
654661
}
655662

@@ -660,36 +667,39 @@ exports.main = async function(event, context) {
660667
return {
661668
statusCode: 400,
662669
headers: {},
663-
body: "Widget name missing"
670+
body: "Widget name missing",
664671
};
665672
}
666673

667-
await S3.send(new DeleteObjectCommand({
668-
Bucket: bucketName, Key: widgetName
669-
}));
674+
await S3.send(
675+
new DeleteObjectCommand({
676+
Bucket: bucketName,
677+
Key: widgetName,
678+
})
679+
);
670680

671681
return {
672682
statusCode: 200,
673683
headers: {},
674-
body: "Successfully deleted widget " + widgetName
684+
body: "Successfully deleted widget " + widgetName,
675685
};
676686
}
677687

678688
// We got something besides a GET, POST, or DELETE
679689
return {
680690
statusCode: 400,
681691
headers: {},
682-
body: "We only accept GET, POST, and DELETE, not " + method
692+
body: "We only accept GET, POST, and DELETE, not " + method,
683693
};
684-
} catch(error) {
694+
} catch (error) {
685695
var body = error.stack || JSON.stringify(error, null, 2);
686696
return {
687697
statusCode: 400,
688698
headers: {},
689-
body: body
690-
}
699+
body: body,
700+
};
691701
}
692-
}
702+
};
693703
```
694704

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

0 commit comments

Comments
 (0)