区块链

开始使用IPFS

2018-05-29  本文已影响2人  DavieKong

下载

下载对应平台的安装包:https://dist.ipfs.io/#go-ipfs

使用预制包安装

Mac OS X and Linux 系统下

下载后,解压压缩包,运行 install.sh脚本,它会把 ipfs添加到/usr/local/bin,为的是之后可以执行ipfs。

tar xvfz go-ipfs.tar.gz
cd go-ipfs
./install.sh

Windows系统下

解压zip压缩包,把ipfs.exe文件所在路径添加到环境变量中。

测试一下是否安装成功

命令行输入 ipfs:

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.

出现上面的效果,恭喜你,IPFS就安装好了。

还可以使用 ipfs-update 来安装,以及通过编译源码来安装,参看官方文档:https://github.com/ipfs/go-ipfs#build-from-source,这里我就不详细讲解了。

开始使用

初始化仓库

ipfs会使用一个全区的本地仓库,位置在 ~/.ipfs
创建这个仓库:

> ipfs init
initializing ipfs node at /Users/jbenet/.go-ipfs
generating 2048-bit RSA keypair...done
peer identity: Qmcpo2iLBikrdf1d6QU6vXuNb6P7hwrbNPW9kLAH8eG67z
to get started, enter:

  ipfs cat /ipfs/QmYwAPJzv5CZsnA625s3Xf2nemtYgPpHdWEz79ojWnPbdG/readme

ipfs会在用户目录下创建一个 .ipfs的文件夹。
注意:你运行产生的hash跟我会不一样。
最后有一句提示:ipfs cat /ipfs/QmYwAPJzv5CZsnA625s3Xf2nemtYgPpHdWEz79ojWnPbdG/readm

运行这句,显示如下:

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

往仓库中添加文件

echo "hello world" >hello

这里把 “hello world”写入到了hello文件中,添加到ipfs系统中

ipfs add hello
added QmT78zSuBmuS4z925WZfrqQ1qHaJ56DQaTfyMUF7F8ff5o hello

添加成功后,可以得到一个hash,如果要查看这个文件的话

ipfs cat QmT78zSuBmuS4z925WZfrqQ1qHaJ56DQaTfyMUF7F8ff5o
hello world

就可以看到内容了。

试试文件夹

  mkdir foo
  mkdir foo/bar
  echo "baz" > foo/baz
  echo "baz" > foo/bar/baz
  ipfs add -r foo

输出:

added QmWLdkp93sNxGRjnFHPaYg8tCQ35NBY3XPn6KiETd3Z4WR foo/bar/baz
added QmWLdkp93sNxGRjnFHPaYg8tCQ35NBY3XPn6KiETd3Z4WR foo/baz
added QmeBpzHngbHes9hoPjfDCmpNHGztkmZFRX4Yp9ftKcXZDN foo/bar
added QmdcYvbv8FSBfbq1VVSfbjLokVaBYRLKHShpnXu3crd3Gm foo

查看内容

ipfs cat QmWLdkp93sNxGRjnFHPaYg8tCQ35NBY3XPn6KiETd3Z4WR
baz

上线

重新打开一个命令行窗口,执行ipfs daemon

ipfs daemon
Initializing daemon...
Swarm listening on /ip4/127.0.0.1/tcp/4001
Swarm listening on /ip4/192.168.3.160/tcp/4001
Swarm listening on /ip6/::1/tcp/4001
Swarm listening on /ip6/fda9:3785:8dca:d8a:21f5:fe23:78fe:ec48/tcp/4001
Swarm listening on /p2p-circuit/ipfs/QmZgt7YohZHkExJCRPynq6iG2ZyUSui9XZUnTQceVnwERW
Swarm announcing /ip4/127.0.0.1/tcp/4001
Swarm announcing /ip4/192.168.3.160/tcp/4001
Swarm announcing /ip6/::1/tcp/4001
Swarm announcing /ip6/fda9:3785:8dca:d8a:21f5:fe23:78fe:ec48/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://127.0.0.1:8080/ipfs/$hash"

image.png
在浏览器查看添加到ipfs里的文件内容。
或者在命令行里:
curl "http://127.0.0.1:8080/ipfs/QmT78zSuBmuS4z925WZfrqQ1qHaJ56DQaTfyMUF7F8ff5o"
hello world

默认情况下,你的网关是不会暴露给外部,所以只能在本地访问。

网络控制台

还有一个更好看的查看节点状态的方式,在浏览器访问:
http://localhost:5001/webui

image.png

查看连接的周围节点:


image.png

其他示例:https://ipfs.io/docs/examples/

原文连接:https://ipfs.io/docs/install/

上一篇下一篇

猜你喜欢

热点阅读