ipfs 学习

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网页
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



源码下载  https://github.com/githubityu/vueipfseth



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值