Azure

Importing File To Azure Blob Utilizing Python

This text will information you thru all of the steps required to add a file to Azure blob storage.

To be able to begin with this, one should have a legitimate Azure subscription and an editor to write down Python code.

Let’s begin by creating an Azure container.

Creating Azure Storage Account And Container

You need to use your present Azure container for executing this move or you’ll be able to create a brand new one utilizing the steps talked about beneath:

  • Login to Azure portal utilizing hyperlink https://portal.azure.com
  • Seek for ‘storage accounts’ within the high search bar and it’ll open a brand new web page the place you might want to click on on Create button as proven beneath: 

Furnish all of the required particulars and click on on Create. Doing this may create an Azure storage account for you. I’ve created a storage account named testupload01 following the identical course of.

  • Click on on a newly created storage account (for me, it’s testupload01) and choose Containers from the left-side blade as proven beneath and observe the steps as outlined in sequence:

  • If all the things is configured appropriately, you will notice {that a} container is created and listed as proven at level 5. Right here my container title is file-folder and this may maintain the file, which we are going to add in our subsequent step.

Putting in Required Python Bundle

Like each different Python utility, we have to set up and import the required package deal. Right here is the command to import:

pip set up azure-storage-blob

Python Code To Add A File To Azure Blob

The code written beneath is straight ahead and self-explanatory, however in case you nonetheless want an evidence about every line, I’d suggest you to observe my video recording on YouTube. The hyperlink to my recording is positioned on the backside of this text.

from azure.storage.blob import BlobServiceClient

storage_account_key = "GRAB_IT_FROM_AZURE_PORTAL"
storage_account_name = "GRAB_IT_FROM_AZURE_PORTAL"
connection_string = "GRAB_IT_FROM_AZURE_PORTAL"
container_name = "GRAB_IT_FROM_AZURE_PORTAL"

def uploadToBlobStorage(file_path,file_name):
   blob_service_client = BlobServiceClient.from_connection_string(connection_string)
   blob_client = blob_service_client.get_blob_client(container=container_name, blob=file_name)
   with open(file_path,”rb”) as information:
      blob_client.upload_blob(information)
      print(f”Uploaded {file_name}.”)

# calling a operate to carry out add
uploadToBlobStorage('PATH_OF_FILE_TO_UPLOAD','FILE_NAME')

File In Azure Blob

Executing the above strains of code efficiently will show your newly uploaded file in a container as proven beneath:

Hope you loved studying this text. If you happen to really feel that any of the factors are unclear, do watch my video right here.

Show More

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button