I am new to Azure cloud services as i am not getting how to copy my blob from the Azure storage account to Linux virtual machine(vm) using python. In my application,i am storing the uploaded file to Azure storage and i have also triggered the Azure function whenever the new file get uploaded now i need to copy that file to Azure VM from Azure function. Any help will be much appreciated.
Below is a code snippet specifically for Python Azure Functions. It reads a file from Blob Storage into memory and then transfers it to a remote location using SSH (specifically SFTP over SSH). It uses a library called paramiko
for SSH.
import logging
import paramiko
import azure.functions as func
def main(myblob: func.InputStream):
ssh_client = paramiko.SSHClient()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh_client.connect(hostname='YOUR_HOST_NAME', username='USERNAME_FROM_APP_SETTINGS', password='PASSWORD_FROM_APP_SETTINGS')
logging.info(f"Python blob trigger function processed blob \n"
f"Name: {myblob.name}\n"
f"Blob Size: {myblob.length} bytes")
with open('LOCAL_FILE_LOCATION', 'wb') as f:
f.write(myblob.read())
ftp_client=ssh_client.open_sftp()
ftp_client.put('LOCAL_FILE_LOCATION','REMOTE_FILE_LOCATION') # same file location written to above
ftp_client.close()