Skip to content

UW-Milwaukee Cloud Computing

Articles on cloud computing from the brave students of CS790/657 Topics in Comp Sci: Cloud Computing

  • Home
  • About
    • About This Site
    • Contributors
    • Creating a Site Like This One
  • Projects
    • Hands-On Project 1: Cloud Computing Warmup
    • Hands-On Project 2: Development PC in the Cloud
    • Hands-On Project 3: Serverless Computing
    • Hands-On Project 4: Application Deployment Using Platform as a Service
    • Hands-On Project 5: Using Cloud Command Line Interfaces
  • Topics
    • Analytics and ML
    • Cloud Security
    • Containers & Orchestration
    • General
    • Platform Services
    • Serverless Computing
    • Software as a Service
    • Storage Services
    • Virtual Machines

How To Upload File To Azure Blob Using Python

Posted on December 7, 2022December 7, 2022 by Tarique Shaikh

You can follow along with this article as it walks you through each step of uploading a file to Azure blob storage using Python SDK from Visual Studio Code.

One needs a working Azure membership and a Python code editor to get started with this.

Let’s start by creating an Azure container.

Creating Azure Storage Account And Container

To execute this flow, you may either use an Azure container that already exists or create a new one by following the instructions below:

  • Log in to the Azure portal using the link https://portal.azure.com
  • Search for ‘storage accounts’ in the top search bar and it will open a new page where you need to click on Create button as shown below:

Furnish all the required fields and click on Create. Doing this will create an Azure storage account for you. I’ve created a storage account named testupload01 following the same process.

  • Click on a newly created storage account (for me, it is testupload01) and select Containers from the left-side blade as shown below, and follow the steps as defined in sequence:
  • If everything is configured correctly, you will see that a container is created and listed as shown at point 5. Here my container name is file-folder and this will hold the file, which we will upload in our next step.

Installing Required Python Package

We must install and import the necessary package, just as with any other Python application. The import command is as:

pip install azure-storage-blob

Python Code To Upload A File To Azure Blob

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 data:
      blob_client.upload_blob(data)
      print(f”Uploaded {file_name}.”)
# calling a function to perform upload
uploadToBlobStorage('PATH_OF_FILE_TO_UPLOAD','FILE_NAME')

Explanation of the above code

  1. We are importing the BlobServiceClient from azure.storage.blob module.
  2. Then we need to define 4 key parameters using which our application can communicate to Azure:
    • storage_account_key
    • storage_account_name
    • connection_string
    • container_name
  3. Then we define a function that will perform the file upload, it will take two parameters:
    • file_path where our file is stored in the local machine.
    • file_name is the file name that we will be using to save the file, this is the file name that we can see in the container when our upload is complete.
  4. Inside the function, we need a blob service container, we use the connection string of our blob BlobServiceClient.from_connection_string(connection_string)
  5. Then, we get the client object using the function get_blob_client, which takes two parameters:
    1. container name
    2. blob file name that we are going to create
  6. Then, we need to open the file and read it in binary mode. We are using “with” statement providing the file path and “rb” is for reading it in binary.
  7. upload_blob is the function that we can use to upload the file using the data that we read.
  8. Then we are printing a message saying that the upload was successful along with the file name.
  9. At last, we can call this function and pass the appropriate parameters

The above 4 key parameters can be fetched from the Azure portal

  1. To get the storage account key, storage account name, and connection string, navigate to the storage account you created to use, under security + networking, go to Access keys.
  2. For the container name, go to containers under data storage and get the container name.

File In Azure Blob

If the above lines of code are properly executed, your just uploaded file will be shown in a container as shown below:

Tagged Azure, Blob, Container, Python, SDK, Storage account

Post navigation

⟵ Scheduling EC2 Instances with Lambda Function in AWS
Hosting a WordPress website using AWS LightSail ⟶

Categories

  • Analytics and ML
  • Cloud Security
  • Concepts
  • Containers & Orchestration
  • Fundamentals
  • General
  • Platform Services
  • Project 1: Cloud Computing Warmup
  • Project 2: Development PC in the Cloud
  • Project 3: Serverless Computing
  • Project 4: App Deployment Using Platform as a Service
  • Project 5: Using the Command Line Interface (CLI)
  • Serverless Computing
  • Service Offerings
  • Software as a Service
  • Storage Services
  • Topics
  • Virtual Machines

Archives

  • January 2024
  • December 2022
  • November 2022
  • October 2022
  • September 2022
  • August 2022
Copyright © 2025 UW-Milwaukee Cloud Computing | Regular Blog by Ascendoor | Powered by WordPress.