2019-01-09 12:18:47 -08:00
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
2024-01-16 10:41:11 -05:00
2019-01-09 12:18:47 -08:00
//
// This file is licensed under the Apache License, Version 2.0 (the "License").
// You may not use this file except in compliance with the License. A copy of the
// License is located at
//
// http://aws.amazon.com/apache2.0/
//
// This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
// OF ANY KIND, either express or implied. See the License for the specific
// language governing permissions and limitations under the License.
// snippet-start:[cdk.typescript.widgets.exports_main_v1]
2020-05-07 13:05:50 +01:00
/*
This code uses callbacks to handle asynchronous function responses.
It currently demonstrates using an async-await pattern.
AWS supports both the async-await and promises patterns.
For more information, see the following:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_promises
https://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/calling-services-asynchronously.html
https://docs.aws.amazon.com/lambda/latest/dg/nodejs-prog-model-handler.html
*/
2019-01-22 06:08:05 -08:00
const AWS = require ( "aws-sdk" ) ;
const S3 = new AWS . S3 ( ) ;
const bucketName = process . env . BUCKET ;
2019-01-09 12:18:47 -08:00
exports . main = async function ( event , context ) {
try {
var method = event . httpMethod ;
if ( method === "GET" ) {
if ( event . path === "/" ) {
const data = await S3 . listObjectsV2 ( { Bucket : bucketName } ) . promise ( ) ;
var body = {
widgets : data . Contents . map ( function ( e ) {
return e . Key ;
} ) ,
} ;
return {
statusCode : 200 ,
headers : { } ,
body : JSON . stringify ( body ) ,
} ;
}
}
// We only accept GET for now
return {
statusCode : 400 ,
headers : { } ,
body : "We only accept GET /" ,
} ;
} catch ( error ) {
var body = error . stack || JSON . stringify ( error , null , 2 ) ;
return {
statusCode : 400 ,
headers : { } ,
body : JSON . stringify ( body ) ,
} ;
}
} ;
2019-01-09 13:23:05 -08:00
// snippet-end:[cdk.typescript.widgets.exports_main_v1]