admin管理员组文章数量:1304127
I am creating a demo app using azure static web app and testing it Live, the app works locally, but there are some challenges testing it with static web app Live (HTTPS).
I am trying to upload a audio file to azure function v4 (javascript & typescript) to a temporary directory so it can be uploaded to OpenAi.
I am getting an error "illegal operation on a directory, open 'C:\local\Temp\test.webm'".
Here is the log from the error in Azure Function app.
I have read the following resource to get information about uploading to a temp directory but it doenst work as expected.
I hope to get a clear understanding to upload a audio file to a temp directory in Azure function V4 (NOT V3)
kind regards
code:
import { createReadStream, createWriteStream, unlinkSync, existsSync } from "fs";
import { readFile, stat, mkdir, unlink } from "fs/promises";
import { Writable } from 'stream';
import { join } from "path";
import { tmpdir } from "os";
const path = tmpdir();
const saveTo = join(path, "audio.webm");
async function writeAudioFile(request: HttpRequest) {
try {
console.log("testing function app");
console.log({path: saveTo});
const bodyPipe = request.body;
try {
// This is for checking the directory is exist
console.log("checking ");
// await stat(path);
await stat(path);
} catch (e: any) {
if (e.code === "ENOENT") {
console.log({error_while_checking: e});
// If the directory doesn't exist (ENOENT : Error No Entry), create one
await mkdir(path, { recursive: true });
} else {
console.error(
"Error while trying to create directory when uploading a file\n",
e
);
return "Something went wrong.";
}
}
const busboyHeader: IncomingHttpHeaders = readHeader(request, 'content-type');
const bb: busboy.Busboy = busboy.default({ headers: busboyHeader });
console.log({temp_directory: saveTo});
const writeStream = createWriteStream(saveTo);
bb.on('file', (
fieldname: string,
file: NodeJS.ReadableStream,
filename: string,
encoding: string,
mimetype: string) => {
if(typeof filename === 'object' ) console.log({filename: filename});
file.on("data", (chunk) => {
if(typeof chunk === 'object') writeStream.write(chunk);
});
});
bb.on('close', () => {
console.log("end streamin audio file");
writeStream.end();
});
if (bodyPipe && typeof bodyPipe === "object") {
return await bodyPipe.pipeTo(Writable.toWeb(bb));
} else {
new Error("bodyPipe is null");
}
} catch (e){
console.error("Error while piping a file\n", e);
return new HttpResponse({body: JSON.stringify({"error": "Something went wrong while piping audio file."})});
}
}
export async function voiceTranscriptTrigger(request: HttpRequest, context: InvocationContext): Promise<HttpResponseInit> {
try{
writeAudioFile(request);
const data = readFile(saveTo);
const length = (await data).byteLength;
const fileExist = existsSync(saveTo);
// console.log('Success audio parsing:', length);
if(typeof data === 'object' && fileExist && length ){
// console.log({god_data: data});
const client = getClient();
const result = await client.audio.transcriptions.create({
model: "", // ssm-siri-assistent-35-turbo / gpt-3.5-turbo-1106
file: createReadStream(saveTo),
});
unlinkSync(saveTo);
// console.log({final: result});
const test: HttpResponseInit = {
status: 200,
body : JSON.stringify({transcript : result.text })
};
return test;
// return new HttpResponse(test);
} else {
throw {body: "Failed reading audio data"};
}
} catch (e) {
console.error("Error while trying to upload a file\n", e);
const test: HttpResponseInit = {
status: 500,
body: JSON.stringify({"error": e})
};
return test;
// return new HttpResponse({body: JSON.stringify({"error": "Something went wrong."})});
}
};
== Azure functio app Log failed uploading audio file ==
After "end streamin audio file" it shows if. there is a file found and what the length/size if of the file. Currently for some reason its always zero, I dont have this issue when testing it locally
2025-02-04T11:39:01Z [Information] ==> before checking request boundray: <==
2025-02-04T11:39:01Z [Information] testing function app
2025-02-04T11:39:01Z [Information] { path: 'C:\\local\\Temp\\audio.webm' }
2025-02-04T11:39:01Z [Information] upload path exist, and ready for upload
2025-02-04T11:39:01Z [Information] { contentType: [Function: values] }
2025-02-04T11:39:01Z [Information] {
2025-02-04T11:39:01Z [Information] new_header: {
2025-02-04T11:39:01Z [Information] 'content-type': 'multipart/form-data; boundary=----WebKitFormBoundaryDN26QARY4W5gjHRl'
2025-02-04T11:39:01Z [Information] }
2025-02-04T11:39:01Z [Information] }
2025-02-04T11:39:01Z [Information] { temp_directory: 'C:\\local\\Temp\\audio.webm' }
2025-02-04T11:39:01Z [Information] {
2025-02-04T11:39:01Z [Information] filename: { filename: 'audio.webm', encoding: '7bit', mimeType: 'audio/webm' }
2025-02-04T11:39:01Z [Information] }
2025-02-04T11:39:01Z [Information] end streamin audio file
2025-02-04T11:39:01Z [Information] Success audio parsing: 0
2025-02-04T11:39:01Z [Information] {
2025-02-04T11:39:01Z [Information] fileExist: true,
2025-02-04T11:39:01Z [Information] got_data: Promise { <Buffer > },
2025-02-04T11:39:01Z [Information] audio_obj: true,
2025-02-04T11:39:01Z [Information] length: 0
2025-02-04T11:39:01Z [Information] }
2025-02-04T11:39:01Z [Error] Error while trying to upload a file
2025-02-04T11:39:01Z [Error] { body: 'Failed reading audio data' }
2025-02-04T11:39:01Z [Information] Executed 'Functions.voiceTranscriptTrigger' (Succeeded, Id=1f4e545d-c122-4615-909d-3d3208663901, Duration=170ms)
2025-02-04T11:39:08Z [Verbose] AuthenticationScheme: ArmToken was not authenticated.
2025-02-04T11:39:08Z [Verbose] AuthenticationScheme: WebJobsAuthLevel was not authenticated.
2025-02-04T11:39:08Z [Verbose] Successfully validated the token.
2025-02-04T11:39:08Z [Verbose] AuthenticationScheme: Bearer was successfully authenticated.
2025-02-04T11:39:08Z [Verbose] Authorization was successful.
2025-02-04T11:39:08Z [Verbose] Authorization was successful.
本文标签:
版权声明:本文标题:node.js - Getting a error while creating a temp directory for a file in Azure function app v4, 'illegal operation on a d 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741782032a2397366.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论