Home > Blockchain >  How can we use Python to load files into Azure Blob Storage or Azure Data Lake Storage?
How can we use Python to load files into Azure Blob Storage or Azure Data Lake Storage?

Time:02-15

I am relatively new to Azure. I am trying to figure out how to use Python to load files into an Azure storage account, such as Data Lake or Blob Storage. I read the info from the following two URLs, and I am stuck about how to proceed.

https://docs.microsoft.com/en-us/azure/storage/blobs/data-lake-storage-directory-file-acl-python

https://docs.microsoft.com/en-us/azure/storage/blobs/storage-quickstart-blobs-python

Maybe I missed something from those resource, or they are not right for what I am trying to do. I just want to get a simple example working so I can start to understand the process better. I would like to do either of the following.

  1. copy a file from my desktop to Azure Blob Storage or Data Lake Storage

  2. download a file from a URL and save it to Azure Blob Storage or Data Lake Storage

I want to run Python from Azure, so I want the code to run in a 'Runbook'. Can someone point me in the right direction to do either item 1 or 2, as described above, or ideally both 1 and 2? Thanks.

CodePudding user response:

You need to add the azure_storage_blob python package in your automation account

Then, you can use the python code below to upload file to azure blob storage from a URL:

from azure.storage.blob import BlobServiceClient
connect_str = "StorageAccountConnectString"
blob_service_client = BlobServiceClient.from_connection_string(connect_str)
destination_blob = blob_service_client.get_blob_client("ContainerName","BlobName")
destination_blob.start_copy_from_url("URL")
  • Related