Skip to content

Instantly share code, notes, and snippets.

@Dosant
Last active September 29, 2024 10:05
Show Gist options
  • Save Dosant/beb67bf1cb0b719d180b7d34b9e46415 to your computer and use it in GitHub Desktop.
Save Dosant/beb67bf1cb0b719d180b7d34b9e46415 to your computer and use it in GitHub Desktop.
AWS S3 to OpenAI Whisper via Node.js stream

Current (2023-03-01) OpenAI Whisper API expects a file uploaded as part of multipart/form-data in POST request. I initially struggled to use this API inside Node.js with an audio file stored inside an AWS s3 bucket, so I decided to share this snippet:

The API usage example from the OpenAI docs:

curl --request POST \
  --url https://api.openai.com/v1/audio/transcriptions \
  --header 'Authorization: Bearer TOKEN' \
  --header 'Content-Type: multipart/form-data' \
  --form file=@/path/to/file/openai.mp3 \
  --form model=whisper-1

See the Node.js + AWS S3 example below:

import fetch from "node-fetch";
import AWS from "aws-sdk"; // v2
import fs from "fs";
import FormData from "form-data";
// Set the region and access keys for your AWS account
AWS.config.update({
region: 'eu-central-1',
accessKeyId: '***',
secretAccessKey: '***'
});
// Create an S3 client
const s3 = new AWS.S3();
// Set the bucket and file key
const bucketName = 'openai-sample';
const fileKey = 'path/to/file/openai.mp3';
// Set the parameters for the S3 getObject operation
const params = {
Bucket: bucketName,
Key: fileKey
};
// Get audio metadata to retrieve size and type
s3.headObject(params, function(err, data) {
if (err) throw err;
// Get read object stream
const s3Stream = s3.getObject(params)
.createReadStream();
// create form data to be send to whisper api
const formData = new FormData();
// append stream with a file
formData.append('file', s3Stream, {
contentType: data.ContentType,
knownLength: data.ContentLength,
filename: fileKey
});
formData.append('model', 'whisper-1');
fetch('https://api.openai.com/v1/audio/transcriptions', {
method: 'POST',
body: formData,
headers: {
'Authorization': 'Bearer TOKEN',
...formData.getHeaders()
}
}).then(res => res.json()).then(json => console.log(json.text));
});
import fetch from "node-fetch";
import {S3Client, GetObjectCommand, HeadObjectCommand} from '@aws-sdk/client-s3'; //v3
import FormData from "form-data";
/*
curl --request POST \
--url https://api.openai.com/v1/audio/transcriptions \
--header 'Authorization: Bearer TOKEN' \
--header 'Content-Type: multipart/form-data' \
--form file=@/path/to/file/openai.mp3 \
--form model=whisper-1
*/
// Set the region and access keys for your AWS account
const s3 = new S3Client({
region: 'eu-central-1',
credentials: {
accessKeyId: '***',
secretAccessKey: '***'
}
});
// Set the bucket and file key
const bucketName = 'openai-sample';
const fileKey = 'path/to/file/openai.mp3';
// Set the parameters for the S3 getObject operation
const params = {
Bucket: bucketName,
Key: fileKey
};
// Call the S3 getObject operation with the specified parameters
const getObjectOutput = await s3.send(new GetObjectCommand(params));
const formData = new FormData();
// getObjectResult.Body is a ReadableStream
formData.append('file', getObjectOutput.Body, {
contentType: getObjectOutput.ContentType,
knownLength: getObjectOutput.ContentLength,
filename: fileKey
});
formData.append('model', 'whisper-1');
const result = await fetch('https://api.openai.com/v1/audio/transcriptions', {
method: 'POST',
body: formData,
headers: {
'Authorization': 'Bearer TOKEN',
...formData.getHeaders()
}
}).then(res => res.json());
console.log(result);
@codeyourwayup
Copy link

good, thanks. Love it.

@avinashdv
Copy link

avinashdv commented Jun 20, 2023

I get an error at line 38 on index-aws@3.js
getObjectOutput.Body in my case is a Readable Stream as below:
image

The error I get is:
TypeError: Failed to execute 'append' on 'FormData': parameter 2 is not of type 'Blob'.

Stuck here for past 3 hours. Please help.

@ThibC96
Copy link

ThibC96 commented Mar 4, 2024

I get an error at line 38 on index-aws@3.js getObjectOutput.Body in my case is a Readable Stream as below: image

The error I get is: TypeError: Failed to execute 'append' on 'FormData': parameter 2 is not of type 'Blob'.

Stuck here for past 3 hours. Please help.

make sur to have the right import

import FormData from "form-data";

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment