cancel
Showing results for 
Search instead for 
Did you mean: 
Get Started Discussions
Start your journey with Databricks by joining discussions on getting started guides, tutorials, and introductory topics. Connect with beginners and experts alike to kickstart your Databricks experience.
cancel
Showing results for 
Search instead for 
Did you mean: 

Import dbfs file into workspace using Python SDK

kurtrm
New Contributor III

Hello,

I am looking to replicate the functionality provided by the databricks_cli Python package using the Python SDK. Previously, using the databricks_cli WorkspaceApi object, I could use the import_workspace or import_workspace_dir methods to move a Python file, SQL file, or directory to my workspace. I am unsure how to do this using the SDK. I have tried a combination of the following:

import os
from databricks.sdk import WorkspaceClient
import databricks.sdk
from zipfile import ZipFile
import io

prod_w = WorkspaceClient(host=dbutils.notebook.entry_point.getDbutils().notebook().getContext().apiUrl().get(),
                         token=dbutils.notebook.entry_point.getDbutils().notebook().getContext().apiToken().get())
down = prod_w.dbfs.download('/FileStore/tmp/my_file.py')
# Try this?
prod_w.workspace.import_('/Users/myworkspace@email.com/my_file', content=down, format=databricks.sdk.service.workspace.ImportFormat.AUTO, language=databricks.sdk.service.workspace.Language.PYTHON, overwrite=True)
# Or this?
prod_w.workspace.upload('/Users/myworkspace@email.com/my_file', content=down, format=databricks.sdk.service.workspace.ImportFormat.AUTO, language=databricks.sdk.service.workspace.Language.PYTHON, overwrite=True)

I get errors about the ImportFormat or the argument provided to the content parameter. In the event that I do get it to work, the file is empty. 

Please don't get too caught up in the details of the above code; I'm just trying my best to provide some tangible example of what I'm trying to do: import a valid dbfs file to my workspace using the Databricks Python SDK. I'm hoping someone more knowledgeable on the SDK can provide a working example of what I'm trying to do.

 

Thank you!

Kurt

4 REPLIES 4

Atanu
Databricks Employee
Databricks Employee

kurtrm
New Contributor III

Hi Atanu,

Thank you for your reply. The examples provided in the GitHub repo are helpful but not much different than the ones provided in the documentation. It is not clear what the difference is between the upload and import_ methods...

Atanu
Databricks Employee
Databricks Employee

what is the exact issue you are facing ? any error message to share so we can check?

Kratik
New Contributor III

Even, I am looking for a way to bring files present in S3 to Workspace programmatically. 

Join Us as a Local Community Builder!

Passionate about hosting events and connecting people? Help us grow a vibrant local community—sign up today to get started!

Sign Up Now