Azure 函数:如何在将文件添加到 blob 容器时触发 PS 脚本以调用 R 脚本处理
Azure function: How to trigger PS script on file addition to blob container to invoke R script processing
我希望创建一个 Azure 函数,它从 Blob 存储中获取文件并将其发送到 R 脚本。
我按照 this 通过添加站点扩展来启用 R 脚本,在最初的几次小插曲之后终于可以使用了。
现在我遇到的问题是:
1. 当新文件上传到 blob 存储时如何触发函数,因为这使用 Powershell 方法。
cd D:\home\site\wwwroot\MyFunctionName
D:\home\R-3.3.3\bin\x64\Rscript.exe script.r 2>&1
2. 如何从 R 脚本访问 Azure blob 文件进行处理。
有没有人有可以分享的简单示例。假设只需在 R 中打开文件并在文件中打印总计行。
问候
基兰
由于 Azure 函数尚未支持 Powershell 的 blob 触发器,您可以考虑使用 BlobTrigger - C#。一个C#函数,每当一个blob被添加到指定的容器中时,就会运行,然后你可以将这个blob文件保存到本地,然后调用R脚本打开并读取文件。
这里有一个 C# 函数示例供您参考。
public static void Run(Stream myBlob, string name, string ext, TraceWriter log)
{
string basePath = Environment.ExpandEnvironmentVariables(@"%home%\site\wwwroot\BlobTriggerCSharp1");
// save to current dir
string filePath = Path.Combine(basePath, $"{Guid.NewGuid().ToString()}.{ext}");
using (FileStream fs = new FileStream(filePath, FileMode.Create))
{
myBlob.CopyTo(fs);
}
// run R script
var process = new System.Diagnostics.Process {
StartInfo = new System.Diagnostics.ProcessStartInfo {
FileName = "D:/home/R-3.3.3/bin/x64/Rscript.exe",
Arguments = basePath + "/script.r " + filePath,
UseShellExecute = false,
RedirectStandardOutput = true,
CreateNoWindow = true
}
};
process.Start();
string output = process.StandardOutput.ReadToEnd();
process.WaitForExit();
log.Info(output);
}
function.json
{
"bindings": [
{
"name": "myBlob",
"type": "blobTrigger",
"direction": "in",
"path": "mycontainer/{name}.{ext}",
"connection": "mystorage_STORAGE"
}
],
"disabled": false
}
如果您的 blob 文件是 TXT 文件,您可以在 R 中使用 read.table()
阅读它。
args = commandArgs(trailingOnly=TRUE)
df = read.table(args[1], header=TRUE)
print(df)
我希望创建一个 Azure 函数,它从 Blob 存储中获取文件并将其发送到 R 脚本。
我按照 this 通过添加站点扩展来启用 R 脚本,在最初的几次小插曲之后终于可以使用了。
现在我遇到的问题是:
1. 当新文件上传到 blob 存储时如何触发函数,因为这使用 Powershell 方法。
cd D:\home\site\wwwroot\MyFunctionName
D:\home\R-3.3.3\bin\x64\Rscript.exe script.r 2>&1
2. 如何从 R 脚本访问 Azure blob 文件进行处理。
有没有人有可以分享的简单示例。假设只需在 R 中打开文件并在文件中打印总计行。
问候 基兰
由于 Azure 函数尚未支持 Powershell 的 blob 触发器,您可以考虑使用 BlobTrigger - C#。一个C#函数,每当一个blob被添加到指定的容器中时,就会运行,然后你可以将这个blob文件保存到本地,然后调用R脚本打开并读取文件。
这里有一个 C# 函数示例供您参考。
public static void Run(Stream myBlob, string name, string ext, TraceWriter log)
{
string basePath = Environment.ExpandEnvironmentVariables(@"%home%\site\wwwroot\BlobTriggerCSharp1");
// save to current dir
string filePath = Path.Combine(basePath, $"{Guid.NewGuid().ToString()}.{ext}");
using (FileStream fs = new FileStream(filePath, FileMode.Create))
{
myBlob.CopyTo(fs);
}
// run R script
var process = new System.Diagnostics.Process {
StartInfo = new System.Diagnostics.ProcessStartInfo {
FileName = "D:/home/R-3.3.3/bin/x64/Rscript.exe",
Arguments = basePath + "/script.r " + filePath,
UseShellExecute = false,
RedirectStandardOutput = true,
CreateNoWindow = true
}
};
process.Start();
string output = process.StandardOutput.ReadToEnd();
process.WaitForExit();
log.Info(output);
}
function.json
{
"bindings": [
{
"name": "myBlob",
"type": "blobTrigger",
"direction": "in",
"path": "mycontainer/{name}.{ext}",
"connection": "mystorage_STORAGE"
}
],
"disabled": false
}
如果您的 blob 文件是 TXT 文件,您可以在 R 中使用 read.table()
阅读它。
args = commandArgs(trailingOnly=TRUE)
df = read.table(args[1], header=TRUE)
print(df)