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

标签 python linux azure azure-functions

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

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

Some of the python docs on Azure证明可以有 bin/根文件夹作为函数包的一部分。将文件放在那里然后调用 subprocess.run()就失败了。它显然找不到可执行文件。

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

我错过了什么?

最佳答案

我按照官方教程 Create your first Python function in Azure (preview) 创建 HttpTrigger Python的函数,并尝试了不同的方法来制作ffmpeg在 Azure Functions 中工作,那么它也适合我。

以上是我的操作步骤,如下,希望对您有所帮助。

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

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

    # 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. 然后我输入 ffmpeg AMD64 静态二进制文件从 https://johnvansickle.com/ffmpeg/ 下载进入我的MyFunctionProj并更改下面的代码以检查文件路径,并命令 func azure functionapp publish <my app name>发布到 Azure。

    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']在浏览器中与我的MyFunctionProj中的相同

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

    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 ,然后考虑将其复制到命令 del out.mp4 .

  5. 尝试使其适用于 Azure Function 上的 Linux 环境,我将命令更改为 ./ffmpeg -i in.mp4 out.mp4rm out.mp4 。但它在 Azure Function 上不起作用。可能是缺少ffmpeg的执行权限导致的从 Windows 上传时的 linux 二进制文件。所以我通过命令 ls -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`
    

注意:我认为ffmpeg的执行权限问题当你在 Linux 中开发时就会发生这种情况。还有in.mp4文件来自https://github.com/kkroening/ffmpeg-python/blob/master/examples/in.mp4 。我曾尝试使用 ffmpeg-python包来实现您的需求,但它似乎不适用于本地和Azure。

关于python - 如何在 Azure Functions 中添加和执行二进制文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55132619/

相关文章:

python - 如何监控 networkx 图创建的状态?

android - Fedora 25 : libgcc_s. so.1 未找到

linux - 复制目录结构忽略一些文件

python - MQTT 客户端不断与 Bluemix IOT Foundation 断开连接

python - 反转 pandas 中的 get_dummies 编码

java - JAR 文件在 Linux 上按预期工作,在 Windows 上抛出异常

azure - 如何将 Kusto 查询输出解析为自动化脚本(Powershell 或 Python)

azure - 如何使用 Terraform 更新 Azure 订阅标签?

azure - ARM 模板和针对 AAD 的强制标签值验证

python - np.array.sum(-1) 中的 sum(-1) 是什么意思?