Skip to content

Handle files (blobs) as output

UbiOps supports files (blobs) as output of deployments or pipelines, but they do work a bit differently than other output data types. On this page you can find how to handle a blob type output in your deployment code, and how to retrieve a blob from a request response.

Handling blobs in deployment code

For UbiOps to be able to handle blob output it simply needs the relative path to the blob. UbiOps does the rest of the file handling automatically. Let's assume you have a deployment with output field csv which refers to a csv file output. In your deployment you can handle this output as follows:

import pandas as pd

class Deployment:

    def __init__(self, base_directory, context):
        print("Initialising My Deployment")

    def request(self, data):
        print("Processing request for My Deployment")

        # A random dataframe for illustration purposes
        data = {'Product': ['Desktop Computer','Tablet','Printer','Laptop'],
                'Price': [850,200,150,1300]
                }

        df = pd.DataFrame(data, columns= ['Product', 'Price'])

        # Here we create the new csv file
        rel_output_path = "output_csv.csv"
        df.to_csv(rel_output_path, index=False, header=True)

        # And we return the path to the newly created file
        return {
            "csv" : rel_output_path
        }

Retrieving request results with blobs

When the data type of one of your deployment output fields is a blob (file), the request response will contain a blob id. If you are using the WebApp the blob id will automatically be used to fetch the right file and you will be provided with a download link, but if you're making the request programmatically you fetch the right blob yourself. Below you can check examples of how to do that with the Python and the R client libraries. We assume we are using the deployment example from the previous section.

# We can make the request:
data = {}
response = api.deployment_requests_create(
    project_name=project_name, deployment_name=deployment_name, data=data
)

# Now we get the blob id
blob_id = response.results["csv"]

# And here we download the csv file and write it to disk
with api.blobs_get(project_name=project_name, blob_id=blob_id) as response:
    # The original file name
    output_file_name = response.getfilename()

    # Write to disk
    with open(output_file_name, 'wb') as f:
        f.write(response.read())
# We can make the request:
data = {}
response = deployment_requests_create(
    deployment.name=deployment_name, data=data
)

# Now we get the blob id
blob_id = response.results["csv"]

# And here we download the csv file and write it to disk
result <- blobs_get(
    blob.id = blob_id
)

# Get the original filename
file_name <- result$getFileName()

# Write to disk
output <- file(file.path(getwd(), file_name), "wb")
readr::write_file(result$getContent(), output)
close(output)

For more information on making requests with the Python client library, see how to make requests.