Open an Azure StorageStreamDownloader without saving it as a file

后端 未结 2 889
孤独总比滥情好
孤独总比滥情好 2021-01-16 06:44

I need to download a PDF from a blob container in azure as a download stream (StorageStreamDownloader) and open it in both PDFPlumber and PDFminer. I developed all the requi

2条回答
  •  感动是毒
    2021-01-16 07:19

    download_blob() download the blob to a StorageStreamDownloader class, and in this class there is a download_to_stream, with this you will get the blob stream.

    from azure.storage.blob import BlobServiceClient, BlobClient, ContainerClient
    from io import BytesIO
    import PyPDF2
    filename = "test.pdf"
    
    container_name="test"
    
    blob_service_client = BlobServiceClient.from_connection_string("connection string")
    container_client=blob_service_client.get_container_client(container_name)
    blob_client = container_client.get_blob_client(filename)
    streamdownloader=blob_client.download_blob()
    
    stream = BytesIO()
    streamdownloader.download_to_stream(stream)
    
    fileReader = PyPDF2.PdfFileReader(stream)
    
    print(fileReader.numPages)
    

    And this is my result. It will print the pdf pages number.

提交回复
热议问题