Azure功能:Node.js,使用文件系统时有哪些限制/限制? [英] Azure Functions: Nodejs, What are restrictions / limitations when using file system?

查看:68
本文介绍了Azure功能:Node.js,使用文件系统时有哪些限制/限制?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法使用节点文件系统模块的azure函数正常工作.

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

我用最基本的HTTP触发功能创建了一个全新的功能应用程序,并包含了"fs"模块:

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.

但是,一旦我添加了实际使用文件系统的代码,似乎就会崩溃 azure函数.它既不会完成也不会抛出错误.它似乎也没有出现在azure函数调用列表中,这很可怕,因为这是失败信息的丢失,我可能认为当实际崩溃时我的服务运行良好.

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

直接从node.js网站获取的fs.writeFile代码: https://nodejs.org/dist/latest-v4 .x/docs/api/fs.html#fs_fs_writefile_file_data_options_callback

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

我在回调中添加了context.done(),但是该代码段在正常的开发环境中应该可以正常工作.

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

这带来了问题:

  • 使用Azure Functions时是否可以使用文件系统?
  • 如果是,限制是什么?
  • 如果没有限制,要求开发人员跟踪并执行 清理还是通过沙箱处理?
  • 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?

根据我的理解,即使这被认为是无服务器计算,但在其下方仍然有一个具有文件系统的VM/Azure网站应用服务. 我可以使用Kudu控制台浏览并浏览/wwwroot和/home/functions/secrets文件中的所有文件.

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.

想象一下这样一种情况:编写了一个Azure函数来写入具有唯一名称的文件并且不执行清除操作,这最终将占用主机VM上的所有磁盘空间并降低性能.开发人员可能会偶然发生这种情况,直到为时已晚才引起注意.

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?

推荐答案

是的,您可以使用文件系统,但有一些限制,如

Yes you can use the file system, with some restrictions as described here. That page describes some directories you can access like D:\HOME and D:\LOCAL\TEMP. I've modified your code below to write to the temp dir and it works:

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 .

Your initial code was failing because it was trying to write to D:\Windows\system32 which is not allowed.

这篇关于Azure功能:Node.js,使用文件系统时有哪些限制/限制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆