美文网首页
IPFS 02-基本使用

IPFS 02-基本使用

作者: Li_MAX | 来源:发表于2018-09-08 20:46 被阅读17次

1. 基本命令查看

lgydembp:IPFS liguoyu$ ipfs
USAGE
  ipfs - Global p2p merkle-dag filesystem.

  ipfs [--config=<config> | -c] [--debug=<debug> | -D] [--help=<help>] [-h=<h>] [--local=<local> | -L] [--api=<api>] <command> ...

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.

2. 上传文件至节点

  lgydembp:IPFS liguoyu$ vi test.txt

vi新建名为 test.txt文件,内容可以随便写,

lgydembp:IPFS liguoyu$ ls
go-ipfs                 test.txt
go-ipfs_v0.4.17_darwin-amd64.tar.gz
lgydembp:IPFS liguoyu$ ipfs add test.txt 
added QmUWMYMnFVerVdujyWqmUayUyWHc4tyt9DP2y8xbSBw2fq test.txt
lgydembp:IPFS liguoyu$ 

当执行完ipfs add test.txt,会将test.txt添加到ipfs当前的节点中,并且会对test.txt文件生成一个唯一的hash地址:QmUWMYMnFVerVdujyWqmUayUyWHc4tyt9DP2y8xbSBw2fq
如果想查看本地ipfs节点的数据,可以通过
ipfs cat QmUWMYMnFVerVdujyWqmUayUyWHc4tyt9DP2y8xbSBw2fq
进行查看。


当我试图通过http://ipfs.io/ipfs/QmUWMYMnFVerVdujyWqmUayUyWHc4tyt9DP2y8xbSBw2fq进行数据访问时,无法访问。
虽然数据已经添加到当前的你自己的IPFS节点中,但是并没有同步到IPFS网络,所以暂时在网络上无法访问。

同步节点

命令:ipfs daemon

lgydembp:IPFS liguoyu$ ipfs daemon
Initializing daemon...
Successfully raised file descriptor limit to 2048.
Swarm listening on /ip4/127.0.0.1/tcp/4001
Swarm listening on /ip4/192.168.1.5/tcp/4001
Swarm listening on /ip6/::1/tcp/4001
Swarm listening on /p2p-circuit/ipfs/QmeUHZQR35N3Bk8yXy73DNQFtCxQptPmerJHALHiawmojL
Swarm announcing /ip4/127.0.0.1/tcp/4001
Swarm announcing /ip4/192.168.1.5/tcp/4001
Swarm announcing /ip6/::1/tcp/4001
API server listening on /ip4/127.0.0.1/tcp/5001
Gateway (readonly) server listening on /ip4/127.0.0.1/tcp/8080
Daemon is ready

再次访问(如果无法查看,请科学上网):http://ipfs.io/ipfs/QmUWMYMnFVerVdujyWqmUayUyWHc4tyt9DP2y8xbSBw2fq

3. 上传文件夹到节点

目录结构:


lgydembp:IPFS liguoyu$ mkdir IPFS_TEST
lgydembp:IPFS liguoyu$ ls IPFS_TEST/
030.jpg     test01.txt  test02.txt  test03.txt
lgydembp:IPFS liguoyu$ ipfs add -r IPFS_TEST/
added QmRGYMf8tD5Xv5k6tKN72odhgeyFWSWdiWcTUdWrTsom9J IPFS_TEST/030.jpg
added QmRX7AvybkZXkH5fET3DjQZAoxXaY26Mib6AM7YCKHavPa IPFS_TEST/test01.txt
added QmShxCCUsvF3sa2vkQcLogVMGJCdq3gt6WGpeL5e5ujFMV IPFS_TEST/test02.txt
added QmZBNcscWiKHjXNnqkKmb8dwWk7mMSsrLbcUTkBB99Evj4 IPFS_TEST/test03.txt
added QmQvw4Wn8rRXcg7t1fFC27uc9RomeH6k7cC4iTzCd4AG7F IPFS_TEST
gydembp:IPFS liguoyu$ 

多种方式查看test01.txt文件数据

lgydembp:IPFS liguoyu$ ipfs cat QmRX7AvybkZXkH5fET3DjQZAoxXaY26Mib6AM7YCKHavPa
第一个test 文件
lgydembp:IPFS liguoyu$ ipfs cat /ipfs/QmRX7AvybkZXkH5fET3DjQZAoxXaY26Mib6AM7YCKHavPa
第一个test 文件
lgydembp:IPFS liguoyu$ ipfs cat /ipfs/QmQvw4Wn8rRXcg7t1fFC27uc9RomeH6k7cC4iTzCd4AG7F/test01.txt
第一个test 文件

浏览器浏览数据:
https://ipfs.io/ipfs/QmQvw4Wn8rRXcg7t1fFC27uc9RomeH6k7cC4iTzCd4AG7F

4. IPNS绑定:

当我们修改网站内容重新添加到ipfs时,hash会发生变化,这时,我们可以将网站发布到IPNS,在IPNS中,允许我们节点的域名空间中引用一个IPFS hash,也就是说我们可以通过节点ID项目根目录的IPFS HASH进行绑定,以后我们访问网站时直接通过节点·ID访问即可,当我们更新文件时,重新发布到IPNS`即可。

lgydembp:IPFS liguoyu$ ipfs name publish QmQvw4Wn8rRXcg7t1fFC27uc9RomeH6k7cC4iTzCd4AG7F
Published to QmeUHZQR35N3Bk8yXy73DNQFtCxQptPmerJHALHiawmojL: /ipfs/QmQvw4Wn8rRXcg7t1fFC27uc9RomeH6k7cC4iTzCd4AG7F
lgydembp:IPFS liguoyu$ ipfs id
{
    "ID": "QmeUHZQR35N3Bk8yXy73DNQFtCxQptPmerJHALHiawmojL"
}
lgydembp:IPFS liguoyu$ 

当我们执行ipfs name publish命令时,会返回我们的节点ID与绑定的hash信息,可以通过ipfs id进行查看验证是否是你的节点ID。

验证

ipfs name resolve <peerId>
lgydembp:IPFS liguoyu$ ipfs name resolve QmeUHZQR35N3Bk8yXy73DNQFtCxQptPmerJHALHiawmojL
/ipfs/QmQvw4Wn8rRXcg7t1fFC27uc9RomeH6k7cC4iTzCd4AG7F

访问地址:
https://ipfs.io/ipns/QmeUHZQR35N3Bk8yXy73DNQFtCxQptPmerJHALHiawmojL
上面地址是ipns而不是ipfs,hash为本节点id

相关文章

网友评论

      本文标题:IPFS 02-基本使用

      本文链接:https://www.haomeiwen.com/subject/fkjlgftx.html