简体   繁体   English

使用 Azure 函数将 gzip 文件存储在存储帐户中

[英]Store a gzip file in a storage account with Azure function

I'm trying to store in my storage account a gzip file following a dump of my database, with Azure function and python code.在我的数据库转储之后,我试图在我的存储帐户中存储一个 gzip 文件,其中包含 Azure 函数和 python 代码。

I tried to follow this documentation我试着按照这个文档

I have an error on this line (outputBlob.set(f)):我在这一行 (outputBlob.set(f)) 上有一个错误:

import datetime
import logging
import os
import gzip
import subprocess

import azure.functions as func

def main(mytimer: func.TimerRequest,outputBlob: func.Out[bytes]) -> None:

    utc_timestamp = datetime.datetime.utcnow().replace(tzinfo=datetime.timezone.utc).isoformat()

    # Get the setting named 'myAppSetting'
    my_app_setting_value = os.environ["LIVE_CONNECTIONSTRING"]
    logging.info(f'My app setting value:{my_app_setting_value}')

    cmd = "pg_dump " + my_app_setting_value + " | sed 's/LOCALE/LC_COLLATE/'"
    logging.info(f'commande : {cmd}')

    with gzip.open('backup.gz', "wb") as f:

        popen = subprocess.Popen(cmd,shell=True, stdout=subprocess.PIPE, universal_newlines=True)

        for stdout_line in iter(popen.stdout.readline, ""):
            f.write(stdout_line.encode("utf-8"))

        popen.stdout.close()
        popen.wait()

    logging.info(type(f))
    logging.info(f)
    logging.info(outputBlob)
    ---> outputBlob.set(f) <---

    if mytimer.past_due:
        logging.info('The timer is past due!')

    logging.info('Python timer trigger function ran at %s', utc_timestamp)

Logs:日志:

2022-12-01T22:46:53Z   [Information]   <class 'gzip.GzipFile'>
2022-12-01T22:46:53Z   [Information]   <gzip on 0x7fca9c5868b0>
2022-12-01T22:46:53Z   [Information]   <azure_functions_worker.bindings.out.Out object at 0x7fca9c586160>
2022-12-01T22:46:53Z   [Error]   Executed 'Functions.TimerTriggerDump' (Failed, Id=7c1664ad-5ab2-431a-9818-4f66473b3ceb, Duration=199ms)

Has anyone ever encountered this case?有没有人遇到过这种情况?

I tried to change the parameter type of my outputblob我试图更改我的 outputblob 的参数类型

Check if the below steps helps to fix the issue:检查以下步骤是否有助于解决问题:

  • The error you are seeing is occurring on the line outputBlob.set(f) , where outputBlob is a binding of type func.Out[bytes] and f is a file object returned by gzip.open .您看到的错误发生在outputBlob.set(f)行,其中outputBlob是类型func.Out[bytes]的绑定,而fgzip.open返回的文件对象。

  • To fix this error, you will need to read the contents of the gzip file and pass them as a bytes object to outputBlob.set .要修复此错误,您需要读取 gzip 文件的内容并将它们作为字节对象传递给outputBlob.set You can do this by calling f.read() on the file object and passing the resulting bytes object to outputBlob.set .您可以通过在文件对象上调用f.read()并将生成的字节对象传递给outputBlob.set来完成此操作。

Here is an example of how you can modify your code to fix the error:以下是如何修改代码以修复错误的示例:

    with gzip.open('backup.gz', "rb") as f:
     # Read the contents of the gzip file as bytes
      file_contents = f.read()
       # Set the contents of the gzip file to the output binding
      outputBlob.set(file_contents)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么 Python Azure function 应用程序不将单个文件从存储帐户中的目录复制到目录? - Why Python Azure function app not copying single file from directory to directory in Storage account? Json to excel from devops to storage account with help of azure function in python - Json to excel from devops to storage account with help of azure function in python 使用python将CSV文件上载到Microsoft Azure存储帐户 - Upload CSV file into Microsoft Azure storage account using python Azure 函数 blob 存储文件名 - Azure function blob storage file name 在上传到云存储之前 Gzip Python 中的文件 - Gzip a file in Python before uploading to Cloud Storage 如何使用 azure ZC1C425Z574E68385D1CAB1 编辑 azure blob 存储中的 *.csv 文件 - How to edit a *.csv file in the azure blob storage with an azure function? 从 Azure Blob 存储加载 Azure 函数中的 pickle 文件 - Load pickle file in Azure function from Azure Blob Storage Azure Function(消耗 Linux 计划)未使用系统身份访问 zip 部署的存储帐户 - Azure Function (Consumption Linux Plan) is not working with System Identity to access the storage account for zip deployment Azure function:System.InvalidOperationException:存储帐户连接字符串'不存在 - Azure function: System.InvalidOperationException: Storage account connection string 'does not exist Azure function 和 Azure Blob 存储 - Azure function and Azure Blob Storage
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM