[关闭]
@zhenxi 2017-10-17T06:28:25.000000Z 字数 12880 阅读 4105

ipfs go版本安装和体验-ubuntu14.04.md

IPFS


写在前面的话

IPFS中每个文件都有一个hash,分2种类型:目录和文件。
目录 /ipfs/QmbefthRKDReojALJi8nGPwvUVPqe1aXdoD9ysX44aUfvG/graph.dot
文件 /ipfs/QmWfAMqVqvuSb8sRs85zma2Z1PkhcuU61v7pbNFM5ng1X3

1.安装最新的go

参考 后端开发-《go-ubuntu16.04安装go18》

  1. root@asch-build:~# go version
  2. go version go1.8.4 linux/amd64

2.安装ipfs-update工具

go get -u github.com/ipfs/ipfs-update

  1. root@asch-build:~# ipfs-update --help
  2. NAME:
  3. ipfs-update - Update ipfs.
  4. USAGE:
  5. ipfs-update [global options] command [command options] [arguments...]
  6. VERSION:
  7. 1.5.1
  8. COMMANDS:
  9. versions Print out all available versions.
  10. version Print out currently installed version.
  11. install Install a version of ipfs.
  12. stash stashes copy of currently installed ipfs binary
  13. revert Revert to previously installed version of ipfs.
  14. fetch Fetch a given version of ipfs. Default: latest.
  15. help, h Shows a list of commands or help for one command
  16. GLOBAL OPTIONS:
  17. --verbose Print verbose output.
  18. --distpath value specify the distributions build to use
  19. --help, -h show help
  20. --version, -v print the version
  21. root@asch-build:~# ipfs-update version
  22. none
  23. root@asch-build:~# ipfs-update versions
  24. v0.3.2
  25. v0.3.4
  26. v0.3.5
  27. v0.3.6
  28. v0.3.7
  29. v0.3.8
  30. v0.3.9
  31. v0.3.10
  32. v0.3.11
  33. v0.4.0
  34. v0.4.1
  35. v0.4.2
  36. v0.4.3
  37. v0.4.4
  38. v0.4.5
  39. v0.4.6
  40. v0.4.7
  41. v0.4.8
  42. v0.4.9
  43. v0.4.10
  44. v0.4.11
  45. root@asch-build:~# date
  46. Tue Oct 10 01:51:53 UTC 2017

3.利用ipfs-update工具安装ipfs

  1. root@asch-build:~# ipfs-update install latest
  2. fetching go-ipfs version v0.4.11
  3. binary downloaded, verifying...
  4. success! tests all passed.
  5. installing new binary to /root/work/bin/ipfs
  6. checking if repo migration is needed...
  7. Installation complete!
  8. root@asch-build:~# ipfs --version
  9. ipfs version 0.4.11
  10. root@asch-build:~# ipfs --help
  11. USAGE
  12. ipfs - Global p2p merkle-dag filesystem.
  13. SYNOPSIS
  14. ipfs [--config=<config> | -c] [--debug=<debug> | -D] [--help=<help>] [-h=<h>] [--local=<local> | -L] [--api=<api>] <command> ...
  15. OPTIONS
  16. -c, --config string - Path to the configuration file to use.
  17. -D, --debug bool - Operate in debug mode. Default: false.
  18. --help bool - Show the full command help text. Default: false.
  19. -h bool - Show a short version of the command help text. Default: false.
  20. -L, --local bool - Run the command locally, instead of using the daemon. Default: false.
  21. --api string - Use a specific API instance (defaults to /ip4/127.0.0.1/tcp/5001).
  22. SUBCOMMANDS
  23. BASIC COMMANDS
  24. init Initialize ipfs local configuration
  25. add <path> Add a file to IPFS
  26. cat <ref> Show IPFS object data
  27. get <ref> Download IPFS objects
  28. ls <ref> List links from an object
  29. refs <ref> List hashes of links from an object
  30. DATA STRUCTURE COMMANDS
  31. block Interact with raw blocks in the datastore
  32. object Interact with raw dag nodes
  33. files Interact with objects as if they were a unix filesystem
  34. dag Interact with IPLD documents (experimental)
  35. ADVANCED COMMANDS
  36. daemon Start a long-running daemon process
  37. mount Mount an IPFS read-only mountpoint
  38. resolve Resolve any type of name
  39. name Publish and resolve IPNS names
  40. key Create and list IPNS name keypairs
  41. dns Resolve DNS links
  42. pin Pin objects to local storage
  43. repo Manipulate the IPFS repository
  44. stats Various operational stats
  45. p2p Libp2p stream mounting
  46. filestore Manage the filestore (experimental)
  47. NETWORK COMMANDS
  48. id Show info about IPFS peers
  49. bootstrap Add or remove bootstrap peers
  50. swarm Manage connections to the p2p network
  51. dht Query the DHT for values or peers
  52. ping Measure the latency of a connection
  53. diag Print diagnostics
  54. TOOL COMMANDS
  55. config Manage configuration
  56. version Show ipfs version information
  57. update Download and apply go-ipfs updates
  58. commands List all available commands
  59. Use 'ipfs <command> --help' to learn more about each command.
  60. ipfs uses a repository in the local file system. By default, the repo is located
  61. at ~/.ipfs. To change the repo location, set the $IPFS_PATH environment variable:
  62. export IPFS_PATH=/path/to/ipfsrepo
  63. EXIT STATUS
  64. The CLI will exit with one of the following values:
  65. 0 Successful execution.
  66. 1 Failed executions.
  67. Use 'ipfs <subcmd> --help' for more information about each command.

其它安装方式参考:http://ipfs.io/docs/install

4. 使用

  1. root@asch-build:~/ipfs# ipfs init
  2. initializing IPFS node at /root/.ipfs
  3. generating 2048-bit RSA keypair...done
  4. peer identity: QmTTpxE4Fq9LYfR8L5jbUtB4CrAtJdfQQAJSBmTkrpZwxC
  5. to get started, enter:
  6. ipfs cat /ipfs/QmS4ustL54uo8FzR9455qaxZwuMiUhyvMcX9Ba8nUH4uVv/readme
  7. root@asch-build:~/ipfs# ipfs cat /ipfs/QmS4ustL54uo8FzR9455qaxZwuMiUhyvMcX9Ba8nUH4uVv/readme
  8. Hello and Welcome to IPFS!
  9. ██╗██████╗ ███████╗███████╗
  10. ██║██╔══██╗██╔════╝██╔════╝
  11. ██║██████╔╝█████╗ ███████╗
  12. ██║██╔═══╝ ██╔══╝ ╚════██║
  13. ██║██║ ██║ ███████║
  14. ╚═╝╚═╝ ╚═╝ ╚══════╝
  15. If you're seeing this, you have successfully installed
  16. IPFS and are now interfacing with the ipfs merkledag!
  17. -------------------------------------------------------
  18. | Warning: |
  19. | This is alpha software. Use at your own discretion! |
  20. | Much is missing or lacking polish. There are bugs. |
  21. | Not yet secure. Read the security notes for more. |
  22. -------------------------------------------------------
  23. Check out some of the other files in this directory:
  24. ./about
  25. ./help
  26. ./quick-start <-- usage examples
  27. ./readme <-- this file
  28. ./security-notes
  29. root@asch-build:~/ipfs# ipfs cat /ipfs/QmS4ustL54uo8FzR9455qaxZwuMiUhyvMcX9Ba8nUH4uVv/quick-start
  30. # 0.1 - Quick Start
  31. This is a set of short examples with minimal explanation. It is meant as
  32. a "quick start".
  33. Add a file to ipfs:
  34. echo "hello world" >hello
  35. ipfs add hello
  36. View it:
  37. ipfs cat <the-hash-you-got-here>
  38. Try a directory:
  39. mkdir foo
  40. mkdir foo/bar
  41. echo "baz" > foo/baz
  42. echo "baz" > foo/bar/baz
  43. ipfs add -r foo
  44. View things:
  45. ipfs ls <the-hash-here>
  46. ipfs ls <the-hash-here>/bar
  47. ipfs cat <the-hash-here>/baz
  48. ipfs cat <the-hash-here>/bar/baz
  49. ipfs cat <the-hash-here>/bar
  50. ipfs ls <the-hash-here>/baz
  51. References:
  52. ipfs refs <the-hash-here>
  53. ipfs refs -r <the-hash-here>
  54. ipfs refs --help
  55. Get:
  56. ipfs get <the-hash-here> -o foo2
  57. diff foo foo2
  58. Objects:
  59. ipfs object get <the-hash-here>
  60. ipfs object get <the-hash-here>/foo2
  61. ipfs object --help
  62. Pin + GC:
  63. ipfs pin add <the-hash-here>
  64. ipfs repo gc
  65. ipfs ls <the-hash-here>
  66. ipfs pin rm <the-hash-here>
  67. ipfs repo gc
  68. Daemon:
  69. ipfs daemon (in another terminal)
  70. ipfs id
  71. Network:
  72. (must be online)
  73. ipfs swarm peers
  74. ipfs id
  75. ipfs cat <hash-of-remote-object>
  76. Mount:
  77. (warning: fuse is finicky!)
  78. ipfs mount
  79. cd /ipfs/<the-hash-here>
  80. ls
  81. Tool:
  82. ipfs version
  83. ipfs update
  84. ipfs commands
  85. ipfs config --help
  86. open http://localhost:5001/webui
  87. Browse:
  88. webui:
  89. http://localhost:5001/webui
  90. video:
  91. http://localhost:8080/ipfs/QmVc6zuAneKJzicnJpfrqCH9gSy6bz54JhcypfJYhGUFQu/play#/ipfs/QmTKZgRNwDNZwHtJSjCp6r5FYefzpULfy37JvMt9DwvXse
  92. images:
  93. http://localhost:8080/ipfs/QmZpc3HvfjEXvLWGQPWbHk3AjD5j8NEN4gmFN8Jmrd5g83/cs
  94. markdown renderer app:
  95. http://localhost:8080/ipfs/QmX7M9CiYXjVeFnkfVGf3y5ixTZ2ACeSGyL1vBJY1HvQPp/mdown
  1. root@asch-build:~/ipfs# ps aux | grep ipfs
  2. root 24297 0.0 0.0 14052 924 pts/0 S+ 02:48 0:00 grep --color=auto ipfs
  3. root@asch-build:~/ipfs# echo "hello world" >hello
  4. root@asch-build:~/ipfs# ipfs add hello
  5. added QmT78zSuBmuS4z925WZfrqQ1qHaJ56DQaTfyMUF7F8ff5o hello
  6. root@asch-build:~/ipfs# ipfs cat /ipfs/QmS4ustL54uo8FzR9455qaxZwuMiUhyvMcX9Ba8nUH4uVv/hello
  7. Error: no link named "hello" under QmS4ustL54uo8FzR9455qaxZwuMiUhyvMcX9Ba8nUH4uVv
  8. root@asch-build:~/ipfs# ipfs cat QmT78zSuBmuS4z925WZfrqQ1qHaJ56DQaTfyMUF7F8ff5o
  9. hello world
  10. root@asch-build:~/ipfs# mkdir foo
  11. root@asch-build:~/ipfs# mkdir foo/bar
  12. root@asch-build:~/ipfs# echo "baz" > foo/baz
  13. root@asch-build:~/ipfs# echo "baz" > foo/bar/baz
  14. root@asch-build:~/ipfs# ipfs add -r foo
  15. added QmWLdkp93sNxGRjnFHPaYg8tCQ35NBY3XPn6KiETd3Z4WR foo/bar/baz
  16. added QmWLdkp93sNxGRjnFHPaYg8tCQ35NBY3XPn6KiETd3Z4WR foo/baz
  17. added QmeBpzHngbHes9hoPjfDCmpNHGztkmZFRX4Yp9ftKcXZDN foo/bar
  18. added QmdcYvbv8FSBfbq1VVSfbjLokVaBYRLKHShpnXu3crd3Gm foo // 该目录的hash
  19. root@asch-build:~/ipfs# ipfs ls QmS4ustL54uo8FzR9455qaxZwuMiUhyvMcX9Ba8nUH4uVv
  20. QmZTR5bcpQD7cFgTorqxZDYaew1Wqgfbd2ud9QqGPAkK2V 1688 about
  21. QmYCvbfNbCwFR45HiNP45rwJgvatpiW38D961L5qAhUM5Y 200 contact
  22. QmY5heUM5qgRubMDD1og9fhCPA6QdkMp3QCwd4s7gJsyE7 322 help
  23. QmejvEPop4D7YUadeGqYWmZxHhLc4JBUCzJJHWMzdcMe2y 12 ping
  24. QmXgqKTbzdh83pQtKFb19SpMCpDDcKR2ujqk3pKph9aCNF 1692 quick-start
  25. QmPZ9gcCEpqKTo6aq61g2nXGUhM4iCL3ewB6LDXZCtioEB 1102 readme
  26. QmQ5vhrL7uv6tuoN9KeVBwd4PwfQkXdVVmDLUZuTNxqgvm 1173 security-notes
  27. root@asch-build:~/ipfs# ipfs ls QmdcYvbv8FSBfbq1VVSfbjLokVaBYRLKHShpnXu3crd3Gm
  28. QmeBpzHngbHes9hoPjfDCmpNHGztkmZFRX4Yp9ftKcXZDN 61 bar/ // 子目录
  29. QmWLdkp93sNxGRjnFHPaYg8tCQ35NBY3XPn6KiETd3Z4WR 12 baz
  30. // web ui
  31. 浏览器打开http://45.32.9.220:8080/ipfs/QmdcYvbv8FSBfbq1VVSfbjLokVaBYRLKHShpnXu3crd3Gm 能看到有2个文件,其中一个是目录,点击文件后能看到文件内容
  32. http://45.32.9.220:8080/ipfs/QmdcYvbv8FSBfbq1VVSfbjLokVaBYRLKHShpnXu3crd3Gm/bar/baz // 直接看到文件内容
  1. root@asch-build:~# ipfs daemon
  2. Initializing daemon...
  3. Adjusting current ulimit to 2048...
  4. Successfully raised file descriptor limit to 2048.
  5. Swarm listening on /ip4/127.0.0.1/tcp/4001
  6. Swarm listening on /ip4/45.32.9.220/tcp/4001
  7. Swarm listening on /ip6/::1/tcp/4001
  8. Swarm listening on /p2p-circuit/ipfs/QmTTpxE4Fq9LYfR8L5jbUtB4CrAtJdfQQAJSBmTkrpZwxC
  9. Swarm announcing /ip4/0.0.0.0/tcp/4001
  10. Swarm announcing /ip4/45.32.9.220/tcp/4001
  11. Swarm announcing /ip6/::1/tcp/4001
  12. API server listening on /ip4/0.0.0.0/tcp/5001
  13. Gateway (readonly) server listening on /ip4/0.0.0.0/tcp/8080
  14. Daemon is ready
  1. // 当前我连接的节点
  2. root@asch-build:~# ipfs swarm peers
  3. /ip4/1.160.198.226/tcp/4001/ipfs/QmSyMwWcWx17uZaXqtQ6a6X7yaTMjBRAmFJcDRfpo4HCYf
  4. /ip4/103.23.35.123/tcp/57844/ipfs/QmVUwvS9WhdJhmkrb97E9YTgScNQQPSNFyLerhd6dH85oL
  5. ...
  6. /ip4/96.244.254.218/tcp/54154/ipfs/QmXm6fQcmhQhubfpz6ebm6hn9NM8fCjg4jJxGq6kbTpcuC
  7. root@asch-build:~#

http api

api默认端口5001,本地文件访问速度很快,但是本地查不到的文件就很慢了

cat

  1. // 本机有的内容基本是瞬间
  2. clark@clark:~/ipfs$ curl "http://localhost:5001/api/v0/cat?arg=QmUgAgTVxq7UeY3Tbumz72fBsSvkUnveEgEkWvVquEvJVV"
  3. hello1
  4. // 本机没有内容返回时间大概是3秒
  5. root@asch-build:~# curl "http://localhost:5001/api/v0/cat?arg=QmP8WUPq2braGQ8iZjJ6w9di6mzgoTWyRLayrMRjjDoyGr/data/readme.md"
  6. xxx
  7. // 本地无此文件,并且swarm peer为空,也查不到别的节点那么就会一直无返回结果
  8. clark@clark:~/ipfs$ curl "http://localhost:5001/api/v0/cat?arg=QmdcYvbv8FSBfbq1VVSfbjLokVaBYRLKHShpnXu3crd3Gm"
  9. // 如果访问的hash是一个目录则会给出提示
  10. root@asch-build:~# curl "http://192.168.25.128:5001/api/v0/cat?arg=QmbefthRKDReojALJi8nGPwvUVPqe1aXdoD9ysX44aUfvG"
  11. {
  12. Message: "this dag node is a directory",
  13. Code: 0
  14. }

webui

访问地址

通过网关访问文件

gateway默认端口8080

  1. root@asch-build:~# echo $hash
  2. QmbjMZqjKxhS24WsZiq798bTd4xbmDzNmBkVpXF7Jdxnqh
  3. root@asch-build:~# ipfs cat $hash // 默认连接的api是本地的5001
  4. I <3 IPFS -root // 访问同一个hash值,得到的内容一致
  5. root@asch-build:~# curl "https://ipfs.io/ipfs/$hash"
  6. I <3 IPFS -root // 访问同一个hash值,得到的内容一致
  7. root@asch-build:~# curl "http://127.0.0.1:8080/ipfs/$hash"
  8. I <3 IPFS -root // 访问同一个hash值,得到的内容一致

文件加速下载(多节点下载)

  1. ipfsipfs2ipfs3分别连接到不同的节点(--api=xxx为不同的值)
  2. zhenxi@asch:~/ipfs2$ md5sum Ubuntu14.04-cf7500eb.vmss // 文件在节点2
  3. 7573b40a4981926de9e6ef8d6025538e Ubuntu14.04-cf7500eb.vmss
  4. zhenxi@asch:~/ipfs2$ ipfs2 add Ubuntu14.04-cf7500eb.vmss // 节点2上传文件
  5. added QmWfAMqVqvuSb8sRs85zma2Z1PkhcuU61v7pbNFM5ng1X3 Ubuntu14.04-cf7500eb.vmss
  6. zhenxi@asch:~/ipfs$ ipfs ls QmWfAMqVqvuSb8sRs85zma2Z1PkhcuU61v7pbNFM5ng1X3 // 节点1访问该hash
  7. QmdtDEQbkJpDKdhPmT1vqCbqjmLvHyebqWwoMNLfSd8TJn 45623854 // 子块hash
  8. QmY4HSz1oVGdUzb8poVYPLsoqBZjH6LZrtgnme9wWn2Qko 45623854
  9. QmQg5g5dhmGG1hXdvNxg1u3uGFMkYzLMrrZAmTG4rHYmKe 44722035
  10. zhenxi@asch:~/ipfs1$ ipfs2 pin ls --type=all // 节点2查看本地pin住的文件,刚上传的文件也在里面
  11. QmRk1rduJvo5DfEYAaLobS2za9tDszk35hzaNSDCJ74DA7 indirect
  12. ...
  13. QmYpGkvaKVnWUS7t4NppmPHcwXdwDS6D8ehVP3uHErXNiW indirect
  14. QmZTR5bcpQD7cFgTorqxZDYaew1Wqgfbd2ud9QqGPAkK2V indirect
  15. QmdtDEQbkJpDKdhPmT1vqCbqjmLvHyebqWwoMNLfSd8TJn indirect // 子块hash
  16. QmeUUYA87nd4rG3Hv8ybUidydRAgvXMAi2CyQEvmGqx2wX indirect
  17. QmRAAKkZeWPDMudWzYAJ3VFsynBozXEN8a9kvfuGoNanQv indirect
  18. QmWfAMqVqvuSb8sRs85zma2Z1PkhcuU61v7pbNFM5ng1X3 recursive // 父文件hash
  19. QmPZ9gcCEpqKTo6aq61g2nXGUhM4iCL3ewB6LDXZCtioEB indirect
  20. QmQg5g5dhmGG1hXdvNxg1u3uGFMkYzLMrrZAmTG4rHYmKe indirect // 子块hash
  21. QmY4HSz1oVGdUzb8poVYPLsoqBZjH6LZrtgnme9wWn2Qko indirect // 子块hash
  22. // 节点3下载分块的文件并合并到一个文件中
  23. zhenxi@asch:~/ipfs3$ ipfs3 cat QmdtDEQbkJpDKdhPmT1vqCbqjmLvHyebqWwoMNLfSd8TJn > newfile
  24. zhenxi@asch:~/ipfs3$ ipfs3 cat QmY4HSz1oVGdUzb8poVYPLsoqBZjH6LZrtgnme9wWn2Qko >> newfile
  25. zhenxi@asch:~/ipfs3$ ipfs3 cat QmQg5g5dhmGG1hXdvNxg1u3uGFMkYzLMrrZAmTG4rHYmKe >> newfile
  26. zhenxi@asch:~/ipfs3$ md5sum newfile // 和上传节点的md5值一致
  27. 7573b40a4981926de9e6ef8d6025538e newfile
  28. // 另外一种方式合并文件
  29. zhenxi@asch:~/ipfs3$ ipfs3 get QmdtDEQbkJpDKdhPmT1vqCbqjmLvHyebqWwoMNLfSd8TJn // 从节点3下载文件
  30. Saving file(s) to QmdtDEQbkJpDKdhPmT1vqCbqjmLvHyebqWwoMNLfSd8TJn
  31. 43.51 MB / 43.51 MB [================================================================] 100.00% 0s
  32. zhenxi@asch:~/ipfs3$ ipfs get QmY4HSz1oVGdUzb8poVYPLsoqBZjH6LZrtgnme9wWn2Qko // 从节点1下载文件
  33. Saving file(s) to QmY4HSz1oVGdUzb8poVYPLsoqBZjH6LZrtgnme9wWn2Qko
  34. 43.51 MB / 43.51 MB [================================================================] 100.00% 0s
  35. zhenxi@asch:~/ipfs3$ ipfs2 get QmQg5g5dhmGG1hXdvNxg1u3uGFMkYzLMrrZAmTG4rHYmKe // 从节点3下载文件
  36. Saving file(s) to QmQg5g5dhmGG1hXdvNxg1u3uGFMkYzLMrrZAmTG4rHYmKe
  37. 42.65 MB / 42.65 MB [================================================================] 100.00% 0s
  38. zhenxi@asch:~/ipfs3$ mv QmdtDEQbkJpDKdhPmT1vqCbqjmLvHyebqWwoMNLfSd8TJn newfile
  39. zhenxi@asch:~/ipfs3$ cat QmY4HSz1oVGdUzb8poVYPLsoqBZjH6LZrtgnme9wWn2Qko >> newfile
  40. zhenxi@asch:~/ipfs3$ cat QmQg5g5dhmGG1hXdvNxg1u3uGFMkYzLMrrZAmTG4rHYmKe >> newfile
  41. zhenxi@asch:~/ipfs3$ md5sum newfile
  42. 7573b40a4981926de9e6ef8d6025538e newfile // md5值一致
  43. // 也可以从任意节点直接下载整个文件
  44. ipfs get QmWfAMqVqvuSb8sRs85zma2Z1PkhcuU61v7pbNFM5ng1X3

IPNS

  1. // 将本地文件发布,返回结果peerid : hash
  2. ipfs2 name publish QmWfAMqVqvuSb8sRs85zma2Z1PkhcuU61v7pbNFM5ng1X3
  3. Published to QmRx4ZyW6V7F6bQq1ALHzDz9J3arNT1yyM1jwapZ8g2qDY: /ipfs/QmWfAMqVqvuSb8sRs85zma2Z1PkhcuU61v7pbNFM5ng1X3
  4. // 本地解析节点名,结果为文件 ipfs地址
  5. zhenxi@asch:~/ipfs2$ ipfs2 name resolve QmRx4ZyW6V7F6bQq1ALHzDz9J3arNT1yyM1jwapZ8g2qDY
  6. /ipfs/QmWfAMqVqvuSb8sRs85zma2Z1PkhcuU61v7pbNFM5ng1X3
  7. // 其它节点解析报错-怀疑是bug
  8. zhenxi@asch:~/ipfs2$ ipfs name resolve QmRx4ZyW6V7F6bQq1ALHzDz9J3arNT1yyM1jwapZ8g2qDY
  9. Error: Could not resolve name.
  10. https://ipfs.io/ipns/QmRx4ZyW6V7F6bQq1ALHzDz9J3arNT1yyM1jwapZ8g2qDY
  11. https://192.168.25.128:8080/ipns/QmRx4ZyW6V7F6bQq1ALHzDz9J3arNT1yyM1jwapZ8g2qDY
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注