Skip to content

Commit

Permalink
Merge pull request #51 from sudeeppr1998/audio-upload-to-s3
Browse files Browse the repository at this point in the history
Audio upload to s3 bucket and Add url to telemetry logs
  • Loading branch information
gouravmore authored Aug 16, 2023
2 parents 5e6d992 + fdad625 commit 7be1116
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 8 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"private": true,
"homepage": ".",
"dependencies": {
"@aws-sdk/client-s3": "^3.388.0",
"@chakra-ui/icons": "^2.0.17",
"@chakra-ui/react": "^2.4.9",
"@emotion/react": "^11.0.0",
Expand Down Expand Up @@ -57,4 +58,4 @@
"last 1 safari version"
]
}
}
}
28 changes: 26 additions & 2 deletions src/components/AudioRecorderTamil/AudioRecorderTamil.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import mic from '../../assests/Images/mic.png';
import { showLoading, stopLoading } from '../../utils/Helper/SpinnerHandle';
import { response,interact } from '../../services/telementryService';
import { replaceAll, compareArrays } from '../../utils/helper';
import { PutObjectCommand } from "@aws-sdk/client-s3";
import S3Client from '../../config/awsS3';

//webkitURL is deprecated but nevertheless
URL = window.URL || window.webkitURL;
Expand Down Expand Up @@ -149,7 +151,7 @@ function Mic({

fetch(ASR_REST_URL, requestOptions)
.then(response => response.text())
.then(result => {
.then( async (result) => {
clearTimeout(waitAlert);
const responseEndTime = new Date().getTime();
const responseDuration = Math.round(
Expand Down Expand Up @@ -216,9 +218,31 @@ function Mic({
}
let word_result = (result_per_words == 100) ? "correct" : "incorrect";

if (process.env.REACT_APP_CAPTURE_AUDIO === 'true') {

var audioFileName = `${process.env.REACT_APP_AWS_s3_BUCKET_FOLDER_NAME}/${localStorage.getItem('contentSessionId')}-${Date.now()}.wav`;

const command = new PutObjectCommand({
Bucket: process.env.REACT_APP_AWS_s3_BUCKET_NAME,
Key: audioFileName,
Body: Uint8Array.from(window.atob(asrInput), (c) => c.charCodeAt(0)),
ContentType: 'audio/wav'
});


try {
const response = await S3Client.send(command);
console.log(`${process.env.REACT_APP_AWS_s3_BUCKET_URL}/${audioFileName}`);
} catch (err) {
console.error(err);
}

}


response(
{ // Required
"target": localStorage.getItem('contentText'), // Required. Target of the response
"target": process.env.REACT_APP_CAPTURE_AUDIO === 'true' ? `${process.env.REACT_APP_AWS_s3_BUCKET_URL}/${audioFileName}` : '', // Required. Target of the response
//"qid": "", // Required. Unique assessment/question id
"type": "SPEAK", // Required. Type of response. CHOOSE, DRAG, SELECT, MATCH, INPUT, SPEAK, WRITE
"values": [
Expand Down
28 changes: 26 additions & 2 deletions src/components/VoiceCompair/VoiceCompair.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import { response, interact } from '../../services/telementryService';
import { showLoading, stopLoading } from '../../utils/Helper/SpinnerHandle';
import { replaceAll, compareArrays } from '../../utils/helper';

import { PutObjectCommand } from "@aws-sdk/client-s3";
import S3Client from '../../config/awsS3';

const VoiceCompair = props => {
const [lang_code, set_lang_code] = useState(
localStorage.getItem('apphomelang')
Expand Down Expand Up @@ -146,7 +149,7 @@ const VoiceCompair = props => {
const responseStartTime = new Date().getTime();
fetch(apiURL, requestOptions)
.then(response => response.text())
.then(result => {
.then(async result => {
clearTimeout(waitAlert);
const responseEndTime = new Date().getTime();
const responseDuration = Math.round(
Expand Down Expand Up @@ -211,8 +214,29 @@ const VoiceCompair = props => {

let word_result = (result_per_words == 100) ? "correct" : "incorrect";

if (process.env.REACT_APP_CAPTURE_AUDIO === 'true') {

var audioFileName = `${process.env.REACT_APP_AWS_s3_BUCKET_FOLDER_NAME}/${localStorage.getItem('contentSessionId')}-${Date.now()}.wav`;

const command = new PutObjectCommand({
Bucket: process.env.REACT_APP_AWS_s3_BUCKET_NAME,
Key: audioFileName,
Body: Uint8Array.from(window.atob(base64Data), (c) => c.charCodeAt(0)),
ContentType: 'audio/wav'
});


try {
const response = await S3Client.send(command);
console.log(response);
console.log(`${process.env.REACT_APP_AWS_s3_BUCKET_URL}/${audioFileName}`);
} catch (err) {
console.error(err);
}
}

response({ // Required
"target": localStorage.getItem('contentText'), // Required. Target of the response
"target": process.env.REACT_APP_CAPTURE_AUDIO === 'true' ? `${process.env.REACT_APP_AWS_s3_BUCKET_URL}/${audioFileName}` : '', // Required. Target of the response
//"qid": "", // Required. Unique assessment/question id
"type": "SPEAK", // Required. Type of response. CHOOSE, DRAG, SELECT, MATCH, INPUT, SPEAK, WRITE
"values": [
Expand Down
3 changes: 0 additions & 3 deletions src/config.js

This file was deleted.

10 changes: 10 additions & 0 deletions src/config/awsS3.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { S3Client } from "@aws-sdk/client-s3";

export default new S3Client(
{
region: process.env.REACT_APP_AWS_S3_REGION,
credentials:{
accessKeyId:process.env.REACT_APP_AWS_ACCESS_KEY_ID,
secretAccessKey:process.env.REACT_APP_AWS_SECRET_ACCESS_KEY
}
});

0 comments on commit 7be1116

Please sign in to comment.