@zhenxi
2017-10-17T06:28:25.000000Z
字数 12880
阅读 4564
IPFS
IPFS中每个文件都有一个hash,分2种类型:目录和文件。
目录 /ipfs/QmbefthRKDReojALJi8nGPwvUVPqe1aXdoD9ysX44aUfvG/graph.dot
文件 /ipfs/QmWfAMqVqvuSb8sRs85zma2Z1PkhcuU61v7pbNFM5ng1X3
参考 后端开发-《go-ubuntu16.04安装go18》
root@asch-build:~# go versiongo version go1.8.4 linux/amd64
go get -u github.com/ipfs/ipfs-update
root@asch-build:~# ipfs-update --helpNAME:ipfs-update - Update ipfs.USAGE:ipfs-update [global options] command [command options] [arguments...]VERSION:1.5.1COMMANDS:versions Print out all available versions.version Print out currently installed version.install Install a version of ipfs.stash stashes copy of currently installed ipfs binaryrevert Revert to previously installed version of ipfs.fetch Fetch a given version of ipfs. Default: latest.help, h Shows a list of commands or help for one commandGLOBAL OPTIONS:--verbose Print verbose output.--distpath value specify the distributions build to use--help, -h show help--version, -v print the versionroot@asch-build:~# ipfs-update versionnoneroot@asch-build:~# ipfs-update versionsv0.3.2v0.3.4v0.3.5v0.3.6v0.3.7v0.3.8v0.3.9v0.3.10v0.3.11v0.4.0v0.4.1v0.4.2v0.4.3v0.4.4v0.4.5v0.4.6v0.4.7v0.4.8v0.4.9v0.4.10v0.4.11root@asch-build:~# dateTue Oct 10 01:51:53 UTC 2017
root@asch-build:~# ipfs-update install latestfetching go-ipfs version v0.4.11binary downloaded, verifying...success! tests all passed.installing new binary to /root/work/bin/ipfschecking if repo migration is needed...Installation complete!root@asch-build:~# ipfs --versionipfs version 0.4.11root@asch-build:~# ipfs --helpUSAGEipfs - Global p2p merkle-dag filesystem.SYNOPSISipfs [--config=<config> | -c] [--debug=<debug> | -D] [--help=<help>] [-h=<h>] [--local=<local> | -L] [--api=<api>] <command> ...OPTIONS-c, --config string - Path to the configuration file to use.-D, --debug bool - Operate in debug mode. Default: false.--help bool - Show the full command help text. Default: false.-h bool - Show a short version of the command help text. Default: false.-L, --local bool - Run the command locally, instead of using the daemon. Default: false.--api string - Use a specific API instance (defaults to /ip4/127.0.0.1/tcp/5001).SUBCOMMANDSBASIC COMMANDSinit Initialize ipfs local configurationadd <path> Add a file to IPFScat <ref> Show IPFS object dataget <ref> Download IPFS objectsls <ref> List links from an objectrefs <ref> List hashes of links from an objectDATA STRUCTURE COMMANDSblock Interact with raw blocks in the datastoreobject Interact with raw dag nodesfiles Interact with objects as if they were a unix filesystemdag Interact with IPLD documents (experimental)ADVANCED COMMANDSdaemon Start a long-running daemon processmount Mount an IPFS read-only mountpointresolve Resolve any type of namename Publish and resolve IPNS nameskey Create and list IPNS name keypairsdns Resolve DNS linkspin Pin objects to local storagerepo Manipulate the IPFS repositorystats Various operational statsp2p Libp2p stream mountingfilestore Manage the filestore (experimental)NETWORK COMMANDSid Show info about IPFS peersbootstrap Add or remove bootstrap peersswarm Manage connections to the p2p networkdht Query the DHT for values or peersping Measure the latency of a connectiondiag Print diagnosticsTOOL COMMANDSconfig Manage configurationversion Show ipfs version informationupdate Download and apply go-ipfs updatescommands List all available commandsUse 'ipfs <command> --help' to learn more about each command.ipfs uses a repository in the local file system. By default, the repo is locatedat ~/.ipfs. To change the repo location, set the $IPFS_PATH environment variable:export IPFS_PATH=/path/to/ipfsrepoEXIT STATUSThe CLI will exit with one of the following values:0 Successful execution.1 Failed executions.Use 'ipfs <subcmd> --help' for more information about each command.
其它安装方式参考:http://ipfs.io/docs/install
root@asch-build:~/ipfs# ipfs initinitializing IPFS node at /root/.ipfsgenerating 2048-bit RSA keypair...donepeer identity: QmTTpxE4Fq9LYfR8L5jbUtB4CrAtJdfQQAJSBmTkrpZwxCto get started, enter:ipfs cat /ipfs/QmS4ustL54uo8FzR9455qaxZwuMiUhyvMcX9Ba8nUH4uVv/readmeroot@asch-build:~/ipfs# ipfs cat /ipfs/QmS4ustL54uo8FzR9455qaxZwuMiUhyvMcX9Ba8nUH4uVv/readmeHello and Welcome to IPFS!██╗██████╗ ███████╗███████╗██║██╔══██╗██╔════╝██╔════╝██║██████╔╝█████╗ ███████╗██║██╔═══╝ ██╔══╝ ╚════██║██║██║ ██║ ███████║╚═╝╚═╝ ╚═╝ ╚══════╝If you're seeing this, you have successfully installedIPFS and are now interfacing with the ipfs merkledag!-------------------------------------------------------| Warning: || This is alpha software. Use at your own discretion! || Much is missing or lacking polish. There are bugs. || Not yet secure. Read the security notes for more. |-------------------------------------------------------Check out some of the other files in this directory:./about./help./quick-start <-- usage examples./readme <-- this file./security-notesroot@asch-build:~/ipfs# ipfs cat /ipfs/QmS4ustL54uo8FzR9455qaxZwuMiUhyvMcX9Ba8nUH4uVv/quick-start# 0.1 - Quick StartThis is a set of short examples with minimal explanation. It is meant asa "quick start".Add a file to ipfs:echo "hello world" >helloipfs add helloView it:ipfs cat <the-hash-you-got-here>Try a directory:mkdir foomkdir foo/barecho "baz" > foo/bazecho "baz" > foo/bar/bazipfs add -r fooView things:ipfs ls <the-hash-here>ipfs ls <the-hash-here>/baripfs cat <the-hash-here>/bazipfs cat <the-hash-here>/bar/bazipfs cat <the-hash-here>/baripfs ls <the-hash-here>/bazReferences:ipfs refs <the-hash-here>ipfs refs -r <the-hash-here>ipfs refs --helpGet:ipfs get <the-hash-here> -o foo2diff foo foo2Objects:ipfs object get <the-hash-here>ipfs object get <the-hash-here>/foo2ipfs object --helpPin + GC:ipfs pin add <the-hash-here>ipfs repo gcipfs ls <the-hash-here>ipfs pin rm <the-hash-here>ipfs repo gcDaemon:ipfs daemon (in another terminal)ipfs idNetwork:(must be online)ipfs swarm peersipfs idipfs cat <hash-of-remote-object>Mount:(warning: fuse is finicky!)ipfs mountcd /ipfs/<the-hash-here>lsTool:ipfs versionipfs updateipfs commandsipfs config --helpopen http://localhost:5001/webuiBrowse:webui:http://localhost:5001/webuivideo:http://localhost:8080/ipfs/QmVc6zuAneKJzicnJpfrqCH9gSy6bz54JhcypfJYhGUFQu/play#/ipfs/QmTKZgRNwDNZwHtJSjCp6r5FYefzpULfy37JvMt9DwvXseimages:http://localhost:8080/ipfs/QmZpc3HvfjEXvLWGQPWbHk3AjD5j8NEN4gmFN8Jmrd5g83/csmarkdown renderer app:http://localhost:8080/ipfs/QmX7M9CiYXjVeFnkfVGf3y5ixTZ2ACeSGyL1vBJY1HvQPp/mdown
root@asch-build:~/ipfs# ps aux | grep ipfsroot 24297 0.0 0.0 14052 924 pts/0 S+ 02:48 0:00 grep --color=auto ipfsroot@asch-build:~/ipfs# echo "hello world" >helloroot@asch-build:~/ipfs# ipfs add helloadded QmT78zSuBmuS4z925WZfrqQ1qHaJ56DQaTfyMUF7F8ff5o helloroot@asch-build:~/ipfs# ipfs cat /ipfs/QmS4ustL54uo8FzR9455qaxZwuMiUhyvMcX9Ba8nUH4uVv/helloError: no link named "hello" under QmS4ustL54uo8FzR9455qaxZwuMiUhyvMcX9Ba8nUH4uVvroot@asch-build:~/ipfs# ipfs cat QmT78zSuBmuS4z925WZfrqQ1qHaJ56DQaTfyMUF7F8ff5ohello worldroot@asch-build:~/ipfs# mkdir fooroot@asch-build:~/ipfs# mkdir foo/barroot@asch-build:~/ipfs# echo "baz" > foo/bazroot@asch-build:~/ipfs# echo "baz" > foo/bar/bazroot@asch-build:~/ipfs# ipfs add -r fooadded QmWLdkp93sNxGRjnFHPaYg8tCQ35NBY3XPn6KiETd3Z4WR foo/bar/bazadded QmWLdkp93sNxGRjnFHPaYg8tCQ35NBY3XPn6KiETd3Z4WR foo/bazadded QmeBpzHngbHes9hoPjfDCmpNHGztkmZFRX4Yp9ftKcXZDN foo/baradded QmdcYvbv8FSBfbq1VVSfbjLokVaBYRLKHShpnXu3crd3Gm foo // 该目录的hashroot@asch-build:~/ipfs# ipfs ls QmS4ustL54uo8FzR9455qaxZwuMiUhyvMcX9Ba8nUH4uVvQmZTR5bcpQD7cFgTorqxZDYaew1Wqgfbd2ud9QqGPAkK2V 1688 aboutQmYCvbfNbCwFR45HiNP45rwJgvatpiW38D961L5qAhUM5Y 200 contactQmY5heUM5qgRubMDD1og9fhCPA6QdkMp3QCwd4s7gJsyE7 322 helpQmejvEPop4D7YUadeGqYWmZxHhLc4JBUCzJJHWMzdcMe2y 12 pingQmXgqKTbzdh83pQtKFb19SpMCpDDcKR2ujqk3pKph9aCNF 1692 quick-startQmPZ9gcCEpqKTo6aq61g2nXGUhM4iCL3ewB6LDXZCtioEB 1102 readmeQmQ5vhrL7uv6tuoN9KeVBwd4PwfQkXdVVmDLUZuTNxqgvm 1173 security-notesroot@asch-build:~/ipfs# ipfs ls QmdcYvbv8FSBfbq1VVSfbjLokVaBYRLKHShpnXu3crd3GmQmeBpzHngbHes9hoPjfDCmpNHGztkmZFRX4Yp9ftKcXZDN 61 bar/ // 子目录QmWLdkp93sNxGRjnFHPaYg8tCQ35NBY3XPn6KiETd3Z4WR 12 baz// web ui浏览器打开http://45.32.9.220:8080/ipfs/QmdcYvbv8FSBfbq1VVSfbjLokVaBYRLKHShpnXu3crd3Gm 能看到有2个文件,其中一个是目录,点击文件后能看到文件内容http://45.32.9.220:8080/ipfs/QmdcYvbv8FSBfbq1VVSfbjLokVaBYRLKHShpnXu3crd3Gm/bar/baz // 直接看到文件内容
root@asch-build:~# ipfs daemonInitializing daemon...Adjusting current ulimit to 2048...Successfully raised file descriptor limit to 2048.Swarm listening on /ip4/127.0.0.1/tcp/4001Swarm listening on /ip4/45.32.9.220/tcp/4001Swarm listening on /ip6/::1/tcp/4001Swarm listening on /p2p-circuit/ipfs/QmTTpxE4Fq9LYfR8L5jbUtB4CrAtJdfQQAJSBmTkrpZwxCSwarm announcing /ip4/0.0.0.0/tcp/4001Swarm announcing /ip4/45.32.9.220/tcp/4001Swarm announcing /ip6/::1/tcp/4001API server listening on /ip4/0.0.0.0/tcp/5001Gateway (readonly) server listening on /ip4/0.0.0.0/tcp/8080Daemon is ready
// 当前我连接的节点root@asch-build:~# ipfs swarm peers/ip4/1.160.198.226/tcp/4001/ipfs/QmSyMwWcWx17uZaXqtQ6a6X7yaTMjBRAmFJcDRfpo4HCYf/ip4/103.23.35.123/tcp/57844/ipfs/QmVUwvS9WhdJhmkrb97E9YTgScNQQPSNFyLerhd6dH85oL.../ip4/96.244.254.218/tcp/54154/ipfs/QmXm6fQcmhQhubfpz6ebm6hn9NM8fCjg4jJxGq6kbTpcuCroot@asch-build:~#
api默认端口5001,本地文件访问速度很快,但是本地查不到的文件就很慢了
// 本机有的内容基本是瞬间clark@clark:~/ipfs$ curl "http://localhost:5001/api/v0/cat?arg=QmUgAgTVxq7UeY3Tbumz72fBsSvkUnveEgEkWvVquEvJVV"hello1// 本机没有内容返回时间大概是3秒root@asch-build:~# curl "http://localhost:5001/api/v0/cat?arg=QmP8WUPq2braGQ8iZjJ6w9di6mzgoTWyRLayrMRjjDoyGr/data/readme.md"xxx// 本地无此文件,并且swarm peer为空,也查不到别的节点那么就会一直无返回结果clark@clark:~/ipfs$ curl "http://localhost:5001/api/v0/cat?arg=QmdcYvbv8FSBfbq1VVSfbjLokVaBYRLKHShpnXu3crd3Gm"// 如果访问的hash是一个目录则会给出提示root@asch-build:~# curl "http://192.168.25.128:5001/api/v0/cat?arg=QmbefthRKDReojALJi8nGPwvUVPqe1aXdoD9ysX44aUfvG"{Message: "this dag node is a directory",Code: 0}
gateway默认端口8080
root@asch-build:~# echo $hashQmbjMZqjKxhS24WsZiq798bTd4xbmDzNmBkVpXF7Jdxnqhroot@asch-build:~# ipfs cat $hash // 默认连接的api是本地的5001I <3 IPFS -root // 访问同一个hash值,得到的内容一致root@asch-build:~# curl "https://ipfs.io/ipfs/$hash"I <3 IPFS -root // 访问同一个hash值,得到的内容一致root@asch-build:~# curl "http://127.0.0.1:8080/ipfs/$hash"I <3 IPFS -root // 访问同一个hash值,得到的内容一致
ipfs、ipfs2、ipfs3分别连接到不同的节点(--api=xxx为不同的值)zhenxi@asch:~/ipfs2$ md5sum Ubuntu14.04-cf7500eb.vmss // 文件在节点2上7573b40a4981926de9e6ef8d6025538e Ubuntu14.04-cf7500eb.vmsszhenxi@asch:~/ipfs2$ ipfs2 add Ubuntu14.04-cf7500eb.vmss // 节点2上传文件added QmWfAMqVqvuSb8sRs85zma2Z1PkhcuU61v7pbNFM5ng1X3 Ubuntu14.04-cf7500eb.vmsszhenxi@asch:~/ipfs$ ipfs ls QmWfAMqVqvuSb8sRs85zma2Z1PkhcuU61v7pbNFM5ng1X3 // 节点1访问该hashQmdtDEQbkJpDKdhPmT1vqCbqjmLvHyebqWwoMNLfSd8TJn 45623854 // 子块hashQmY4HSz1oVGdUzb8poVYPLsoqBZjH6LZrtgnme9wWn2Qko 45623854QmQg5g5dhmGG1hXdvNxg1u3uGFMkYzLMrrZAmTG4rHYmKe 44722035zhenxi@asch:~/ipfs1$ ipfs2 pin ls --type=all // 节点2查看本地pin住的文件,刚上传的文件也在里面QmRk1rduJvo5DfEYAaLobS2za9tDszk35hzaNSDCJ74DA7 indirect...QmYpGkvaKVnWUS7t4NppmPHcwXdwDS6D8ehVP3uHErXNiW indirectQmZTR5bcpQD7cFgTorqxZDYaew1Wqgfbd2ud9QqGPAkK2V indirectQmdtDEQbkJpDKdhPmT1vqCbqjmLvHyebqWwoMNLfSd8TJn indirect // 子块hashQmeUUYA87nd4rG3Hv8ybUidydRAgvXMAi2CyQEvmGqx2wX indirectQmRAAKkZeWPDMudWzYAJ3VFsynBozXEN8a9kvfuGoNanQv indirectQmWfAMqVqvuSb8sRs85zma2Z1PkhcuU61v7pbNFM5ng1X3 recursive // 父文件hashQmPZ9gcCEpqKTo6aq61g2nXGUhM4iCL3ewB6LDXZCtioEB indirectQmQg5g5dhmGG1hXdvNxg1u3uGFMkYzLMrrZAmTG4rHYmKe indirect // 子块hashQmY4HSz1oVGdUzb8poVYPLsoqBZjH6LZrtgnme9wWn2Qko indirect // 子块hash// 节点3下载分块的文件并合并到一个文件中zhenxi@asch:~/ipfs3$ ipfs3 cat QmdtDEQbkJpDKdhPmT1vqCbqjmLvHyebqWwoMNLfSd8TJn > newfilezhenxi@asch:~/ipfs3$ ipfs3 cat QmY4HSz1oVGdUzb8poVYPLsoqBZjH6LZrtgnme9wWn2Qko >> newfilezhenxi@asch:~/ipfs3$ ipfs3 cat QmQg5g5dhmGG1hXdvNxg1u3uGFMkYzLMrrZAmTG4rHYmKe >> newfilezhenxi@asch:~/ipfs3$ md5sum newfile // 和上传节点的md5值一致7573b40a4981926de9e6ef8d6025538e newfile// 另外一种方式合并文件zhenxi@asch:~/ipfs3$ ipfs3 get QmdtDEQbkJpDKdhPmT1vqCbqjmLvHyebqWwoMNLfSd8TJn // 从节点3下载文件Saving file(s) to QmdtDEQbkJpDKdhPmT1vqCbqjmLvHyebqWwoMNLfSd8TJn43.51 MB / 43.51 MB [================================================================] 100.00% 0szhenxi@asch:~/ipfs3$ ipfs get QmY4HSz1oVGdUzb8poVYPLsoqBZjH6LZrtgnme9wWn2Qko // 从节点1下载文件Saving file(s) to QmY4HSz1oVGdUzb8poVYPLsoqBZjH6LZrtgnme9wWn2Qko43.51 MB / 43.51 MB [================================================================] 100.00% 0szhenxi@asch:~/ipfs3$ ipfs2 get QmQg5g5dhmGG1hXdvNxg1u3uGFMkYzLMrrZAmTG4rHYmKe // 从节点3下载文件Saving file(s) to QmQg5g5dhmGG1hXdvNxg1u3uGFMkYzLMrrZAmTG4rHYmKe42.65 MB / 42.65 MB [================================================================] 100.00% 0szhenxi@asch:~/ipfs3$ mv QmdtDEQbkJpDKdhPmT1vqCbqjmLvHyebqWwoMNLfSd8TJn newfilezhenxi@asch:~/ipfs3$ cat QmY4HSz1oVGdUzb8poVYPLsoqBZjH6LZrtgnme9wWn2Qko >> newfilezhenxi@asch:~/ipfs3$ cat QmQg5g5dhmGG1hXdvNxg1u3uGFMkYzLMrrZAmTG4rHYmKe >> newfilezhenxi@asch:~/ipfs3$ md5sum newfile7573b40a4981926de9e6ef8d6025538e newfile // md5值一致// 也可以从任意节点直接下载整个文件ipfs get QmWfAMqVqvuSb8sRs85zma2Z1PkhcuU61v7pbNFM5ng1X3
// 将本地文件发布,返回结果peerid : hashipfs2 name publish QmWfAMqVqvuSb8sRs85zma2Z1PkhcuU61v7pbNFM5ng1X3Published to QmRx4ZyW6V7F6bQq1ALHzDz9J3arNT1yyM1jwapZ8g2qDY: /ipfs/QmWfAMqVqvuSb8sRs85zma2Z1PkhcuU61v7pbNFM5ng1X3// 本地解析节点名,结果为文件 ipfs地址zhenxi@asch:~/ipfs2$ ipfs2 name resolve QmRx4ZyW6V7F6bQq1ALHzDz9J3arNT1yyM1jwapZ8g2qDY/ipfs/QmWfAMqVqvuSb8sRs85zma2Z1PkhcuU61v7pbNFM5ng1X3// 其它节点解析报错-怀疑是bugzhenxi@asch:~/ipfs2$ ipfs name resolve QmRx4ZyW6V7F6bQq1ALHzDz9J3arNT1yyM1jwapZ8g2qDYError: Could not resolve name.https://ipfs.io/ipns/QmRx4ZyW6V7F6bQq1ALHzDz9J3arNT1yyM1jwapZ8g2qDYhttps://192.168.25.128:8080/ipns/QmRx4ZyW6V7F6bQq1ALHzDz9J3arNT1yyM1jwapZ8g2qDY