【IPFS应用开发】--自定义储存固定器

在IPFS应用开发过程中,遇到了一个问题,就是没有好的与做种节点交互的程序,本文编写固定器在做种节点上就行储存固定(pin)管理。

filecoin已经开始在测试了,这些测试人员都面临着一个问题:近4G的证明文件要下载。
通过ipfs网络来下载再适合不过了,先固定到本地节点,要用到时再传输到filecoin节点,固定这4G文件成为本固定器的范例。

  1. 结构图
    在这里插入图片描述

  2. pin-manage

#!/usr/bin/python3
import ipfshttpclient
import json
import os
import pika
api = ipfshttpclient.connect('/ip4/127.0.0.1/tcp/5001',timeout=1200)
path = os.path.join(os.path.split(os.path.realpath(__file__))[0],'data')

username = 'guest'
pwd = 'guest'
user_pwd = pika.PlainCredentials(username, pwd)
s_conn = pika.BlockingConnection(pika.ConnectionParameters('127.0.0.1', credentials=user_pwd))

chan = s_conn.channel() 
chan.queue_declare(queue='ipfspin',durable=True)


def loadjson(jsonfile):
    with open(jsonfile) as json_file:
        data = json.load(json_file)
        return data

def getpinhash():
    mylist = []
    templist = api.pin.ls(type='recursive')
    for item in templist['Keys']:
        mylist.append(item)
    return mylist

def getmyhash():
    mylist = []
    files = os.listdir(path)
    for item in files:
        if item == 'parameters.json':
            data = loadjson(os.path.join(path,item))
            for k1,v1 in data.items():
                mylist.append(v1['cid'])
    return mylist

def ipfsrmpin(lists):
    for item in lists:
        print(item)
        api.pin.rm(item)

def ipfsaddpin(lists):
    for item in lists:
        print(item)
        #api.pin.add(item)
        chan.basic_publish(exchange='',routing_key='ipfspin',body=item,properties=pika.BasicProperties(delivery_mode=2))
    s_conn.close()


if __name__ =='__main__':

    localhash = getpinhash()
    pinhash = getmyhash()
    
    rmpinhash = list(set(localhash).difference(set(pinhash)))
    downloadpinhash = list(set(pinhash).difference(set(localhash)))
    
    print(localhash)
    print(pinhash)
    print(rmpinhash)
    ipfsrmpin(rmpinhash)
    print(downloadpinhash)
    ipfsaddpin(downloadpinhash)

  1. pin-server
#!/usr/bin/python3
import ipfshttpclient
import pika
import time
username = 'guest'
pwd= 'guest'
api = ipfshttpclient.connect('/ip4/127.0.0.1/tcp/5001',timeout=1200)
user_pwd = pika.PlainCredentials(username, pwd)
s_conn = pika.BlockingConnection(pika.ConnectionParameters('127.0.0.1', credentials=user_pwd))
chan = s_conn.channel()
chan.queue_declare(queue='ipfspin',durable=True)
def callback(ch,method,properties,body):
    print(body.decode('UTF-8'))
    api.pin.add(body.decode('UTF-8'))
    ch.basic_ack(delivery_tag = method.delivery_tag)
chan.basic_qos(prefetch_count=1)
chan.basic_consume(queue='ipfspin',on_message_callback=callback,auto_ack=False)
chan.start_consuming()

  1. 运行服务端
nohup python3 /opt/ipfscache/pin-server.py &
  1. 放置json文件
    在这里插入图片描述
    直接将文件丢上去即可
  2. 查看
    ipfs已经固定的大小:
    在这里插入图片描述
    有垃圾还没回收
    ipfs 固定的内容
    在这里插入图片描述
    在固定过程中,消息队列的情况,(我的节点已经固定好了,删除一个文件后执行截图的)
    在这里插入图片描述
    文件已经被固定在这个ipfs节点上了
原创文章 29 获赞 21 访问量 1万+

猜你喜欢

转载自blog.csdn.net/weixin_43668031/article/details/94971703