forked from sannya-singal/sample-transcribe-app
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
nodejs20.x - migrate sample from aws-sdk v2 to v3 (#10)
- Loading branch information
1 parent
7f84c71
commit 6d8ff59
Showing
11 changed files
with
224 additions
and
179 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
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
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 |
---|---|---|
@@ -1,9 +1,13 @@ | ||
{ | ||
"dependencies": { | ||
"aws-sdk": "^2.1386.0", | ||
"serverless": "^3.31.0", | ||
"serverless-lift": "^1.26.1", | ||
"serverless-localstack": "^1.1.1" | ||
} | ||
"type":"module", | ||
"dependencies": { | ||
"aws-sdk": "^2.1386.0", | ||
"serverless": "^3.38.0", | ||
"serverless-lift": "^1.26.1", | ||
"serverless-localstack": "^1.1.1", | ||
"@aws-sdk/client-s3": "^3.0.0", | ||
"@aws-sdk/client-sqs": "^3.0.0", | ||
"@aws-sdk/client-ses": "^3.0.0", | ||
"@aws-sdk/client-transcribe": "^3.0.0" | ||
} | ||
} | ||
|
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
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
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
This file was deleted.
Oops, something went wrong.
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,122 @@ | ||
import { S3, GetObjectCommand } from "@aws-sdk/client-s3"; | ||
import { SES, SendEmailCommand } from "@aws-sdk/client-ses"; | ||
import { SQS, SendMessageCommand } from "@aws-sdk/client-sqs"; | ||
|
||
const endpoint = process.env.AWS_ENDPOINT_URL; | ||
const sqs = new SQS({ | ||
endpoint: endpoint, | ||
credentials: { | ||
accessKeyId:'test', | ||
secretAccessKey:'test' | ||
}, | ||
}); | ||
const ses = new SES({ | ||
endpoint: endpoint, | ||
credentials: { | ||
accessKeyId:'test', | ||
secretAccessKey:'test' | ||
}, | ||
}); | ||
const s3Client = new S3({ | ||
endpoint: endpoint, | ||
forcePathStyle: true, | ||
credentials: { | ||
accessKeyId:'test', | ||
secretAccessKey:'test' | ||
}, | ||
}); | ||
const queueUrl = `${endpoint}/000000000000/aws-node-sample-transcribe-s3-local-jobs`; | ||
const transcriptionBucket = process.env.S3_TRANSCRIPTION_BUCKET | ||
|
||
// This function consumes the event from s3 PutObject and pushes a new message to SQS. | ||
const producer = async (event, context, callback) => { | ||
let statusCode = 200; | ||
let message; | ||
|
||
try { | ||
// Get the record from the s3 event | ||
const records = event.Records; | ||
const sqsSendMessagePromises = records.map(async (record) => { | ||
const params = { | ||
Bucket: transcriptionBucket, | ||
Key: record.s3.object.key, | ||
}; | ||
|
||
try { | ||
const data = await s3Client.send(new GetObjectCommand(params)); | ||
const str = await data.Body.transformToString(); | ||
console.log("str: ", str); | ||
|
||
const jsonContent = await JSON.parse(str); | ||
console.log("jsonContent: ", jsonContent); | ||
|
||
// Send message to SQS queue | ||
const sendMessageParams = { | ||
QueueUrl: queueUrl, | ||
MessageBody: jsonContent.results.transcripts[0].transcript, | ||
MessageAttributes: { | ||
AttributeName: { | ||
StringValue: "Attribute Value", | ||
DataType: "String", | ||
}, | ||
}, | ||
}; | ||
await sqs.send(new SendMessageCommand(sendMessageParams)); | ||
} catch (err) { | ||
console.error("Error getting object from S3 bucket: ", transcriptionBucket, err); | ||
throw err; | ||
} | ||
}); | ||
|
||
await Promise.all(sqsSendMessagePromises); | ||
callback(null, { message: 'Message sent successfully' }); | ||
message = "Message accepted!"; | ||
} catch (error) { | ||
console.log(error); | ||
message = error.message; | ||
statusCode = 500; | ||
} | ||
|
||
return { | ||
statusCode, | ||
body: JSON.stringify({ | ||
message, | ||
}), | ||
}; | ||
}; | ||
|
||
// The function is triggered by the SQS queue. | ||
// A function is triggered whenever there is a new message in the queue. | ||
// When the function is triggered, it sends an email to '[email protected]' | ||
const consumer = async (event) => { | ||
for (const record of event.Records) { | ||
const params = { | ||
Destination: { | ||
ToAddresses: ["[email protected]"] // Email address/addresses that you want to send your email | ||
}, | ||
Message: { | ||
Body: { | ||
Text: { | ||
Charset: "UTF-8", | ||
Data: `Hey there! Here is your generated transcribed file:\n${record.body}` | ||
} | ||
}, | ||
Subject: { | ||
Charset: "UTF-8", | ||
Data: "Test Email - JOB COMPLETED SUCCESSFULLY" | ||
} | ||
}, | ||
Source: "[email protected]" // Sender email address | ||
}; | ||
|
||
try { | ||
// Send email to recipient | ||
const result = await ses.send(new SendEmailCommand(params)); | ||
console.log("Email sent successfully: ", result); | ||
} catch (error) { | ||
console.error("Error sending email: ", error); | ||
} | ||
} | ||
}; | ||
|
||
export { producer, consumer }; |
Oops, something went wrong.