@zhenxi
2017-10-17T14:28:25.000000Z
字数 12880
阅读 4291
IPFS
IPFS中每个文件都有一个hash,分2种类型:目录和文件。
目录
/ipfs/QmbefthRKDReojALJi8nGPwvUVPqe1aXdoD9ysX44aUfvG/graph.dot
文件
/ipfs/QmWfAMqVqvuSb8sRs85zma2Z1PkhcuU61v7pbNFM5ng1X3
参考 后端开发-《go-ubuntu16.04安装go18》
root@asch-build:~# go version
go version go1.8.4 linux/amd64
go get -u github.com/ipfs/ipfs-update
root@asch-build:~# ipfs-update --help
NAME:
ipfs-update - Update ipfs.
USAGE:
ipfs-update [global options] command [command options] [arguments...]
VERSION:
1.5.1
COMMANDS:
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 binary
revert 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 command
GLOBAL OPTIONS:
--verbose Print verbose output.
--distpath value specify the distributions build to use
--help, -h show help
--version, -v print the version
root@asch-build:~# ipfs-update version
none
root@asch-build:~# ipfs-update versions
v0.3.2
v0.3.4
v0.3.5
v0.3.6
v0.3.7
v0.3.8
v0.3.9
v0.3.10
v0.3.11
v0.4.0
v0.4.1
v0.4.2
v0.4.3
v0.4.4
v0.4.5
v0.4.6
v0.4.7
v0.4.8
v0.4.9
v0.4.10
v0.4.11
root@asch-build:~# date
Tue Oct 10 01:51:53 UTC 2017
root@asch-build:~# ipfs-update install latest
fetching go-ipfs version v0.4.11
binary downloaded, verifying...
success! tests all passed.
installing new binary to /root/work/bin/ipfs
checking if repo migration is needed...
Installation complete!
root@asch-build:~# ipfs --version
ipfs version 0.4.11
root@asch-build:~# ipfs --help
USAGE
ipfs - Global p2p merkle-dag filesystem.
SYNOPSIS
ipfs [--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).
SUBCOMMANDS
BASIC COMMANDS
init Initialize ipfs local configuration
add <path> Add a file to IPFS
cat <ref> Show IPFS object data
get <ref> Download IPFS objects
ls <ref> List links from an object
refs <ref> List hashes of links from an object
DATA STRUCTURE COMMANDS
block Interact with raw blocks in the datastore
object Interact with raw dag nodes
files Interact with objects as if they were a unix filesystem
dag Interact with IPLD documents (experimental)
ADVANCED COMMANDS
daemon Start a long-running daemon process
mount Mount an IPFS read-only mountpoint
resolve Resolve any type of name
name Publish and resolve IPNS names
key Create and list IPNS name keypairs
dns Resolve DNS links
pin Pin objects to local storage
repo Manipulate the IPFS repository
stats Various operational stats
p2p Libp2p stream mounting
filestore Manage the filestore (experimental)
NETWORK COMMANDS
id Show info about IPFS peers
bootstrap Add or remove bootstrap peers
swarm Manage connections to the p2p network
dht Query the DHT for values or peers
ping Measure the latency of a connection
diag Print diagnostics
TOOL COMMANDS
config Manage configuration
version Show ipfs version information
update Download and apply go-ipfs updates
commands List all available commands
Use 'ipfs <command> --help' to learn more about each command.
ipfs uses a repository in the local file system. By default, the repo is located
at ~/.ipfs. To change the repo location, set the $IPFS_PATH environment variable:
export IPFS_PATH=/path/to/ipfsrepo
EXIT STATUS
The 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 init
initializing IPFS node at /root/.ipfs
generating 2048-bit RSA keypair...done
peer identity: QmTTpxE4Fq9LYfR8L5jbUtB4CrAtJdfQQAJSBmTkrpZwxC
to get started, enter:
ipfs cat /ipfs/QmS4ustL54uo8FzR9455qaxZwuMiUhyvMcX9Ba8nUH4uVv/readme
root@asch-build:~/ipfs# ipfs cat /ipfs/QmS4ustL54uo8FzR9455qaxZwuMiUhyvMcX9Ba8nUH4uVv/readme
Hello and Welcome to IPFS!
██╗██████╗ ███████╗███████╗
██║██╔══██╗██╔════╝██╔════╝
██║██████╔╝█████╗ ███████╗
██║██╔═══╝ ██╔══╝ ╚════██║
██║██║ ██║ ███████║
╚═╝╚═╝ ╚═╝ ╚══════╝
If you're seeing this, you have successfully installed
IPFS 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-notes
root@asch-build:~/ipfs# ipfs cat /ipfs/QmS4ustL54uo8FzR9455qaxZwuMiUhyvMcX9Ba8nUH4uVv/quick-start
# 0.1 - Quick Start
This is a set of short examples with minimal explanation. It is meant as
a "quick start".
Add a file to ipfs:
echo "hello world" >hello
ipfs add hello
View it:
ipfs cat <the-hash-you-got-here>
Try a directory:
mkdir foo
mkdir foo/bar
echo "baz" > foo/baz
echo "baz" > foo/bar/baz
ipfs add -r foo
View things:
ipfs ls <the-hash-here>
ipfs ls <the-hash-here>/bar
ipfs cat <the-hash-here>/baz
ipfs cat <the-hash-here>/bar/baz
ipfs cat <the-hash-here>/bar
ipfs ls <the-hash-here>/baz
References:
ipfs refs <the-hash-here>
ipfs refs -r <the-hash-here>
ipfs refs --help
Get:
ipfs get <the-hash-here> -o foo2
diff foo foo2
Objects:
ipfs object get <the-hash-here>
ipfs object get <the-hash-here>/foo2
ipfs object --help
Pin + GC:
ipfs pin add <the-hash-here>
ipfs repo gc
ipfs ls <the-hash-here>
ipfs pin rm <the-hash-here>
ipfs repo gc
Daemon:
ipfs daemon (in another terminal)
ipfs id
Network:
(must be online)
ipfs swarm peers
ipfs id
ipfs cat <hash-of-remote-object>
Mount:
(warning: fuse is finicky!)
ipfs mount
cd /ipfs/<the-hash-here>
ls
Tool:
ipfs version
ipfs update
ipfs commands
ipfs config --help
open http://localhost:5001/webui
Browse:
webui:
http://localhost:5001/webui
video:
http://localhost:8080/ipfs/QmVc6zuAneKJzicnJpfrqCH9gSy6bz54JhcypfJYhGUFQu/play#/ipfs/QmTKZgRNwDNZwHtJSjCp6r5FYefzpULfy37JvMt9DwvXse
images:
http://localhost:8080/ipfs/QmZpc3HvfjEXvLWGQPWbHk3AjD5j8NEN4gmFN8Jmrd5g83/cs
markdown renderer app:
http://localhost:8080/ipfs/QmX7M9CiYXjVeFnkfVGf3y5ixTZ2ACeSGyL1vBJY1HvQPp/mdown
root@asch-build:~/ipfs# ps aux | grep ipfs
root 24297 0.0 0.0 14052 924 pts/0 S+ 02:48 0:00 grep --color=auto ipfs
root@asch-build:~/ipfs# echo "hello world" >hello
root@asch-build:~/ipfs# ipfs add hello
added QmT78zSuBmuS4z925WZfrqQ1qHaJ56DQaTfyMUF7F8ff5o hello
root@asch-build:~/ipfs# ipfs cat /ipfs/QmS4ustL54uo8FzR9455qaxZwuMiUhyvMcX9Ba8nUH4uVv/hello
Error: no link named "hello" under QmS4ustL54uo8FzR9455qaxZwuMiUhyvMcX9Ba8nUH4uVv
root@asch-build:~/ipfs# ipfs cat QmT78zSuBmuS4z925WZfrqQ1qHaJ56DQaTfyMUF7F8ff5o
hello world
root@asch-build:~/ipfs# mkdir foo
root@asch-build:~/ipfs# mkdir foo/bar
root@asch-build:~/ipfs# echo "baz" > foo/baz
root@asch-build:~/ipfs# echo "baz" > foo/bar/baz
root@asch-build:~/ipfs# ipfs add -r foo
added QmWLdkp93sNxGRjnFHPaYg8tCQ35NBY3XPn6KiETd3Z4WR foo/bar/baz
added QmWLdkp93sNxGRjnFHPaYg8tCQ35NBY3XPn6KiETd3Z4WR foo/baz
added QmeBpzHngbHes9hoPjfDCmpNHGztkmZFRX4Yp9ftKcXZDN foo/bar
added QmdcYvbv8FSBfbq1VVSfbjLokVaBYRLKHShpnXu3crd3Gm foo // 该目录的hash
root@asch-build:~/ipfs# ipfs ls QmS4ustL54uo8FzR9455qaxZwuMiUhyvMcX9Ba8nUH4uVv
QmZTR5bcpQD7cFgTorqxZDYaew1Wqgfbd2ud9QqGPAkK2V 1688 about
QmYCvbfNbCwFR45HiNP45rwJgvatpiW38D961L5qAhUM5Y 200 contact
QmY5heUM5qgRubMDD1og9fhCPA6QdkMp3QCwd4s7gJsyE7 322 help
QmejvEPop4D7YUadeGqYWmZxHhLc4JBUCzJJHWMzdcMe2y 12 ping
QmXgqKTbzdh83pQtKFb19SpMCpDDcKR2ujqk3pKph9aCNF 1692 quick-start
QmPZ9gcCEpqKTo6aq61g2nXGUhM4iCL3ewB6LDXZCtioEB 1102 readme
QmQ5vhrL7uv6tuoN9KeVBwd4PwfQkXdVVmDLUZuTNxqgvm 1173 security-notes
root@asch-build:~/ipfs# ipfs ls QmdcYvbv8FSBfbq1VVSfbjLokVaBYRLKHShpnXu3crd3Gm
QmeBpzHngbHes9hoPjfDCmpNHGztkmZFRX4Yp9ftKcXZDN 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 daemon
Initializing daemon...
Adjusting current ulimit to 2048...
Successfully raised file descriptor limit to 2048.
Swarm listening on /ip4/127.0.0.1/tcp/4001
Swarm listening on /ip4/45.32.9.220/tcp/4001
Swarm listening on /ip6/::1/tcp/4001
Swarm listening on /p2p-circuit/ipfs/QmTTpxE4Fq9LYfR8L5jbUtB4CrAtJdfQQAJSBmTkrpZwxC
Swarm announcing /ip4/0.0.0.0/tcp/4001
Swarm announcing /ip4/45.32.9.220/tcp/4001
Swarm announcing /ip6/::1/tcp/4001
API server listening on /ip4/0.0.0.0/tcp/5001
Gateway (readonly) server listening on /ip4/0.0.0.0/tcp/8080
Daemon 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/QmXm6fQcmhQhubfpz6ebm6hn9NM8fCjg4jJxGq6kbTpcuC
root@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 $hash
QmbjMZqjKxhS24WsZiq798bTd4xbmDzNmBkVpXF7Jdxnqh
root@asch-build:~# ipfs cat $hash // 默认连接的api是本地的5001
I <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.vmss
zhenxi@asch:~/ipfs2$ ipfs2 add Ubuntu14.04-cf7500eb.vmss // 节点2上传文件
added QmWfAMqVqvuSb8sRs85zma2Z1PkhcuU61v7pbNFM5ng1X3 Ubuntu14.04-cf7500eb.vmss
zhenxi@asch:~/ipfs$ ipfs ls QmWfAMqVqvuSb8sRs85zma2Z1PkhcuU61v7pbNFM5ng1X3 // 节点1访问该hash
QmdtDEQbkJpDKdhPmT1vqCbqjmLvHyebqWwoMNLfSd8TJn 45623854 // 子块hash
QmY4HSz1oVGdUzb8poVYPLsoqBZjH6LZrtgnme9wWn2Qko 45623854
QmQg5g5dhmGG1hXdvNxg1u3uGFMkYzLMrrZAmTG4rHYmKe 44722035
zhenxi@asch:~/ipfs1$ ipfs2 pin ls --type=all // 节点2查看本地pin住的文件,刚上传的文件也在里面
QmRk1rduJvo5DfEYAaLobS2za9tDszk35hzaNSDCJ74DA7 indirect
...
QmYpGkvaKVnWUS7t4NppmPHcwXdwDS6D8ehVP3uHErXNiW indirect
QmZTR5bcpQD7cFgTorqxZDYaew1Wqgfbd2ud9QqGPAkK2V indirect
QmdtDEQbkJpDKdhPmT1vqCbqjmLvHyebqWwoMNLfSd8TJn indirect // 子块hash
QmeUUYA87nd4rG3Hv8ybUidydRAgvXMAi2CyQEvmGqx2wX indirect
QmRAAKkZeWPDMudWzYAJ3VFsynBozXEN8a9kvfuGoNanQv indirect
QmWfAMqVqvuSb8sRs85zma2Z1PkhcuU61v7pbNFM5ng1X3 recursive // 父文件hash
QmPZ9gcCEpqKTo6aq61g2nXGUhM4iCL3ewB6LDXZCtioEB indirect
QmQg5g5dhmGG1hXdvNxg1u3uGFMkYzLMrrZAmTG4rHYmKe indirect // 子块hash
QmY4HSz1oVGdUzb8poVYPLsoqBZjH6LZrtgnme9wWn2Qko indirect // 子块hash
// 节点3下载分块的文件并合并到一个文件中
zhenxi@asch:~/ipfs3$ ipfs3 cat QmdtDEQbkJpDKdhPmT1vqCbqjmLvHyebqWwoMNLfSd8TJn > newfile
zhenxi@asch:~/ipfs3$ ipfs3 cat QmY4HSz1oVGdUzb8poVYPLsoqBZjH6LZrtgnme9wWn2Qko >> newfile
zhenxi@asch:~/ipfs3$ ipfs3 cat QmQg5g5dhmGG1hXdvNxg1u3uGFMkYzLMrrZAmTG4rHYmKe >> newfile
zhenxi@asch:~/ipfs3$ md5sum newfile // 和上传节点的md5值一致
7573b40a4981926de9e6ef8d6025538e newfile
// 另外一种方式合并文件
zhenxi@asch:~/ipfs3$ ipfs3 get QmdtDEQbkJpDKdhPmT1vqCbqjmLvHyebqWwoMNLfSd8TJn // 从节点3下载文件
Saving file(s) to QmdtDEQbkJpDKdhPmT1vqCbqjmLvHyebqWwoMNLfSd8TJn
43.51 MB / 43.51 MB [================================================================] 100.00% 0s
zhenxi@asch:~/ipfs3$ ipfs get QmY4HSz1oVGdUzb8poVYPLsoqBZjH6LZrtgnme9wWn2Qko // 从节点1下载文件
Saving file(s) to QmY4HSz1oVGdUzb8poVYPLsoqBZjH6LZrtgnme9wWn2Qko
43.51 MB / 43.51 MB [================================================================] 100.00% 0s
zhenxi@asch:~/ipfs3$ ipfs2 get QmQg5g5dhmGG1hXdvNxg1u3uGFMkYzLMrrZAmTG4rHYmKe // 从节点3下载文件
Saving file(s) to QmQg5g5dhmGG1hXdvNxg1u3uGFMkYzLMrrZAmTG4rHYmKe
42.65 MB / 42.65 MB [================================================================] 100.00% 0s
zhenxi@asch:~/ipfs3$ mv QmdtDEQbkJpDKdhPmT1vqCbqjmLvHyebqWwoMNLfSd8TJn newfile
zhenxi@asch:~/ipfs3$ cat QmY4HSz1oVGdUzb8poVYPLsoqBZjH6LZrtgnme9wWn2Qko >> newfile
zhenxi@asch:~/ipfs3$ cat QmQg5g5dhmGG1hXdvNxg1u3uGFMkYzLMrrZAmTG4rHYmKe >> newfile
zhenxi@asch:~/ipfs3$ md5sum newfile
7573b40a4981926de9e6ef8d6025538e newfile // md5值一致
// 也可以从任意节点直接下载整个文件
ipfs get QmWfAMqVqvuSb8sRs85zma2Z1PkhcuU61v7pbNFM5ng1X3
// 将本地文件发布,返回结果peerid : hash
ipfs2 name publish QmWfAMqVqvuSb8sRs85zma2Z1PkhcuU61v7pbNFM5ng1X3
Published to QmRx4ZyW6V7F6bQq1ALHzDz9J3arNT1yyM1jwapZ8g2qDY: /ipfs/QmWfAMqVqvuSb8sRs85zma2Z1PkhcuU61v7pbNFM5ng1X3
// 本地解析节点名,结果为文件 ipfs地址
zhenxi@asch:~/ipfs2$ ipfs2 name resolve QmRx4ZyW6V7F6bQq1ALHzDz9J3arNT1yyM1jwapZ8g2qDY
/ipfs/QmWfAMqVqvuSb8sRs85zma2Z1PkhcuU61v7pbNFM5ng1X3
// 其它节点解析报错-怀疑是bug
zhenxi@asch:~/ipfs2$ ipfs name resolve QmRx4ZyW6V7F6bQq1ALHzDz9J3arNT1yyM1jwapZ8g2qDY
Error: Could not resolve name.
https://ipfs.io/ipns/QmRx4ZyW6V7F6bQq1ALHzDz9J3arNT1yyM1jwapZ8g2qDY
https://192.168.25.128:8080/ipns/QmRx4ZyW6V7F6bQq1ALHzDz9J3arNT1yyM1jwapZ8g2qDY