I've tried to upload files to ipfs. But it's not uploading in ipfs. I'm getting hash in return after uploading the file. But it's not accessible using https://ipfs.io/ipfs/+hash
but I can access the file localhost:8080/ipfs/+hash
.
What am I doing wrong? What do I upload files in https://ipfs.io/ipfs
here is my app.js:
const express = require("express");
const app = express();
const ipfsClient = require("ipfs-http-client");
const ipfs = new ipfsClient();
const expFileUpload = require("express-fileupload");
app.use(expFileUpload());
app.post("/upload", (req, res) => {
let fileObj = {};
if (req.files.inputFile) {
const file = req.files.inputFile;
const fileName = file.name;
const filePath = __dirname + "/files/" + fileName;
file.mv(filePath, async (err) => {
if (err) {
console.log("Error: failed to download file.");
return res.status(500).send(err);
}
const fileHash = await addFile(fileName, filePath);
console.log("File Hash received __>", fileHash);
fs.unlink(filePath, (err) => {
if (err) {
console.log("Error: Unable to delete file. ", err);
}
});
fileObj = {
file: file,
name: fileName,
path: filePath,
hash: fileHash
};
res.render("transfer", { fileObj });
});
}
});
const addFile = async (fileName, filePath) => {
const file = fs.readFileSync(filePath);
const filesAdded = await ipfs.add({ path: fileName, content: file }, {
progress: (len) => console.log("Uploading file..." + len)
});
console.log(filesAdded);
const fileHash = filesAdded.cid.string;
return fileHash;
};
app.listen(3000);
Need help. Thank you.
As @deltab said, your local IPFS node must be reachable from the gateway. It's not possible to push or upload files to the IPFS gateway. When you make an http request to the gateway it looks up the content on the IPFS network for you.
Your local IPFS node is hosting the data you added to it. If the gateway's IPFS nodes can't connect to your local IPFS node, then it won't be able to find the data for the hash you requested. (Unless other nodes are also hosting it... co-hosting FTW \o/)
If your local IPFS node is running, it may be stuck behind a NAT or Firewall. If you run ipfs id
, you'll see an array of Addresses
your node is listening on. If you see one that looks like a public IP address, then grab the IP and port and see if the port is open using an online service like https://portchecker.co/
The https://docs.ipfs.io has good articles on: