Internal server error om Azure when writing file from buffer to filesystem

断了今生、忘了曾经 提交于 2019-12-13 03:45:12

问题


Context

I am working on a Proof of Concept for an accounting bot. Part of the solution is the processing of receipts. User makes picture of receipt, bot asks some questions about it and stores it in the accounting solution.

Approach

I am using the BotFramework nodejs example 15.handling attachments that loads the attachment into an arraybuffer and stores it on the local filesystem. Ready to be picked up and send to the accounting software's api.

async function handleReceipts(attachments) {
    const attachment = attachments[0];
    const url = attachment.contentUrl;
    const localFileName = path.join(__dirname, attachment.name);
    try {
     const response = await axios.get(url, { responseType: 'arraybuffer' });
        if (response.headers['content-type'] === 'application/json') {
            response.data = JSON.parse(response.data, (key, value) => {
                return value && value.type === 'Buffer' ? Buffer.from(value.data) : value;
            });
        }
         fs.writeFile(localFileName, response.data, (fsError) => {
            if (fsError) {
                throw fsError;
            }
        });
    } catch (error) {
        console.error(error);
        return undefined;
    }
    return (`success`);
}

Running locally it all works like a charm (also thanks to mdrichardson - MSFT). Stored on Azure, I get

There was an error sending this message to your bot: HTTP status code InternalServerError

I narrowed the problem down to the second part of the code. The part that write to the local filesystem (fs.writefile). Small files and big files result in the same error on Azure.fs.writefile seams unable to find the file

What is happpening according to stream logs:

Attachment uploaded by user is saved on Azure

{ contentType: 'image/png',contentUrl: 'https://webchat.botframework.com/attachments//0000004/0/25753007.png?t=< a very long string>',name: 'fromClient::25753007.png' }

localFilename (the destination of the attachment) resolves into

localFileName: D:\home\site\wwwroot\dialogs\fromClient::25753007.png

Axios loads the attachment into an arraybuffer. Its response:

response.headers.content-type: image/png

This is interesting because locally it is 'application/octet-stream'

fs throws an error:

fsError: Error: ENOENT: no such file or directory, open 'D:\home\site\wwwroot\dialogs\fromClient::25753007.png

Some assistance really appreciated.


回答1:


Removing ::fromClient prefix from attachment.name solved it. As @Sandeep mentioned in the comments, the special characters where probably the issue. Not sure what its purpose is. Will mention it in the Botframework sample library github repository. [update] team will fix this. Was caused by directline service.



来源:https://stackoverflow.com/questions/58306146/internal-server-error-om-azure-when-writing-file-from-buffer-to-filesystem

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!