documents/dev/snippets/aws/Lambda S3 getObject.md

Lambda s3.getObject

const resJSON = (obj)=> {
  const body = typeof obj === 'string' ? obj : JSON.stringify(obj);
  return {
    status: 200,
    statusDescription: "OK",
    body: body,
    headers: {
      "content-type": [{
          "key": "Content-Type",
          "value": "application/json; charset=utf-8"
      }]
    }
  };
};

exports.handler = async(event, context) => {
  if(request.uri.match(/\/AssetLibrary\/jupa3p$/)) {
    const s3 = new AWS.S3({region: 'us-east-1'});
    const data = await s3.getObject({
      Bucket: 'jupa',
      Key: 'AssetLibrary/_tree.3p.json'
    }).promise();
    return resJSON({
      contentType: data.ContentType,
      contentLength: data.ContentLength,
      ETag: data.ETag,
      type: data.Body.constructor.name,
      body: data.Body.toString()
    });
  }
}