cancel
Showing results for 
Search instead for 
Did you mean: 
Community Platform Discussions
Connect with fellow community members to discuss general topics related to the Databricks platform, industry trends, and best practices. Share experiences, ask questions, and foster collaboration within the community.
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. 

Connect with Databricks Users in Your Area

Join a Regional User Group to connect with local Databricks users. Events will be happening in your city, and you won’t want to miss the chance to attend and share knowledge.

If there isn’t a group near you, start one and help create a community that brings people together.

Request a New Group