Search code examples
pythonazuresaveazure-blob-storageblobstorage

Saving a file into Azure Blob storage


I am running a Python code on Azure Machine Learning platform/cloud and I cannot figure out how to save a resulting dataframe into a csv file in the Azure Blob Storage. Can someone tell me how to do it or point me where I could read more about it?


Solution

  • Can you try uploading the file to Azure blog using below python code:

    global service_client
    
    service_client = DataLakeServiceClient(account_url="{}://{}.dfs.core.windows.net".format("https", 'Storage_account_name'), credential='Storage_Account_Key')
    
    #upload file to ADLS Gen 2
    file_system_client = service_client.get_file_system_client(file_system="your_container_name")
    
    directory_client = file_system_client.get_directory_client("my-directory")
    
    file_client = directory_client.create_file("uploaded-file.txt")
    local_file = open("C:\\Users\\xxxxxxx\\Desktop\\testFile.txt", 'r')
    
    file_contents = local_file.read()
    
    file_client.append_data(data=file_contents, offset=0, length=len(file_contents))
    
    file_client.flush_data(len(file_contents))
    

    For more details to the complete code, visit this link