Moving a file from a notebook environment into a managed folder

ben_p
ben_p Neuron 2020, Registered, Neuron 2021, Neuron 2022, Dataiku Frontrunner Awards 2021 Participant Posts: 143 ✭✭✭✭✭✭✭
edited July 16 in Using Dataiku

Hi everyone,

I have some python code in a notebook which is generating a PDF, I would like to copy this PDF into a managed folder. I think I want to use the function `put_file()`, but I cannot get it to work!

I have followed to docs to this stage...

handle = dataiku.Folder("acquisition_reports")
handle.put_file("tuto1.pdf","tuto1.pdf")

But I get this error:

AttributeError: 'Folder' object has no attribute 'put_file'

Where am I going wrong?

The file generated in my notebook is called tuto1.pdf.

Thanks in advance,
Ben

Best Answer

  • HarizoR
    HarizoR Dataiker, Alpha Tester, Registered Posts: 138 Dataiker
    edited July 17 Answer ✓

    Hi Ben,

    Maybe your upload operation will require handling the byte stream in a more specific way. Can you try:

    with open("path/to/local.pdf", "rb") as f:
        stream = io.BytesIO(f.read())
        folder.upload_stream("path/to/managed/folder/file.pdf", stream.getvalue())

    Best,

    Harizo

Answers

  • HarizoR
    HarizoR Dataiker, Alpha Tester, Registered Posts: 138 Dataiker

    Hi Ben,

    If you want to use a dataiku.Folder object, the appropriate method to upload a file to the managed folder is upload_stream(), as documented here.

    Best,

    Harizo

  • ben_p
    ben_p Neuron 2020, Registered, Neuron 2021, Neuron 2022, Dataiku Frontrunner Awards 2021 Participant Posts: 143 ✭✭✭✭✭✭✭
    edited July 17

    @HarizoR
    thanks for your response!

    I tried the following:
    with open("tuto1.pdf") as f:
    handle.upload_stream("tuto1.pdf", f)

    But got the error:

    UnicodeDecodeError: 'utf-8' codec can't decode byte 0x9c in position 139: invalid start byte

    Also wondered if I could just use upload_file, but this generated the same error:
    handle.upload_file("acquisition_reports/tuto1.pdf","tuto1.pdf")

    What am I doing wrong?

  • ben_p
    ben_p Neuron 2020, Registered, Neuron 2021, Neuron 2022, Dataiku Frontrunner Awards 2021 Participant Posts: 143 ✭✭✭✭✭✭✭

    Thanks @HarizoR
    , that worked! My image is getting trimmed somewhere, but that seems to be before the PDF is copied, so can't be this process effecting it.

    Thank you for your help!
    Ben

Setup Info
    Tags
      Help me…