2

我正在尝试在使用 NodeJS 制作的应用程序中实现 IPFS。我可以添加文件并获取 CID,但如果我尝试下载它,ipfs get <CID>它不会下载任何东西。任何想法可以是什么?我使用此代码启动 IPFS 节点:

const IPFS = require('ipfs');
// Spawn your IPFS node \o/
const node = new IPFS();

node.on('ready', () => {
    node.id((err, id) => {
        if (err) {
            return console.log(err)
        }
        console.log(id)
    })

    let files = [
        {
            path: '/home/my/file.jpg',
            content: File.read('/home/my/file.jpg', null)
        }
    ]
    node.files.add(files, function (err, files) {
        if (err) {
            console.log(err);
        } else {
            console.log(files)
        }
    })
})
4

2 回答 2

4

当您启动节点应用程序时,您将获得如下输出:

[ { path: 'home/my/Pictures',
    hash: '...hash',
    size: 10329 },
  { path: 'home/my',
    hash: '...hash',
    size: 10384 },
  { path: 'home',
    hash: '...hash',
    size: 10435 },
  { path: '/home/my/Pictures/google.png',
    hash: 'QmYeznhKxbZY37g5ymFzWnmrbP8ph2fdxycAuw9S9goUYr',
    size: 10272 } ]

然后复制该文件哈希,确保您可以从浏览器访问该文件。

// replace the hash code for your own need
https://ipfs.io/ipfs/QmYeznhKxbZY37g5ymFzWnmrbP8ph2fdxycAuw9S9goUYr

然后在终端中下载文件

// replace the hash code for your own need
ipfs get QmYeznhKxbZY37g5ymFzWnmrbP8ph2fdxycAuw9S9goUYr -o=google.png

在这里查看更多选项

于 2017-12-31T10:50:27.150 回答
1

从 nodejs 你可以做这样的事情提供必要的包被导入:

const fileHash = 'you hash of the file you want to get'

ipfs.files.get(fileHash, function (err, files) {
    files.forEach((file) => {
        console.log(file.path)
        console.log("File content >> ",file.content.toString('utf8'))
    })
})
于 2018-05-25T11:23:01.647 回答