我正在使用 ipfs-http-client 从 infura 中读取文件内容,如何使用“cat”功能正确获取字符串/json 格式的数据?
const client = create({
url: ipfsUrl(),
headers: {
authorization: ipfsAuthPhrase(),
},
});
const cidformat = "f" + cid.substring(2);
const cidV0 = new CID(cidformat).toV0().toString();
const resp = await client.cat(cidV0);
let content = [];
for await (const chunk of resp) {
content = [...content, ...chunk];
}
console.log(content.toString());
现在我只是在控制台日志中获取一组二进制文件。
最佳答案
从这一点来看,它只是解码 content
缓冲区的问题。
如果内容是一些 JSON:
const raw = Buffer.from(content).toString('utf8')
console.log(JSON.parse(raw))
如果内容是图片:
Buffer.from(content).toString('base64')
关于javascript - 如何从 ipfs-http-client 获取文件内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70979793/