Skip to content

Latest commit

 

History

History
28 lines (22 loc) · 1.29 KB

File metadata and controls

28 lines (22 loc) · 1.29 KB
title type
Create a directory
file-upload

We've learned how to add files to the root directory, but how can we make a new directory? Again, the process is very similar to what you may have experienced on the command line on your own computer.

The MFS method files.mkdir creates a new directory at a specified path. For example, to add a directory images to our root directory ( / ), we could do this:

await ipfs.files.mkdir('/images')

An optional parents property, which defaults to false, specifies whether any parent directories in the given path should be created if they don't already exist. We didn't need that above, because the new images directory was a direct child of an existing directory ( / ). However, if we instead wanted to create a new directory nested under others that don't yet exist, we'd need to explicitly set the value of parents to true, like so:

await ipfs.files.mkdir('/my/beautiful/images', { parents: true })

Gotcha! Although the goal of creating a missing path is similar, notice how we use the { parents: true } option with files.mkdir instead of the { create: true } option that was available with files.write.