美文网首页IPFS教程IPFS
IPFS入门-HelloWorld

IPFS入门-HelloWorld

作者: 程序员大叔日记 | 来源:发表于2018-01-21 15:22 被阅读577次

    本文由币乎(bihu.com)内容支持计划奖励

    目录

    1. 新建一个HelloWorld.html文件
    2. 查看ipfs使用命令
    3. 将HelloWorld.htm添加到ipfs节点
    4. 同步节点文件
    5. 从IPFS网络查看文件

    1 新建一个HelloWorld.html文件

    新增一个文件夹,在文件夹新建一个HelloWorld.html文件:

    image

    文件内容是一段简单html:

    <html>
    <head>
    <title>IPFS,Hello World!</title>
    </head>
    <body>
    <p>IPFS,Hello World!</p>
    </body>
    </html>
    
    

    2 查看ipfs使用命令

    新建一个终端,执行命令ipfs.exe help,可以查到相关使用命令,输入命令如下:

    D:\ipfs\go-ipfs>ipfs.exe 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 将HelloWorld.html添加到ipfs节点

    首先我们将HelloWord.html添加到本地节点,在终端输入一下命令:

    D:\ipfs\go-ipfs>ipfs.exe add D:\ipfs\lean\HelloWorld.html
    109 B / 109 B [==================================================================================================================================
    added QmZ52xEibwKm28x4942KsLAs4njhF3QtkbNcs4pC4LCyBp HelloWorld.html

    当执行完ipfs.exe add HelloWorld.html 命令之后,就会将HelloWorld.html 添加到ipfs当前节点中,并且会对HelloWorld.html 文件生产一个唯一的hash:QmSzPqkCCBHmyymof83g8xPZjiZHyVz1UkNuGXUGutFPaK

    如果想看本地ipfs节点数据,可以通过输入下面命令:

    D:\ipfs\go-ipfs>ipfs.exe cat QmZ52xEibwKm28x4942KsLAs4njhF3QtkbNcs4pC4LCyBp
    <html>
    <head>
    <title>IPFS,Hello World!</title>
    </head>
    <body>
    <p>IPFS,Hello World!</p>
    </body>
    </html>

    4 同步节点文件

    虽然文件已经添加到当前自己的IPFS节点中,如果想同步到IPFS网络上,还需要进行同步操作,接下来执行同步命令,将节点文件同步到IPFS网络,输入命令:

    D:\ipfs\go-ipfs>ipfs.exe daemon
    Initializing daemon...
    Swarm listening on /ip4/127.0.0.1/tcp/4001
    Swarm listening on /ip4/169.254.35.144/tcp/4001
    Swarm listening on /ip4/169.254.36.228/tcp/4001
    Swarm listening on /ip4/192.168.0.103/tcp/4001
    Swarm listening on /ip6/::1/tcp/4001
    Swarm listening on /p2p-circuit/ipfs/QmNaTdLDsVymhfhNxbPVkSN9jAbrZHYWvg9b83j2PLWUcy
    Swarm announcing /ip4/127.0.0.1/tcp/4001
    Swarm announcing /ip4/169.254.35.144/tcp/4001
    Swarm announcing /ip4/169.254.36.228/tcp/4001
    Swarm announcing /ip4/192.168.0.103/tcp/4001
    Swarm announcing /ip4/192.168.1.2/tcp/16143
    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

    5 从IPFS网络查看文件

    同步执行完成之后,可以通过公网地址直接访问第4步同步的HelloWorld.html
    浏览器输入:

    https://ipfs.io/ipfs/QmZ52xEibwKm28x4942KsLAs4njhF3QtkbNcs4pC4LCyBp

    image.png

    至此已经成功将Hello World发送到IPFS网络上!更多精彩章节后续更新!

    相关文章

      网友评论

      • Vincent_a7ee:你好,ipfs入门需要学什么编程语言呢
      • 陈友辉:你这个文件似乎凉了,打不开
        bobo_wan:@陈友辉 已经被墙了
        陈友辉:@程序员大叔日记 我自己搭建,似乎也打不开?我的操作问题?
        程序员大叔日记:@陈友辉 这两天网站打不开

      本文标题:IPFS入门-HelloWorld

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