Python script to use data from Azure Storage Blob by stream, and update blob by stream without local file reading and uploading

前端 未结 1 444
礼貌的吻别
礼貌的吻别 2020-12-17 04:51

I have a python code for data processing , i want to use azure block blob as the data input for the code, to be specify, a csv file from block blob. its all good to download

相关标签:
1条回答
  • 2020-12-17 05:09

    So the document is still in progress, I think it is getting better and better... Useful link:

    • Github - Microsoft Azure Storage SDK for Python
    • Quickstart: Upload, download, and list blobs using Python

    To download a file as a stream from blob storage, you can use BytesIO:

    from azure.storage.blob import BlockBlobService
    from io import BytesIO
    from shutil import copyfileobj 
    with BytesIO() as input_blob:
        with BytesIO() as output_blob:
            block_blob_service = BlockBlobService(account_name='my_account_name', account_key='my_account_key')
            # Download as a stream
            block_blob_service.get_blob_to_stream('mycontainer', 'myinputfilename', input_blob)
    
            # Do whatever you want to do - here I am just copying the input stream to the output stream
            copyfileobj(input_blob, output_blob)
            ...
    
            # Create the a new blob
            block_blob_service.create_blob_from_stream('mycontainer', 'myoutputfilename', output_blob)
    
            # Or update the same blob
            block_blob_service.create_blob_from_stream('mycontainer', 'myinputfilename', output_blob)
    
    0 讨论(0)
提交回复
热议问题