1.创建节点
ipfs init
2.查询节点id。
ipfs id
3.添加内容到节点。
ipfs add llkj.txt
2.启动节点。
ipfs daemon
3.查询内容
ipfs.io/ipfs/hash
4. 跨域资源共享CORS配置
- ipfs config --json API.HTTPHeaders.Access-Control-Allow-Methods '["PUT", "GET", "POST", "OPTIONS"]'
- ipfs config --json API.HTTPHeaders.Access-Control-Allow-Origin '["*"]'
- ipfs config --json API.HTTPHeaders.Access-Control-Allow-Methods "[\"PUT\",\"GET\",\"POST\",\"OPTIONS\"]"
- ipfs config --json API.HTTPHeaders.Access-Control-Allow-Origin "[\"*\"]"
- ipfs config --json API.HTTPHeaders.Access-Control-Allow-Origin '["*"]'
- ipfs config --json API.HTTPHeaders.Access-Control-Allow-Methods '["GET", "POST"]'
- ipfs config --json API.HTTPHeaders.Access-Control-Allow-Headers '["Authorization"]'
- ipfs config --json API.HTTPHeaders.Access-Control-Expose-Headers '["Location"]'
- ipfs config --json API.HTTPHeaders.Access-Control-Allow-Credentials '["true"]'
5.查看ipfs网页
http://localhost:5001/ipfs/QmQLXHs7K98JNQdWrBB2cQLJahPhmupbDjRuH1b9ibmwVa/#/home
6.修改配置
127.0.0.1 修改为0.0.0.0
就可以通过ip访问
7.生成swarm key
用到的命令
ipfs add 添加文件到ipfs ipfs add a.txt
ipfs cat 通过hash查看文件 ipfs cat hash
ipfs init 初始化节点
ipfs id 查看节点信息
ipfs add -r 上传目录 ipfs add -r testaddr/
ipfs cat /ipfs/QmPBMm4Nrimx2MmNxQBMrmABuDpt9FHp5tAQ3uT1TEeuaw/one.txt
ipfs cat /ipfs/QmcaDj3Wj517iRBVJfeD5jQHFLVtHHgj1FR1SnWxJMB16N
ipfs cat QmcaDj3Wj517iRBVJfeD5jQHFLVtHHgj1FR1SnWxJMB16N
ipfs add -r testaddr/
ipfs add -r ipfs_blogger-master/
//将根目录hash发布到id
ipfs name publish QmbZq5Rd3o5QYssUfFng16mHFqi36f7n5CsSWTuwW1VhZe
QmbZq5Rd3o5QYssUfFng16mHFqi36f7n5CsSWTuwW1VhZe根目录的hash
访问
ipfs.io/ipns/QmXV2E3DoSXWJCKProq8qavWFPWKhhUa4cEouieVYRG7uY
//ipfs创建文件夹
ipfs files mkdir /yjl
//查看文件夹列表
ipfs files ls /yjl
//拷贝文件到文件夹下
ipfs files cp /ipfs/QmdAHx9XcDCGCX2Hh3NqdZiEMbSfojJNSwopSCK34ts1m3 /yjl/one.txt
//读取文件夹文件内容
ipfs files read /yjl/one.txt
//查看
ipfs config Addresses.API
//设置
ipfs config Addresses.API /ip4/127.0.0.1/tcp/5001
//上传文本
sumitData(){ let that = this; new Promise(function (resolve,reject) { const desc = Buffer.from(that.msg,'utf-8'); that.ipfsapi.add(desc).then((response) => { console.log(response); resolve(response[0].hash) }).catch((err) =>{ console.error(err) reject(err); }) }).then((hash) => { that.ipfshash = hash; }) }
//读取文本 getData(){ this.ipfsapi.cat(this.ipfshash).then((stream => { console.log(stream) })); //上传文件 sumitFile(){ let that = this; console.log(this.$refs.file.files); let file = this.$refs.file.files[0]; let reader = new FileReader(); reader.readAsArrayBuffer(file); reader.onloadend = (e) => { console.log(reader); new Promise(function (resolve, reject) { const buffer = Buffer.from(reader.result); that.ipfsapi.add(buffer).then((response) => { console.log(response); resolve(response[0].hash) }).catch((err) =>{ console.error(err) reject(err); }) }) } }
//查看图片内容
http://localhost:8080/ipfs/QmdL2vyxExSvDPLvagWfzqVLNPRU9k7nnYvCP9vo3XC3Af