如何在Azure Functions中添加和执行二进制文件?

时间:2019-03-13 00:25:58

标签: python linux azure azure-functions

我需要从运行python / linux的Azure函数中运行linux二进制文件(在本示例中为ffmpeg

我还没有找到如何打包或执行它的方法。

Some of the python docs on Azure显示作为功能包的一部分,可以有一个bin/根文件夹。将文件放在此处,然后对其调用subprocess.run()失败。显然找不到可执行文件。

我尝试将文件移动到根目录,或者尝试使用完整路径查找它(我尝试过/home/site/www及其变体)。还是没有运气。

我想念什么?

2 个答案:

答案 0 :(得分:1)

我按照官方教程Create your first Python function in Azure (preview)为Python创建了HttpTrigger函数,并尝试了各种方法使ffmpeg在Azure Functions中工作,然后为我工作。

这是我在上面执行此操作的步骤,如下所示,希望对您有所帮助。

  1. 按照Azure Funtions for Python官方教程在本地Windows计算机上安装Azure Functions Core Tools,以创建名为MyFunctionProj的项目和名为HttpTrigger的函数。

  2. 在通过部署上传ffmpeg之前,我通过使用以下代码更改正式的示例代码来检查Azure上我的Azure Functions实例的OS平台体系结构。

    # add these codes
    import platform, os
    .....
    
    def main(req: func.HttpRequest) -> func.HttpResponse:
        if name:
            return func.HttpResponse(f"Hello {name}! {platform.architecture()}")
    

    搜索结果为Hello peter-pan! ('64bit', '')

  3. 然后,我将从https://johnvansickle.com/ffmpeg/下载的ffmpeg AMD64静态二进制文件放入我的MyFunctionProj中,并在下面的代码中进行更改以检查文件路径并命令{{1 }}发布到Azure。

    func azure functionapp publish <my app name>

    其结果与我的def main(req: func.HttpRequest) -> func.HttpResponse: if name: return func.HttpResponse(f"Hello {name}! {platform.architecture()} {os.listdir()} {os.listdir('HttpTrigger')}") 中的Hello peter-pan! ('64bit', '') ['in.mp4', 'ffmpeg', 'host.json', 'requirements.txt', 'ffmpeg.exe', '.python_packages', 'HttpTrigger'] ['in.mp4', '__pycache__', 'sample.dat', 'host.json', 'function.json', '__init__.py']相同

  4. 我发现MyFunctionProj文件夹中的所有内容都将上传到Azure并调用MyFunctionProj以显示os.listdir()的文件列表,因此Python中的当前路径与MyFunctionProj在本地。然后,我尝试通过下面的代码在本地Windows环境中调用MyFunctionProje

    ffmpeg

    通过命令def main(req: func.HttpRequest) -> func.HttpResponse: if name: return func.HttpResponse(f"Hello {name}! {platform.architecture()} {os.listdir()} {os.listdir('HttpTrigger')} {os.path.exists('in.mp4')} {os.popen('ffmpeg.exe -i in.mp4 out.mp4').read()} {os.path.exists('out.mp4')} {os.popen('del out.mp4').read()}") 输出文件out.mp4,然后考虑将其复制到命令ffmpeg.exe -i in.mp4 out.mp4

  5. 尝试使它适用于Azure Function上的Linux环境,我用del out.mp4./ffmpeg -i in.mp4 out.mp4更改了命令。但是它不适用于Azure Function。从Windows上载时,可能缺少rm out.mp4 linux二进制文件的执行权限。因此,在调用它之前,我先通过命令ffmpegls -l ffmpeg检查了我的猜测。

    chmod u+x ffmpeg

    现在可以使用,结果如下所示。

    def main(req: func.HttpRequest) -> func.HttpResponse:
        if name:
            return func.HttpResponse(f"Hello {name}! {platform.architecture()} {os.listdir()}  {os.listdir('HttpTrigger')} {os.popen('ls -l ffmpeg').read()} {os.popen('chmod u+x ffmpeg').read()} {os.popen('ls -l ffmpeg').read()} {os.path.exists('in.mp4')} {os.popen('./ffmpeg -i in.mp4 out.mp4').read()} {os.path.exists('out.mp4')} {os.popen('rm out.mp4').read()}")
    

注意:我认为Hello peter-pan! // Offical sample output ('64bit', '') // the Linux platform architecture of Azure Functions for Python ['in.mp4', 'ffmpeg', 'host.json', 'requirements.txt', 'ffmpeg.exe', '.python_packages', 'HttpTrigger'] // file list of MyFunctionProj ['in.mp4', '__pycache__', 'sample.dat', 'host.json', 'function.json', '__init__.py'] // file list of HttpTrigger -r--r--r-- 1 root root 69563752 Mar 13 2019 ffmpeg // before chmod u+x -rwxr--r-- 1 root root 69563752 Mar 13 2019 ffmpeg // after chmod u+x True // in.mp4 exists True // out.mp4 exists before delete it using `rm` 的执行许可问题将在您使用Linux开发时发生。 ffmpeg文件来自https://github.com/kkroening/ffmpeg-python/blob/master/examples/in.mp4。我曾尝试使用in.mp4包来实现您的需求,但似乎不适用于本地和Azure。

答案 1 :(得分:0)

您可以在apt-get install ffmpeg上的Kudu控制台中运行诸如https://<YOUR-APP>.scm.azurewebsites.net之类的命令。

但是,如果您部署或扩展应用程序,则环境将被重置。另一个解决方案是使用docker部署,以便您可以指定要添加到服务器上的二进制文件。