How to access file bytes using Python code node?

Hi. I’m on n8n 2.16.1, self-hosted, with docker, docker runners with geopandas installed.

I am pulling data from 3 Baserow tables (buildings, entrances, documents). I am making a compound JSON and I am pre-fetching all files.

Then, I want to use a Python node to use geopandas to export my geo data into a geo bundle (gpkg, and to a lesser extent gpx/geojson/…). My issue is, I don’t know how to access the binary blob data with Python, and geopandas is a Python lib. When I select subdicts of the binary object, I only ever get metadata, never the underlying data.

Thank you.

This is how I try to generate my “bundle”:

import base64
import io

import geopandas
from shapely.geometry import Point

FILENAME = "gpd_export.gpkg"
gdf_prep = []


def b64(bytes):
    return base64.b64encode(bytes).decode("utf-8")


for building in _items:
    prep_common = {
        "name": building["json"].get("Name"),
        ...
    }

    for idx, bin in enumerate(building["binary"].items()):
        prep_common[f"doc_{idx:02}"] = bin
        # bin['binary]
        # bin['binary]['...]
        # bin['binary]['...]['data]
        # only ever passes the metadata, not the actual binary blob

    for entrance in building["json"]["entrances"]:
        if not entrance.get("lon") or not entrance.get("lat"):
            continue

        prep_point = prep_common.copy()
        prep_point["geometry"] = Point(
            float(entrance["lon"]), float(entrance["lat"])
        )
        gdf_prep.append(prep_point)

gdf = geopandas.GeoDataFrame(gdf_prep, crs="EPSG:4326")

io_file = io.BytesIO()
gdf.to_file(io_file, driver="GPKG")
io_file.seek(0)
gpkg_bytes = io_file.read()


return [
    {
        "json": {
            "filename": FILENAME,
            "itemCount": 1,
        },
        "binary": {
            "geo_bundle": {
                "data": b64(gpkg_bytes),
                "mimeType": "application/geopackage+sqlite3",
                "fileName": FILENAME,
            }
        },
    }
]

@boar_master the python node can’t access raw binary data directly, you just get "filesystem-v2" back instead of actual bytes — known limitation. Stick an Extract From File node before your python node set to Move File to Base64 String, then grab the base64 from _items[i]["json"] and decode with base64.b64decode().

Ah, thanks for confirming. I may do what you suggest, or I may reprocess my sqlite file with JS in a subsequent node. Thanks!

@boar_master
I would first use a JavaScript Code node to read the file with getBinaryDataBuffer() and pass it forward as base64 (or write/read it via disk on self-hosted), then consume that data in Python for geopandas

Your welcome! Hope it helps!