AWS Code Sample
Catalog

widgetsv1.js

// Copyright 2010-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. // // 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. const AWS = require('aws-sdk'); const S3 = new AWS.S3(); const bucketName = process.env.BUCKET; 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) } } }

Sample Details

Service: cdk

Last tested: 2019-2-8

Author: Doug-AWS

Type: snippet

On this page: