Last active
May 31, 2023 03:07
-
-
Save amalgjose/dc0a6563c52d43485995d459fd058d88 to your computer and use it in GitHub Desktop.
Python program to upload a directory or a folder to Azure Data Lake Storage Gen 2 ( ADLS Gen 2 ) . For more details read the detailed article in my blog https://amalgjose.com
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from azure.storage.blob import BlobServiceClient | |
# Install the following package before running this program | |
# pip install azure-storage-blob | |
def upload_data_to_adls(): | |
""" | |
Function to upload local directory to ADLS | |
:return: | |
""" | |
# Azure Storage connection string | |
connect_str = "" | |
# Name of the Azure container | |
container_name = "" | |
# The path to be removed from the local directory path while uploading it to ADLS | |
path_to_remove = "" | |
# The local directory to upload to ADLS | |
local_path = "" | |
blob_service_client = BlobServiceClient.from_connection_string(connect_str) | |
# The below code block will iteratively traverse through the files and directories under the given folder and uploads to ADLS. | |
for r, d, f in os.walk(local_path): | |
if f: | |
for file in f: | |
file_path_on_azure = os.path.join(r, file).replace(path_to_remove, "") | |
file_path_on_local = os.path.join(r, file) | |
blob_client = blob_service_client.get_blob_client(container=container_name, blob=file_path_on_azure) | |
with open(file_path_on_local, "rb") as data: | |
blob_client.upload_blob(data) | |
print("uploading file ---->", file_path_on_local) | |
if __name__ == '__main__': | |
# invoking the upload_data_to_adls() function. | |
upload_data_to_adls() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@amalgjose Please note,
import os
is missing. This snippet is super useful, thanks.