Dapp开发

IPFS环境配置

2018-04-11  本文已影响99人  yuyangray

1. IPFS简介

IPFS(InterPlanetary File System)是一个点对点的分布式超媒体分发协议,它整合了过去几年最好的分布式系统思路,为所有人提供全球统一的可寻址空间,包括Git、自证明文件系统SFSBitTorrentDHT,同时也被认为是最有可能取代HTTP的新一代互联网协议。

IPFS用基于内容的寻址替代传统的基于域名的寻址,用户不需要关心服务器的位置,不用考虑文件存储的名字和路径。我们将一个文件放到IPFS节点中,将会得到基于其内容计算出的唯一加密哈希值。哈希值直接反映文件的内容,哪怕只修改1比特,哈希值也会完全不同。当IPFS被请求一个文件哈希时,它会使用一个分布式哈希表找到文件所在的节点,取回文件并验证文件数据。

IPFS是通用目的的基础架构,基本没有存储上的限制。大文件会被切分成小的分块,下载的时候可以从多个服务器同时获取。IPFS的网络是不固定的、细粒度的、分布式的网络,可以很好的适应内容分发网络的要求。这样的设计可以很好的共享各类数据,包括图像、视频流、分布式数据库、整个操作系统、模块链、8英寸软盘的备份,还有静态网站。

IPFS提供了一个友好的WEB访问接口,用户可通过http://ipfs.io/hash 获取IPFS网络中的内容,也许在不久的将来,IPFS协议将会彻底替代传统的HTTP协议。

2. IPFS本地环境安装

2.1 下载安装包

官网:https://ipfs.io

2.2 安装

解压

yuyangdeMacBook-Pro:~ yuyang$ tar xvfz go-ipfs_v0.4.14_darwin-amd64.tar
x go-ipfs/build-log
x go-ipfs/install.sh
x go-ipfs/ipfs
x go-ipfs/LICENSE
x go-ipfs/README.md

安装

yuyangdeMacBook-Pro:~ yuyang$ cd go-ipfs
yuyangdeMacBook-Pro:go-ipfs yuyang$ ./install.sh
Moved ./ipfs to /usr/local/bin
yuyangdeMacBook-Pro:go-ipfs yuyang$ ipfs help
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.

3. 项目配置

3.1 创建ipfs节点

为了运行项目,我们需要通过ipfs init在本地计算机建立一个IPFS节点。

yuyangdeMacBook-Pro:~ yuyang$ ipfs init
initializing IPFS node at /Users/yuyang/.ipfs
generating 2048-bit RSA keypair...done
peer identity: QmVhF9zg4qcrVTw8jkkCGvTJGXJjvwX6CvRuxzUErEyx97
to get started, enter:

    ipfs cat /ipfs/QmS4ustL54uo8FzR9455qaxZwuMiUhyvMcX9Ba8nUH4uVv/readme

3.2 修改节点默认存储空间

执行完ipfs init命令后,会在根目录生成一个.ipfs的文件夹存储节点数据。

.ipfs的文件夹默认是隐藏的。Command+Shift+. 可以显示隐藏文件、文件夹,再按一次,恢复隐藏。

.ipfs节点默认存储空间为10个G。

如果你自己想修改节点默认存储空间,可打开终端执行下面的命令。

yuyangdeMacBook-Pro:~ yuyang$ export EDITOR=/usr/bin/vim
yuyangdeMacBook-Pro:~ yuyang$ ipfs config edit

执行完ipfs config edit命令后会打开一个文件,在这个文件中找到下图中绘制红线的位置,将10GB修改成你自己想要的存储空间。修改完毕,保存退出。(PS:输入i可以开始编辑,编译完毕后按esc键,再输入:wq保存并且退出)。

3.3 查看节点id

yuyangdeMacBook-Pro:~ yuyang$ ipfs id
{
    "ID": "QmVhF9zg4qcrVTw8jkkCGvTJGXJjvwX6CvRuxzUErEyx97",
    "PublicKey": "CAASpgIwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQC6fMRFRKKg7rn7fJVVENsmSp/qftAV0FApEEFi0LNJOW9oYENCNykCc6Vpwg0/9oXjfkywAJqENgEycUV1ZdK75Ef9NJlY5vy62e2aitF83ock89zAeXXLU4GhXifW/foT5DsHHSe0rseBmybZDHjAOJtfhaN4K4yusw23wzvt9Fg8S3qNszEaImYtWUlS08piiR0tWdz+24I/oWiSnWB855KVCdLnYnwy6M3OLgeuJvXB2XGwnbtj4Q1jvp9rf/KGjHtb0KB9DwfowZ/ypvrDUNakbjNGYfn3I+AvRl1Qz4++o57VtbRmZYThygWnz9vTjqj0NDX2ftYh6MrZkSg5AgMBAAE=",
    "Addresses": null,
    "AgentVersion": "go-ipfs/0.4.14/",
    "ProtocolVersion": "ipfs/0.1.0"
}

QmVhF9zg4qcrVTw8jkkCGvTJGXJjvwX6CvRuxzUErEyx97为我的节点ID。每个节点都会有一个唯一的ID

3.4 启动节点服务器

yuyangdeMacBook-Pro:~ yuyang$ ipfs daemon
Initializing daemon...
Successfully raised file descriptor limit to 2048.
Swarm listening on /ip4/10.1.0.117/tcp/4001
Swarm listening on /ip4/127.0.0.1/tcp/4001
Swarm listening on /ip4/192.168.0.4/tcp/4001
Swarm listening on /ip6/::1/tcp/4001
Swarm listening on /p2p-circuit/ipfs/QmVhF9zg4qcrVTw8jkkCGvTJGXJjvwX6CvRuxzUErEyx97
Swarm announcing /ip4/10.1.0.117/tcp/4001
Swarm announcing /ip4/127.0.0.1/tcp/4001
Swarm announcing /ip4/192.168.0.4/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

3.5 跨域资源共享CORS配置

为了后续的开发方便,我们还需要对跨域资源共享( CORS )进行配置,ctrl- c退出ipfs,然后按照下面的步骤进行跨域配置。

3.6 验证

yuyangdeMacBook-Pro:~ yuyang$ ipfs daemon
yuyangdeMacBook-Pro:~ yuyang$ ipfs cat /ipfs/QmS4ustL54uo8FzR9455qaxZwuMiUhyvMcX9Ba8nUH4uVv/readme

这里的节点id可以查看3.1创建节点时返回的信息:

to get started, enter:

    ipfs cat /ipfs/QmS4ustL54uo8FzR9455qaxZwuMiUhyvMcX9Ba8nUH4uVv/readme

返回信息:

yuyangdeMacBook-Pro:~ yuyang$ 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

打开http://localhost:5001/webui会看到一个漂亮的UI界面。

参考:【IPFS + 区块链 系列】 入门篇 - IPFS环境配置
作者:黎跃春

上一篇下一篇

猜你喜欢

热点阅读