cancel
Showing results forย 
Search instead forย 
Did you mean:ย 
Data Engineering
Join discussions on data engineering best practices, architectures, and optimization strategies within the Databricks Community. Exchange insights and solutions with fellow data engineers.
cancel
Showing results forย 
Search instead forย 
Did you mean:ย 

Rendering Volumes file content programmatically

lauraxyz
New Contributor II

Hi there!

I have some files stored in Volume, and I have a use case that I need to show the file content in a UI.  Say I have a REST API that already knows the Volume path to the file, is there any built-in feature from Databricks that i can use to help get the content?

one of the limitations Unity Catalog Volumes has is: "The DBFS endpoint for the REST API does not support volumes paths.",  what does this imply to my use case?

Thank you!

1 ACCEPTED SOLUTION

Accepted Solutions

cgrant
Databricks Employee
Databricks Employee

Hi @lauraxyz here is an example using the Databricks SDK in Python:

from databricks.sdk import WorkspaceClient

ws = WorkspaceClient()
image_path = '/Volumes/catalog/schema/volume/filename.jpg'

image_data = (
    ws.files.download(image_path) # download file
    .contents # pull out streamed response
    .read() # materialize bytes
)

From here, you can do whatever you like with the image bytes. For example:

from PIL import Image
from io import BytesIO

image = Image.open(BytesIO(image_data)) # open the image with pillow
image.show() #

If using a different language, we can always use the REST API directly, here is a quick Javascript example that uses the REST API (/api/2.0/fs/files):

// Set up API endpoint and headers
const host = "https://..."; // Replace with your workspace URL, leaving the end slash off
const token = "dapi..."; // Replace with your actual Databricks access token
const imagePath = '/Volumes/catalog/schema/volume/filename.jpg';

// Download image using REST API
fetch(`${host}/api/2.0/fs/files${imagePath}`, {
    headers: {
        'Authorization': `Bearer ${token}`
    }
})
.then(response => response.blob())
.then(blob => {
    const img = document.createElement('img');
    img.src=URL.createObjectURL(blob);
    document.body.appendChild(img);
})
.catch(error => console.error('Error:', error));

 

View solution in original post

3 REPLIES 3

cgrant
Databricks Employee
Databricks Employee

Hi @lauraxyz here is an example using the Databricks SDK in Python:

from databricks.sdk import WorkspaceClient

ws = WorkspaceClient()
image_path = '/Volumes/catalog/schema/volume/filename.jpg'

image_data = (
    ws.files.download(image_path) # download file
    .contents # pull out streamed response
    .read() # materialize bytes
)

From here, you can do whatever you like with the image bytes. For example:

from PIL import Image
from io import BytesIO

image = Image.open(BytesIO(image_data)) # open the image with pillow
image.show() #

If using a different language, we can always use the REST API directly, here is a quick Javascript example that uses the REST API (/api/2.0/fs/files):

// Set up API endpoint and headers
const host = "https://..."; // Replace with your workspace URL, leaving the end slash off
const token = "dapi..."; // Replace with your actual Databricks access token
const imagePath = '/Volumes/catalog/schema/volume/filename.jpg';

// Download image using REST API
fetch(`${host}/api/2.0/fs/files${imagePath}`, {
    headers: {
        'Authorization': `Bearer ${token}`
    }
})
.then(response => response.blob())
.then(blob => {
    const img = document.createElement('img');
    img.src=URL.createObjectURL(blob);
    document.body.appendChild(img);
})
.catch(error => console.error('Error:', error));

 

lauraxyz
New Contributor II

Thank you @cgrant this is very helpful! 

Another related question:  if I want to accept user input from a UI and save that into a Volume, how should I do that?  I'm referring this Databricks API doc but it is only to update volume metadata instead of volume files.

cgrant
Databricks Employee
Databricks Employee

Hi @lauraxyz, the files API should be helpful, particularly the upload endpoint.

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