Upload file using python supabase client

Hello, I am trying supabase for the first time with fastAPI and really enjoying the project. However, I am struggling with figuring out how to upload a file using the supabage python storage client.

My function takes a an Excel file, converts it to a Pandas dataframe to perform some operations and they it should upload to Supabase storage.

This is my current function:
@app.post("/uploadfile/")
def create_upload_file(file: UploadFile = File(...)):
    """
    Uploads a file to an existing bucket.
    Parameters
    ----------
    path
        The relative file path including the bucket ID. Should be of the format `bucket/folder/subfolder/filename.png`.
    file
        The File object to be stored in the bucket. or a async generator of chunks
    file_options
        HTTP headers. For example `cache-control`
    """

    print (file.filename)

    contents = file.file.read()
    buffer = BytesIO(contents)

    df = pd.read_excel(buffer)
    df.fillna('', inplace=True)

    buffer.close()
    file.file.close()

    print (df)

    # Do operations with df, all good up to this point
    for index, row in df.iterrows():
        # print(row["id"], "|", row["title"], "|", count, "|", row["state"], sep="")
        print (row["Column4"])

    #Save the dataframe as excel file in memory
    in_memory_fp = io.BytesIO()
    df.to_excel(in_memory_fp, index=False)
    in_memory_fp.seek(0,0)
    # pd.testing.assert_frame_equal(df, pd.read_excel(in_memory_fp))

    actual_file = in_memory_fp.read()

    print (isinstance(in_memory_fp, BufferedReader))
    print (isinstance(in_memory_fp, bytes))
    print (isinstance(in_memory_fp, FileIO))

    print (isinstance(actual_file, BufferedReader))
    print (isinstance(actual_file, bytes))
    print (isinstance(actual_file, FileIO))

      
    supabase.storage().StorageFileAPI("tripcase-results").upload("tripcase-results/supabase.xlsx",actual_file)

    return {"filename": file.filename}
Was this page helpful?