0

我正在尝试使用 Node.js 中的 ipfs-core 模块将文件保存到磁盘。

我试过了

const node = await IPFS.create();
const stream = node.cat("QmZxK39cgDzGNQRUg1Z6HaSErNzdXQSMRVUSYokcQmTnnr");
  let data = "";
  for await (const chunk of stream) {
    // chunks of data are returned as a Buffer, convert it back to a string
    data += chunk.toString();
  }
  fs.writeFile("test.png", data, (err) => console.log(err));

但我无法打开保存的文件。node.get() 似乎也不起作用。有谁知道我在哪里滑倒?

4

1 回答 1

0
import { create } from 'ipfs-http-client';
const fs = require('fs');

// and then call the below function
async function getIpfsFileAsArchive(ipfs, cid){
    for await (const buf of ipfs.get(cid, {archive: true, compress: true})) {
        const ipfs = await create({host: "replace with ipfs host ip address"});
        fs.createWriteStream(`./ipfs/${cid}.tar.gz`).write(buf);
    }
}
于 2022-03-04T06:52:32.450 回答