Azure Functions: Nodejs, What are restrictions / limitations when using file system?

时间:2016-08-30 04:43:12

标签: node.js azure azure-functions

I have not been able to get an azure function working that uses the node file system module.

I created a brand new function app with most basic HTTP trigger function and included the 'fs' module:

var fs = require('fs');

module.exports = function (context, req, res) {
    context.log('function triggered');
    context.log(req);

    context.done();
}

This works fine. I see the full request in live streaming logs, and in the function invocation list.

However, as soon as I add the code which actually uses the file system, it seems to crash the azure function. It neither completes or throws the error. It also doesn't seem to show up in the azure function invocations list which is scary since this is loss of failure information and I might think my service was running fine when there were actually crashes.

var fs = require('fs');

module.exports = function (context, req, res) {
    context.log('function triggered');
    context.log(req);

    fs.writeFile('message.txt', 'Hello Node.js', (err) => {
        if (err) throw err;
        console.log('It\'s saved!');
        context.done();
    });
}

The fs.writeFile code taken directly from the node.js website: https://nodejs.org/dist/latest-v4.x/docs/api/fs.html#fs_fs_writefile_file_data_options_callback

I added the context.done() in the callback, but that snippet should work without issue on normal development environment.

This brings up the questions:

  • Is it possible to use the file system when using Azure Functions?
  • If so, what are the restrictions?
  • If no restrictions, are developers required to keep track and perform cleanup or is this taken care of by some sandboxing?

From my understanding even though this is considered server-less computing there is still a VM / Azure Website App Service underneath which has a file system. I can use the Kudu console and navigate around and see all the files in /wwwroot and the /home/functions/secrets files.

Imagine a scenario where an azure function is written to write a file with unique name and not perform cleanup it would eventually take up all the disk space on the host VM and degrade performance. This could happen accidentally by a developer and possibly go unnoticed until it's too late.

This makes me wonder if it is by design not to use the file system, or if my function is just written wrong?

1 个答案:

答案 0 :(得分:8)

是的,您可以使用文件系统,但有here所述的一些限制。该页面描述了一些您可以访问的目录,如 D:\ HOME D:\ LOCAL \ TEMP 。我已经修改了下面的代码来写入临时目录并且它有效:

var fs = require('fs');

 module.exports = function (context, input) {
    fs.writeFile('D:/local/Temp/message.txt', input, (err) => {
    if (err) {
        context.log(err);
        throw err;
    }
    context.log('It\'s saved!');
        context.done();
    });
}

您的初始代码失败了,因为它试图写入 D:\ Windows \ system32 ,这是不允许的。