-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add Lambda function to respond to S3 put events and add json files
- Loading branch information
Showing
3 changed files
with
111 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 () => { | ||
// | ||
// }) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |