Why isn't the `toFile()` function working in my workflow code and how does the `put_url` function update a file?

This topic was automatically generated from Slack. You can find the original thread here.

I’m getting this when I call this code below to get a file and work with it in a workflow…. Basically toFile() as mentioned here isn’t working File Stores (pipedream.com)

If I understand we are to use the get_url to download the file from the url, how does the put_url work to update the file?

**const** getDataToFile = **async** (fileName) => {
**const** file = **await** $.files.open(tesst.json) _//.toFile(/tmp/${fileName}.json)_
**if** (file) {

    `console.log(**await** file.toUrl())`
   _`// console.log(await file.toFile(`/tmp/${fileName}.json`))`_
    `**return** file;`
  `} **else** {`
    **`return`** `**null**;`
  `}`
`}`

Hi Ayoola,

Is tesst.json already uploaded to the project’s File Store?

If so, can you please describe in more detail what you mean about toFile() not working? What are you expecting to happen vs what you’re seeing?

I used code to upload tesst.json from tmp to file store….

Now, I want to load tesst.json, make some changes to it and return back to file store.

My expectation is that .toFile() will write tesst.json from file store to tmp for me to write to based on this

image.png

Thanks, good so the file tesst.json already exists in the File Store.

Are you trying to interact with it with another step downstream? From what I can tell the download is successful based on the code you provided.

What makes you believe /tmp/tesst.json doesn’t exist in the workflow’s execution environment?

I get undefined when I try this

**const** file = **await** $.files.open(tesst.json).toFile(/tmp/${fileName}.json)

  `console.log(file)`

To achieve what I showed on the screenshot, I remove the toFile() bit of the above, then I get the get_url and put_url. toUrl() works to return the get_url, but toFile() returns undefined, when I use console.dir(file), I see error

Thanks for clarifying. The result of the toFile call will not be the actual local file, but rather the File instance on the File Store.

The File instance refers to the cloud version of the file. After you called toFile it downloaded the file locally.

You’ll then need to use the Node.js fs module to load the downloaded version.

Here’s a good example of how to read a file in temp using Node.js:

So in your case, the code should look something like:

import fs from "fs";

export default defineComponent({
  async run({ steps, $ }) {
    // download the file from the cloud File Store
    await $.files.open(`tesst.json`).toFile('/tmp/tesst.json);

    // read the file version of the file stored in /tmp
    return (await fs.promises.readFile('/tmp/tesst.json')).toString()
  }
});

Oh I see…

Thank you