Skip to main content

Using Azure SDK, upload files to Azure storage

 Here is a script to upload files to azure storage using the latest SDK.


#!/usr/bin/env python
from azure.storage.blob import BlobClient
import os
import uuid

storage_access_key = "storage_account_access_key"
storage_name = "storage_account_name"
CNTR_STORAGE_NAME = "container_name"
con_str = f'DefaultEndpointsProtocol=https;AccountName={storage_name};AccountKey={storage_access_key}'

# Create a local directory to hold blob data if the path not exist
local_path = "./data"
if not os.path.exists(local_path):
os.mkdir(local_path)

# Create a file in the local data directory to upload and download
local_file_name = str(uuid.uuid4()) + ".txt"
upload_file_path = os.path.join(local_path, local_file_name)

# Write text to the file
file = open(upload_file_path, 'w')
file.write("Hello, World!")
file.close()

blob = BlobClient.from_connection_string(conn_str=con_str, container_name=CNTR_STORAGE_NAME, blob_name=local_file_name)
with open(upload_file_path, "rb") as data:
blob.upload_blob(data)

Comments

Popular posts from this blog

01 Azure DevOps in Hindi

Hello Folks, This is my first video about Azure DevOps in Hindi.

Printing - NATO phonetic alphabet #python3

  Hi Foks, This code illustrates how to print the NATO phonetic alphabet in Python3. prompt_response = str ( input ( "Enter your name?" )) dict = { 'A' : 'Alpha' , 'B' : 'Bravo' , 'C' : 'Charlie' , 'D' : 'Delta' , 'E' : 'Echo' , 'F' : 'Foxtrot' , 'G' : 'Golf' , "H" : "Hotel" , 'I' : 'India' , 'J' : 'Juliet' , 'K' : 'Kilo' , 'L' : 'Lima' , 'M' : 'Mike' , 'N' : 'November' , 'O' : 'Oscar' , 'P' : 'Papa' , 'Q' : 'Quebec' , 'R' : 'Romeo' , 'S' : 'Sierra' , 'T' : 'Tango' , 'U' : 'Uniform' , 'V' : 'Victor' , 'W' : 'Whiskey' , 'X' : 'Xray' , 'Y' : 'Yankee' , 'Z' : '...

How I Fixed an Azure SQL Server Error: Retrieving Long Term Retention Policies for Database Using Terraform

During my recent project, I encountered a deployment issue while configuring Azure SQL Server and Long Term Retention (LTR) Policies using Terraform . The error occurred when attempting to retrieve the LTR policies, and the root cause was twofold: Incorrect Dependency References : The problem stemmed from an incorrect reference within the SQL Server identity block, which led to Terraform not properly managing dependencies. Specifically, it failed to establish the necessary order for operations. Race Condition with Key Vault Encryption : In addition, there was a race condition with the Key Vault encryption key , where the SQL Server identity was trying to assign access policies before the encryption key was fully created and available. These challenges resulted in deployment failures, manifesting in errors such as "retrieving Long Term Retention Policies" and improper assignment of access policies to the SQL Server identity. How I Fixed It To resolve the issue: I ensured tha...