- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-22-2024 10:47 AM
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!
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-22-2024 01:30 PM
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));
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-22-2024 01:30 PM
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));
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-25-2024 12:18 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-25-2024 12:30 PM

