[英]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]
的绑定,而f
是gzip.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.