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));
});