@@ -143,14 +143,12 @@ mkdir resources
143
143
Create the following JavaScript file, ` widgets.js ` , in the ` resources ` directory\.
144
144
145
145
``` js
146
- import {
146
+ const {
147
147
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' );
153
150
const S3 = new S3Client ({});
151
+
154
152
const bucketName = process .env .BUCKET ;
155
153
156
154
exports .main = async function (event , context ) {
@@ -160,8 +158,8 @@ exports.main = async function(event, context) {
160
158
if (method === " GET" ) {
161
159
if (event .path === " /" ) {
162
160
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 }) : []
165
163
};
166
164
return {
167
165
statusCode: 200 ,
@@ -576,20 +574,18 @@ The next step is to create Lambda functions to create, show, and delete individu
576
574
Replace the code in ` widgets.js ` \( in ` resources ` \) with the following\.
577
575
578
576
``` js
579
- import {
577
+ const {
580
578
S3Client ,
581
579
ListObjectsV2Command ,
582
580
GetObjectCommand ,
583
581
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" );
589
584
const S3 = new S3Client ({});
585
+
590
586
const bucketName = process .env .BUCKET ;
591
587
592
- exports .main = async function (event , context ) {
588
+ exports .main = async function (event , context ) {
593
589
try {
594
590
const method = event .httpMethod ;
595
591
// Get name, if present
@@ -598,26 +594,35 @@ exports.main = async function(event, context) {
598
594
if (method === " GET" ) {
599
595
// GET / to get the names of all widgets
600
596
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
+ );
602
600
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
+ : [],
604
607
};
605
608
return {
606
609
statusCode: 200 ,
607
610
headers: {},
608
- body: JSON .stringify (body)
611
+ body: JSON .stringify (body),
609
612
};
610
613
}
611
614
612
615
if (widgetName) {
613
616
// 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" );
616
621
617
622
return {
618
623
statusCode: 200 ,
619
624
headers: {},
620
- body: JSON .stringify (body)
625
+ body: JSON .stringify (body),
621
626
};
622
627
}
623
628
}
@@ -629,27 +634,29 @@ exports.main = async function(event, context) {
629
634
return {
630
635
statusCode: 400 ,
631
636
headers: {},
632
- body: " Widget name missing"
637
+ body: " Widget name missing" ,
633
638
};
634
639
}
635
640
636
641
// Create some dummy data to populate object
637
642
const now = new Date ();
638
643
const data = widgetName + " created: " + now;
639
644
640
- const base64data = Buffer .from (data, ' binary' );
645
+ const base64data = Buffer .from (data, " binary" );
641
646
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
+ );
648
655
649
656
return {
650
657
statusCode: 200 ,
651
658
headers: {},
652
- body: data
659
+ body: data,
653
660
};
654
661
}
655
662
@@ -660,36 +667,39 @@ exports.main = async function(event, context) {
660
667
return {
661
668
statusCode: 400 ,
662
669
headers: {},
663
- body: " Widget name missing"
670
+ body: " Widget name missing" ,
664
671
};
665
672
}
666
673
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
+ );
670
680
671
681
return {
672
682
statusCode: 200 ,
673
683
headers: {},
674
- body: " Successfully deleted widget " + widgetName
684
+ body: " Successfully deleted widget " + widgetName,
675
685
};
676
686
}
677
687
678
688
// We got something besides a GET, POST, or DELETE
679
689
return {
680
690
statusCode: 400 ,
681
691
headers: {},
682
- body: " We only accept GET, POST, and DELETE, not " + method
692
+ body: " We only accept GET, POST, and DELETE, not " + method,
683
693
};
684
- } catch (error) {
694
+ } catch (error) {
685
695
var body = error .stack || JSON .stringify (error, null , 2 );
686
696
return {
687
697
statusCode: 400 ,
688
698
headers: {},
689
- body: body
690
- }
699
+ body: body,
700
+ };
691
701
}
692
- }
702
+ };
693
703
```
694
704
695
705
Wire up these functions to your API Gateway code at the end of the ` WidgetService ` constructor\.
0 commit comments