โ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!
โ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));
โ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));
โ11-25-2024 12:18 PM
โ11-25-2024 12:30 PM
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