how to get CID of file when creating using ipfs.files.write using ipfs-http-client

392 Views Asked by At

first I created directory using ipfs.files.mkdir it works fine but doesn't return folder CID or any other information, then I created file to that directory using below code

fs.readFile(temFilePath, async (err1, data) => {
      if (err1) throw err1;
      let content = await ipfsClient.files.write("/foldername/file.png",data,{create:true});
      console.log(content);
      res.status(201).send({
        message: "file uploaded successfully",
      });
    });

this code also works and I can see files and folder in IPFS Desktop App but is returns nothing content is just empty, my question is how can I get CID of FOLDER or FILEs when creating folders of files,

1

There are 1 best solutions below

0
On

You can get the CID of a file or folder you've added to MFS via ipfs.files.stat("/path/to/whatever") for example:

let fileInfo = await ipfs.files.stat("/foldername/file.png");
console.log(fileInfo.cid.toString()); // Qm...