admin管理员组文章数量:1403455
I am trying to implement IPFS in an application made with NodeJS. I can add file and get the CID, but if I try to download it with ipfs get <CID>
it does not download anything. Any ideas what can be ?. I use this code to start an IPFS node:
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)
}
})
})
I am trying to implement IPFS in an application made with NodeJS. I can add file and get the CID, but if I try to download it with ipfs get <CID>
it does not download anything. Any ideas what can be ?. I use this code to start an IPFS node:
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)
}
})
})
Share
Improve this question
asked Dec 30, 2017 at 16:39
Ander AcostaAnder Acosta
1,0691 gold badge12 silver badges26 bronze badges
2 Answers
Reset to default 4When you start the node application, you will get some output like this:
[ { 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 } ]
then copy that file hash, make sure you can access that file from the browser.
// replace the hash code for your own need
https://ipfs.io/ipfs/QmYeznhKxbZY37g5ymFzWnmrbP8ph2fdxycAuw9S9goUYr
then download the file in your terminal
// replace the hash code for your own need
ipfs get QmYeznhKxbZY37g5ymFzWnmrbP8ph2fdxycAuw9S9goUYr -o=google.png
Check here for more options
From nodejs you can do something like this provided necessary packages are imported:
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'))
})
})
本文标签: javascriptHow download file via ipfs using NodejsStack Overflow
版权声明:本文标题:javascript - How download file via ipfs using Nodejs? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744402845a2604550.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论