无法使用python脚本从azure容器下载blob csv文件[错误30]只读文件系统

5anewei6  于 2023-07-31  发布在  Python
关注(0)|答案(2)|浏览(86)

我无法从存储容器下载csv文件,这里是我的代码,因为我不断得到一个错误。:

import datetime
    import logging
    from azure.storage.blob import BlobServiceClient
    import azure.functions as func
    import os

    def main(mytimer: func.TimerRequest) -> None:
        utc_timestamp = datetime.datetime.utcnow().replace(
            tzinfo=datetime.timezone.utc).isoformat()

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

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

        # Azure Blob Storage credentials
        account_name = ''
        account_key = ''
        container_name = ''
        connection_string = f""

        # Connect to Blob Storage
        blob_service_client = BlobServiceClient.from_connection_string(connection_string)
        container_client = blob_service_client.get_container_client(container_name)
        blob_client = blob_service_client.get_blob_client(container=container_name, blob="blob.csv")
        filepath = "/home/site/wwwroot/TimerTriggerTEST"
        with open(file=os.path.join(filepath, 'filename'), mode="wb") as sample_blob:
            download_stream = blob_client.download_blob()
            sample_blob.write(download_stream.readall())

字符串
下面是我得到的日志截图:
Logs
我一直得到这个错误,即使在尝试了很多事情,
我尝试在应用程序设置FUNCTION_APP_EDIT_MODE中添加值readwrite
我检查了关于如何下载blob的文档,它没有帮助,
我也看到只有"/tmp dir "在一些论坛上是可写的,但是我应该如何使用和传输文件数据呢?因为肯定我可以下载到tmp目录,但我不能复制粘贴数据到csv文件,我在我的主目录,因为我也得到这个错误:"结果:失败异常:错误:[错误30]只读文件系统:'/home/site/wwwroot/TimerTrigger1/outbound. csv '堆栈:文件"/azure-functions-host/workers/python/3.10/LINUX/X64/azure_functions_worker/www.example.com ",第479行,in_handle__invocation_request call_result = await self。_loop。run_in_executor(文件"/usr/local/lib/python3.10/concurrent/futures/thread。py”,第58行,在run result = self中。fn(* self. args,**self. kwargs)文件"/azure-functions-host/workers/python/3.10/LINUX/X64/azure_functions_worker/www.example.com",第752行,in_run_sync_func return ExtensionManager。获取同步调用 Package 器(context,File "/azure-functions-host/workers/python/3.10/LINUX/X64/azure_functions_worker/dispatcher.py
如果可能的话,我需要一个解决方案来做到这一点

rta7y2nd

rta7y2nd1#

经过研究和故障排除,我认为我能做的最好的就是使用blob容器来存储信息
基本上的想法是你处理一切你需要在tmp文件,然后要么上传或下载到存储帐户,这使得它更繁琐的代码,但这是唯一的事情,我发现工程

monwx1rj

monwx1rj2#

谢谢@zixer,下面是我的观察。
我在Azure Portal中创建了存储帐户和容器。
x1c 0d1x的数据
我在容器中上传了一个文件。



我在你的代码中做了一些修改,下面的代码工作正常。

import os
import tempfile
from azure.storage.blob import BlobServiceClient
import azure.functions as func

def main(mytimer: func.TimerRequest) -> None:

    account_name = 'welcome092'
    account_key = '**your key**'
    container_name = 'well'
    connection_string = "**your connection string**"

   
    blob_service_client = BlobServiceClient.from_connection_string(connection_string)
    container_client = blob_service_client.get_container_client(container_name)
    blob_client = blob_service_client.get_blob_client(container=container_name, blob="blob.csv")

  
    with tempfile.NamedTemporaryFile(delete=False) as tmp_file:
        tmp_file_path = tmp_file.name

        with open(tmp_file_path, 'wb') as download_file:
            download_stream = blob_client.download_blob()
            download_file.write(download_stream.readall())

    destination_file_path = "C:/Users/v-ppesala/Downloads/pavanblob.csv"

    os.rename(tmp_file_path, destination_file_path)

字符串
以上代码运行成功,文件下载到指定路径。

输出:

相关问题