Skip to content

Commit

Permalink
add Lambda function to respond to S3 put events and add json files
Browse files Browse the repository at this point in the history
  • Loading branch information
dysmento committed Jan 5, 2024
1 parent 1927dbe commit 2e90cf6
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 0 deletions.
8 changes: 8 additions & 0 deletions api/src/functions/excelToJson/excelToJson.scenarios.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import type { ScenarioData } from '@redwoodjs/testing/api'

export const standard = defineScenario({
// Define the "fixture" to write into your test database here
// See guide: https://redwoodjs.com/docs/testing#scenarios
})

export type StandardScenario = ScenarioData<unknown>
54 changes: 54 additions & 0 deletions api/src/functions/excelToJson/excelToJson.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// import { S3EventRecord } from 'aws-lambda'

// import { handler } from './excelToJson'

// Improve this test with help from the Redwood Testing Doc:
// https://redwoodjs.com/docs/testing#testing-functions

describe('excelToJson function', () => {
// it('Should respond with 200', async () => {
// const record: S3EventRecord = {
// eventVersion: '2.0',
// eventSource: 'aws:s3',
// eventName: 'ObjectCreated:Put',
// eventTime: '1970-01-01T00:00:00.000Z',
// userIdentity: { principalId: 'test-principalId' },
// requestParameters: { sourceIPAddress: 'test-sourceIPAddress' },
// responseElements: {
// 'x-amz-request-id': 'test-x-amz-request-id',
// 'x-amz-id-2': 'test-x-amz-id-2',
// },
// awsRegion: 'us-east-1',
// s3: {
// s3SchemaVersion: '1.0',
// configurationId: 'test-configurationId',
// bucket: {
// name: 'test-bucket',
// arn: 'test-arn',
// ownerIdentity: {
// principalId: 'test-principalId',
// },
// },
// object: {
// key: 'test-key',
// size: 1234,
// eTag: 'test-etag',
// sequencer: 'test-sequencer',
// },
// },
// }
// const s3Event = {
// Records: [record],
// }
// const response = await handler(s3Event, null, null)
// const { data } = JSON.parse(response.body)
// expect(response.statusCode).toBe(200)
// expect(data).toBe('excelToJson function')
})

// You can also use scenarios to test your api functions
// See guide here: https://redwoodjs.com/docs/testing#scenarios
//
// scenario('Scenario test', async () => {
//
// })
49 changes: 49 additions & 0 deletions api/src/functions/excelToJson/excelToJson.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import {
S3Client,
GetObjectCommand,
PutObjectCommand,
} from '@aws-sdk/client-s3'
import { NodeJsClient } from '@smithy/types'
import { S3Event, S3Handler } from 'aws-lambda'
import { Workbook } from 'exceljs'

import { logger } from 'src/lib/logger'

const s3 = new S3Client({}) as NodeJsClient<S3Client>

export const handler: S3Handler = async (event: S3Event): Promise<void> => {
try {
const bucket = event.Records[0].s3.bucket.name
const key = event.Records[0].s3.object.key

// Download the Excel file from S3
const getObjectResponse = await s3.send(
new GetObjectCommand({ Bucket: bucket, Key: key })
)

if (getObjectResponse.Body) {
new Workbook().xlsx.read(getObjectResponse.Body)
const workbook = new Workbook()

const worksheet = workbook.worksheets[0]
const jsonData = worksheet.getSheetValues()

// Write JSON data to a file
const jsonFileName = `${key}.json` // Use the same key with .json extension
const jsonFileContent = JSON.stringify(jsonData)

// Upload the JSON file to the same bucket
s3.send(
new PutObjectCommand({
Bucket: bucket,
Key: jsonFileName,
Body: jsonFileContent,
ContentType: 'application/json',
})
)
}
} catch (error) {
logger.error('Error processing S3 event:', error)
throw error
}
}

0 comments on commit 2e90cf6

Please sign in to comment.