简体   繁体   中英

Writing pandas dataframe as xlsx file to an azure blob storage without creating a local file

I have the following solution using a local file, but I would like to skip that. Is there a possibility?

blob_service_client = BlobServiceClient(account_url = url, credential = token)
write_blob_client = blob_service_client.get_blob_client(container=container_name, blob=blob_name)
filename = "example.xlsx"
df.to_excel(filename ,index = False)
with open(filename, "rb") as df:
write_blob_client.upload_blob(df, overwrite = True)

Instead of the last three rows I have tried

teststream = BytesIO(df.to_records(index = False).tostring())
write_blob_client.upload_blob(teststream, overwrite = True)

This writes an excel file to the blob storage, but when trying to open it I get an error that the file extention doesn't match the format of the file.

You have to get the value from the BytesIO object. The following code works for me.

writer = io.BytesIO()
df.to_excel(writer)
blob_client.upload_blob(writer.getvalue(), overwrite = True)

Of course you dont need to create a local file.

You just need to put a stream into upload_blob. It seems like you are using the example code provide by offcial. You dont need to use open(filename, "rb") as df , just convert to stream is ok.

This is my code:

import logging

import azure.functions as func
import os, uuid, io
from azure.storage.blob import BlobServiceClient, BlobClient, ContainerClient

def main(req: func.HttpRequest) -> func.HttpResponse:
    logging.info('Python HTTP trigger function processed a request.')
    connect_str = os.getenv('str')
    blob_service_client = BlobServiceClient.from_connection_string(connect_str)
    container_name = "video"
    container_client = blob_service_client.get_container_client(container_name)
    filename = "test.xlsx"
    blob_client = blob_service_client.get_blob_client(container_name, filename)
    teststream = io.BytesIO(req.get_body())
    blob_client.upload_blob(teststream, overwrite = True)
    name = req.params.get('name')

    return func.HttpResponse("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")

The above code convert the body of request from byte to stream and upload to azure blob storage.

Let me know whether you can work it out, any doubt please let me know.:)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM