使用node.js-infura上傳文件到ipfs
安裝
npm install ipfs-api
//IPFS
const ipfsClient = require("ipfs-api");
const ipfs=new ipfsClient({host:'ipfs.infura.io',port:5001,protocol:'https'});
router.post('/ipfs',async function(req,res,next){
const mydata=req.body.content;
console.log(mydata);
const fileHash = await addFile(mydata);
console.log(fileHash);
res.json("https://ipfs.io/ipfs/"+fileHash);
});
const addFile = async (mydata) =>{
const file = {path:'testfile',content:Buffer.from(mydata)};
const fileAdded = await ipfs.add(file);
console.log(fileAdded[0]);
return fileAdded[0].hash;
}
https://ipfs.io/ipfs/QmQw7b4Fr7S38kKTmYF2uEHuR6F1sQMmdFvuhDoULWLHw2
上傳檔案
const ipfsClient = require("ipfs-api");
const ipfs = new ipfsClient({host:'ipfs.infura.io',port:5001,protocol:'https'});
const fs = require("fs");
//上傳檔案
router.post('/ipfsfile',function(req,res,next){
addpath = req.body.addpath;
buffile = fs.readFileSync(addpath);
ipfs.add(buffile).then((fileinfo)=>{
console.log(fileinfo);
res.json("https://ipfs.io/ipfs/"+fileinfo[0].hash);
});
});
https://ipfs.io/ipfs/QmUYcVbQXfpooZDTYRTVx2F6KSXEaardfWh1xq3GoUMeqW
參考
https://medium.com/swlh/ipfs-nodejs-app-8e35f504d440
上傳檔案:
https://blog.csdn.net/koastal/article/details/78771932
Yiru@Studio - 關於我 - 意如