简体   繁体   English

如何将 Excel 文件作为 Excel 文件上传到 Blob 存储

[英]How to uploade an Excel file to a Blob Storage as an Excel File

i have a process that's beening triggered by Blobs.我有一个由 Blob 触发的进程。 I'm trying to upload a dataframe to Azure blob storage and i can achieve this via this code.我正在尝试将 dataframe 上传到 Azure blob 存储,我可以通过此代码实现此目的。

async def main(myblob: func.InputStream,  outputblob: func.Out[str]) -> None:
(Some Code)
outputblob.set(Buch.to_string())

but the problem with that approach is that it uploads the DataFrames as a File and not as CSV or Excel file.但这种方法的问题在于它将数据帧作为文件上传,而不是作为 CSV 或 Excel 文件上传。 I also tried this Code but this time nothing was uploaded.我也尝试了此代码,但这次没有上传任何内容。

outputblob.set(Deb.to_excel("Deb.xlsx"))

Is there anyway that we can uploade as Excel files?无论如何,我们可以上传 Excel 文件吗?

the Solution is to upload it manually without using the Built-in function:解决方案是不使用内置 function 手动上传:

outputblob.set()

my code for uploading a CSV file to a container.我用于将 CSV 文件上传到容器的代码。 works for HTTP and Blob Trigger.适用于 HTTP 和 Blob 触发器。

def upload(_file,_connectionString,_containerName,_fileName):            
        blob_service_client = BlobServiceClient.from_connection_string(_connectionString)
        # Instantiate a new ContainerClient
        container_client = blob_service_client.get_container_client(_containerName)
        if not container_client.exists():
            # Create new Container in the service
            print("container does not exist. create it")
            container_client.create_container()
            properties = container_client.get_container_properties()
            # Instantiate a new BlobClient            
            blob_client = container_client.get_blob_client(_fileName)
            # upload data
            blob_client.upload_blob(_file, blob_type="BlockBlob")
        else:
            blob = BlobClient.from_connection_string(conn_str=_connectionString,
                                                    container_name=_containerName,
                                                    blob_name=_fileName) 
            if blob.exists():
                logging.info(f'{_fileName} exists')                    
            else:
                # Instantiate a new BlobClient            
                blob_client = container_client.get_blob_client(_fileName)
                # upload data
                blob_client.upload_blob(_file, blob_type="BlockBlob")

use it like This:像这样使用它:

upload(YourDataFrame.to_csv(index=False, encoding = "utf-8"),_connectionString,_containerName,_fileName)

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

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