<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Download Binary File from SFTP in Notebook in Data Engineering</title>
    <link>https://community.fabric.microsoft.com/t5/Data-Engineering/Download-Binary-File-from-SFTP-in-Notebook/m-p/3910784#M1547</link>
    <description>&lt;P&gt;Thank you! That link gave me the clue I needed. I'm downloaded data from SFTP using:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="python"&gt;## Main SFTP Function

def download_files_from_sftp(hostname, port, username, password, local_directory, remote_directory):
    ## Initialize the SSH client
    ssh_client = paramiko.SSHClient()
    ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    
    try:
        ## Connect to the server
        ssh_client.connect(hostname, port=port, username=username, password=password)

        ## Initialize SFTP client
        with ssh_client.open_sftp() as sftp:
            ## List files in the specified directory
            file_list = sftp.listdir(f"{remote_directory}")
            for file in file_list:
                remote_filepath = f"{remote_directory}/{file}"
                local_filepath = os.path.join(local_directory, file)

                ## Check if it is a file and not a directory
                if sftp.stat(remote_filepath).st_mode &amp;amp; 0o170000 == 0o100000: # 0o100000 is the mask for a regular file
                    ## Download the file
                    ## Reference lakehouse as local storage using: "/lakehouse/default/Files/FolderName/filename.csv"
                    sftp.get(remote_filepath, local_filepath)
                    print(f"Downloaded {file}")

    except Exception as e:
        print(f"Error: {e}")

    finally:
        ## Close the SSH client
        if ssh_client: ssh_client.close()&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The missing part was correctly referencing the attached default lakehouse as if it was local storage.&lt;/P&gt;
&lt;P&gt;By using a path in the format:&amp;nbsp;&lt;STRONG&gt;&lt;SPAN&gt;"/lakehouse/default/Files/FolderName/filename.csv"&amp;nbsp;&lt;/SPAN&gt;&lt;/STRONG&gt;&lt;SPAN&gt;the problems I was having went away!&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Obviously if you're using the code above make sure you're storing the passwords etc as secrets in Azure Key Vault and access them using&lt;/SPAN&gt;&lt;/P&gt;
&lt;LI-CODE lang="python"&gt;sftp_password = mssparkutils.credentials.getSecret("https://keyvaultname.vault.azure.net/", "Secret-Name")&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sat, 11 May 2024 14:03:30 GMT</pubDate>
    <dc:creator>bcdobbs</dc:creator>
    <dc:date>2024-05-11T14:03:30Z</dc:date>
    <item>
      <title>Download Binary File from SFTP in Notebook</title>
      <link>https://community.fabric.microsoft.com/t5/Data-Engineering/Download-Binary-File-from-SFTP-in-Notebook/m-p/3901404#M1544</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;I know I can do this in a data pipeline copy activity but I'd like to be able to download a binary file from SFTP within a notebook.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I can download CSV using&amp;nbsp;&lt;SPAN&gt;paramiko&amp;nbsp;&lt;/SPAN&gt;decoding it to a string and then writing it to one lake but when I try and write anything other than string data it fails saying it can't find the path. I'd like to be able to just do a direct binary copy.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Has anyone got a code snippet that would work?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;Ben&lt;/P&gt;</description>
      <pubDate>Wed, 08 May 2024 07:53:01 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Data-Engineering/Download-Binary-File-from-SFTP-in-Notebook/m-p/3901404#M1544</guid>
      <dc:creator>bcdobbs</dc:creator>
      <dc:date>2024-05-08T07:53:01Z</dc:date>
    </item>
    <item>
      <title>Re: Download Binary File from SFTP in Notebook</title>
      <link>https://community.fabric.microsoft.com/t5/Data-Engineering/Download-Binary-File-from-SFTP-in-Notebook/m-p/3903937#M1545</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.fabric.microsoft.com/t5/user/viewprofilepage/user-id/168297"&gt;@bcdobbs&lt;/a&gt;&amp;nbsp;,&lt;BR /&gt;&lt;BR /&gt;Thanks for using Fabric Community.&lt;BR /&gt;Can you please check this similar thread -&amp;nbsp;&lt;A href="https://community.fabric.microsoft.com/t5/Data-Pipelines/Copy-a-file-from-an-URL-to-OneLake/m-p/3735616" target="_blank"&gt;Solved: Re: Copy a file from an URL to OneLake - Microsoft Fabric Community&lt;/A&gt;&amp;nbsp;.&amp;nbsp;&lt;BR /&gt;I think you might get some ideas after looking into that thread.&lt;BR /&gt;&lt;BR /&gt;Please let me know if you have further queries.&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 09 May 2024 02:56:53 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Data-Engineering/Download-Binary-File-from-SFTP-in-Notebook/m-p/3903937#M1545</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2024-05-09T02:56:53Z</dc:date>
    </item>
    <item>
      <title>Re: Download Binary File from SFTP in Notebook</title>
      <link>https://community.fabric.microsoft.com/t5/Data-Engineering/Download-Binary-File-from-SFTP-in-Notebook/m-p/3907658#M1546</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://community.fabric.microsoft.com/t5/user/viewprofilepage/user-id/168297"&gt;@bcdobbs&lt;/a&gt;&amp;nbsp;,&lt;BR /&gt;&lt;BR /&gt;We haven’t heard from you on the last response and was just checking back to see if you have a resolution yet .&lt;BR /&gt;In case if you have any resolution please do share that same with the community as it can be helpful to others . &lt;BR /&gt;Otherwise, will respond back with the more details and we will try to help .&lt;/P&gt;</description>
      <pubDate>Fri, 10 May 2024 07:36:31 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Data-Engineering/Download-Binary-File-from-SFTP-in-Notebook/m-p/3907658#M1546</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2024-05-10T07:36:31Z</dc:date>
    </item>
    <item>
      <title>Re: Download Binary File from SFTP in Notebook</title>
      <link>https://community.fabric.microsoft.com/t5/Data-Engineering/Download-Binary-File-from-SFTP-in-Notebook/m-p/3910784#M1547</link>
      <description>&lt;P&gt;Thank you! That link gave me the clue I needed. I'm downloaded data from SFTP using:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="python"&gt;## Main SFTP Function

def download_files_from_sftp(hostname, port, username, password, local_directory, remote_directory):
    ## Initialize the SSH client
    ssh_client = paramiko.SSHClient()
    ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    
    try:
        ## Connect to the server
        ssh_client.connect(hostname, port=port, username=username, password=password)

        ## Initialize SFTP client
        with ssh_client.open_sftp() as sftp:
            ## List files in the specified directory
            file_list = sftp.listdir(f"{remote_directory}")
            for file in file_list:
                remote_filepath = f"{remote_directory}/{file}"
                local_filepath = os.path.join(local_directory, file)

                ## Check if it is a file and not a directory
                if sftp.stat(remote_filepath).st_mode &amp;amp; 0o170000 == 0o100000: # 0o100000 is the mask for a regular file
                    ## Download the file
                    ## Reference lakehouse as local storage using: "/lakehouse/default/Files/FolderName/filename.csv"
                    sftp.get(remote_filepath, local_filepath)
                    print(f"Downloaded {file}")

    except Exception as e:
        print(f"Error: {e}")

    finally:
        ## Close the SSH client
        if ssh_client: ssh_client.close()&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The missing part was correctly referencing the attached default lakehouse as if it was local storage.&lt;/P&gt;
&lt;P&gt;By using a path in the format:&amp;nbsp;&lt;STRONG&gt;&lt;SPAN&gt;"/lakehouse/default/Files/FolderName/filename.csv"&amp;nbsp;&lt;/SPAN&gt;&lt;/STRONG&gt;&lt;SPAN&gt;the problems I was having went away!&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Obviously if you're using the code above make sure you're storing the passwords etc as secrets in Azure Key Vault and access them using&lt;/SPAN&gt;&lt;/P&gt;
&lt;LI-CODE lang="python"&gt;sftp_password = mssparkutils.credentials.getSecret("https://keyvaultname.vault.azure.net/", "Secret-Name")&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 11 May 2024 14:03:30 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Data-Engineering/Download-Binary-File-from-SFTP-in-Notebook/m-p/3910784#M1547</guid>
      <dc:creator>bcdobbs</dc:creator>
      <dc:date>2024-05-11T14:03:30Z</dc:date>
    </item>
    <item>
      <title>Re: Download Binary File from SFTP in Notebook</title>
      <link>https://community.fabric.microsoft.com/t5/Data-Engineering/Download-Binary-File-from-SFTP-in-Notebook/m-p/3913107#M1548</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.fabric.microsoft.com/t5/user/viewprofilepage/user-id/168297"&gt;@bcdobbs&lt;/a&gt;&amp;nbsp;,&lt;BR /&gt;&lt;BR /&gt;Glad to know that your query got resolved. Please continue using Fabric Community on your further queries.&lt;/P&gt;</description>
      <pubDate>Mon, 13 May 2024 07:25:10 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Data-Engineering/Download-Binary-File-from-SFTP-in-Notebook/m-p/3913107#M1548</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2024-05-13T07:25:10Z</dc:date>
    </item>
  </channel>
</rss>

