IPFS部署与使用踩坑记录

1.Linux下部署

        基础流程:搭建IPFS【附源码】_redcock_51CTO博客

        地址不能用0.0.0.0,要用云服务器内网地址

        跨域,8080是访问地址,5001是地址

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-Credentials '["true"]'
ipfs config --json API.HTTPHeaders.Access-Control-Allow-Headers '["Authorization"]'
ipfs config --json API.HTTPHeaders.Access-Control-Expose-Headers '["Location"]'

2.ipfs-api踩坑记录

        1.纯前端不能使用node的fs,要用原生js操作DOM,res.files是返回的File对象数组

            reader是HTML5内置的,将文件转为ArrayBuffer,注意reader是异步的

<input type="file" id="file" @change="filePathChange"/>
<el-button type="primary" size="mini" @click="uploadFile">确认上传</el-button>
const filePathChange=()=>{
      let res=document.getElementById("file")
      const reader=new FileReader();
      reader.readAsArrayBuffer(res.files[0])
      reader.onload=(e)=>{
        data.fileBlob=e.target.result
        console.log('file:',data.fileBlob)
      }

    }

        2.host换成自己的节点地址或者能用的公共节点地址,注意上一步转换的ArrayBuffer要再转一次Buffer

const ipfsAPI=require('ipfs-api');

const ipfs = ipfsAPI({host: 'xxx.xxx.xxx.xxx', port: '5001', protocol: 'http'});
export  function saveOnIpfs(blob){
  console.log('start saveOnIpfs')
  console.log('blob=',blob)
  const descBuffer = Buffer.from(blob, 'utf-8');
  ipfs.add(descBuffer, function (err, files) {
    if (err || typeof files == "undefined") {
      console.log("err="+err);
    } else {
      console.log('files=',files[0].hash);
      return files
    }
  })
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值