【p2p、分布式,区块链笔记 IPFS】网关+多地址+HTTP RPC API+kubo-rpc-client

【p2p、分布式,区块链笔记 IPFS】网关+多地址+HTTP RPC API+kubo-rpc-client 目录multiaddr API 多地址示例kubo-rpc-client连接服务 create([options])添加数据读取数据使用示例nodejs浏览器直接使用CG在 IPFS 中网关是提供公共访问接口的服务器它们允许你通过 HTTP 协议访问 IPFS 网络中的内容通常形式为http://gateway/ipfs/cid。网关扮演着桥梁的角色其通过提供传统 HTTP 协议的入口使得非 IPFS 用户也能够方便地访问存储在 IPFS 上的文件和数据。gateway为多地址multiaddress形式。multiaddr API 多地址IPFS 节点使用多地址multiaddress来指定不同的通信协议和网络层。IPFS 支持多种类型的多地址multiaddr格式支持多种协议如https,http,ws,wss等以及不同类型的连接方式如通过 DNS、IP 地址、WebSocket 等。例如DNS 地址/dns4/{hostname}/https/例如/dns4/ipfs.io/https/表示通过 DNS 解析ipfs.io并使用 HTTPS 协议与其连接。If you want browsers to connect to e.g. /dns4/example.com/tcp/443/wss/p2p/QmFooIP 地址/ip4/{ip}/tcp/{port}例如/ip4/192.168.1.100/tcp/5001表示连接到 IP 地址192.168.1.100上的 IPFS 节点使用 TCP 协议和 5001 端口。WebSocket 连接/ip4/{ip}/tcp/{port}/ws如果你使用 WebSocket 协议与节点通信可以使用这样的地址例如/ip4/127.0.0.1/tcp/5001/ws。IPv6 地址/ip6/{ipv6_address}/tcp/{port}如果你希望通过 IPv6 地址连接到 IPFS 节点可以使用类似/ip6/2001:db8::/tcp/5001这样的格式。示例IPFS 提供商constAPI_MULTIADDR/dns4/ipfs.io/https/;// 使用 ipfs.io 作为网关// 有些提供商如 Pinata、Fleek、Infura 等提供了专用的 IPFS API 端点constAPI_MULTIADDR/dns4/ipfs.infura.io/https/;// Infura 网关constAPI_MULTIADDR/dns4/ipfs.pinata.cloud/https/;// Pinata 网关使用本地运行的 IPFS 节点constAPI_MULTIADDR/ip4/127.0.0.1/tcp/5001;// 本地节点使用 WebSocket 连接constAPI_MULTIADDR/ip4/127.0.0.1/tcp/5001/ws;// WebSocket 连接kubo-rpc-client当 Kubo IPFS 节点作为守护进程运行时它会公开一个 HTTP RPC APIkubo-rpc-clientnpm i kubo-rpc-client用于连接RPC API的服务地址允许您控制节点并运行与命令行相同的命令。连接服务 create([options])import{create}fromkubo-rpc-client// connect to ipfs daemon API serverconstipfscreate(http://localhost:5001)// (the default in Node.js)// or connect with multiaddrconstipfscreate(/ip4/127.0.0.1/tcp/5001)// or using optionsconstipfscreate({host:localhost,port:5001,protocol:http})// or specifying a specific API pathconstipfscreate({host:1.1.1.1,port:80,apiPath:/ipfs/api/v0})添加数据const{cid}await client.add(Hello world!)读取数据constchunks[];forawait(constchunk of ipfs.cat(cidToFetch)){chunks.push(chunk);}constallChunksnewUint8Array(chunks.reduce((acc,chunk)[...acc,...chunk],[]));constcontentnewTextDecoder().decode(allChunks);读取的chunk为Uint8Array数据(8 位无符号整数数组CID的数据太长会分片示例Uint8Array(10) [72, 69, 76, 76, 79, 87, 79, 82, 76, 68, buffer: ArrayBuffer(10), byteLength: 10, byteOffset: 0, length: 10, Symbol(Symbol.toStringTag): ‘Uint8Array’]使用示例nodejs// sudo apt update sudo apt install -y nodejs npm// https://codesandbox.io/examples/package/kubo-rpc-client// https://codesandbox.io/p/sandbox/ipfs-upload-and-fetch-v8-4cggs3import{randomBytes}fromcrypto;import{create}fromkubo-rpc-client;constDATA_SIZE512001;//const DATA_SIZE 1860000;constAPI_MULTIADDR/dns4/ipfs-upload.v8-bellecour.iex.ec/https/;constGATEWAYS[https://ipfs-gateway.v8-bellecour.iex.ec];constuploadasync(){try{constipfscreate(API_MULTIADDR);constbufferrandomBytes(DATA_SIZE);console.log(adding${buffer.length}random bytes);constresultawaitipfs.add(buffer);console.log(ipfs add ok);const{cid}result;console.log(added${cid.toString()});awaitPromise.all(GATEWAYS.map((gateway)fetch(${gateway}/ipfs/${cid.toString()}).then((res){if(!res.ok){throwError(${gateway}bad response:${res.status});}else{console.log(found${cid.toString()}on${gateway});}}).catch((e)console.error(e.message))));}catch(e){console.error(e.message);}};constupload_cidasync(){try{const{cid}QmWp48QrabA75rUF86iEkiEMLq222YiE37D76gTtoJocxS;console.log(try to download${cid});awaitPromise.all(GATEWAYS.map((gateway)fetch(${gateway}/ipfs/${cid}).then((res){if(!res.ok){throwError(${gateway}bad response:${res.status});}else{console.log(found${cid.toString()}on${gateway});}}).catch((e)console.error(e.message))));}catch(e){console.error(e.message);}};constbuttondocument.getElementById(upload-button);constbutton2document.getElementById(upload-cid);button.addEventListener(click,()upload());button2.addEventListener(click,()upload());button.disabledfalse;button2.disabledfalse;浏览器直接使用script srchttps://unpkg.com/kubo-rpc-client/dist/index.min.js/script // https://github.com/ipfs/js-kubo-rpc-client更多示例可见https://github.com/ipfs-examples/js-ipfs-examples/tree/master/examplesCGhttps://webui.ipfs.io/#/welcome